Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 仅在值出现两次时从CSV中获取最近的行_Java_Csv_Duplicates_Mule_Dataweave - Fatal编程技术网

Java 仅在值出现两次时从CSV中获取最近的行

Java 仅在值出现两次时从CSV中获取最近的行,java,csv,duplicates,mule,dataweave,Java,Csv,Duplicates,Mule,Dataweave,我正在使用Mule中的CSV文件,该文件可能类似于以下内容: ID|LastUpdated 01|01/12/2016 09:00:00 01|01/12/2016 09:45:00 02|01/12/2016 09:00:00 02|01/12/2016 09:45:00 03|01/12/2016 09:00:00 ID|LastUpdated 01|01/12/2016 09:45:00 02|01/12/2016 09:45:00 03|01/12/2016 09:00:00 我正试

我正在使用Mule中的CSV文件,该文件可能类似于以下内容:

ID|LastUpdated
01|01/12/2016 09:00:00
01|01/12/2016 09:45:00
02|01/12/2016 09:00:00
02|01/12/2016 09:45:00
03|01/12/2016 09:00:00
ID|LastUpdated
01|01/12/2016 09:45:00
02|01/12/2016 09:45:00
03|01/12/2016 09:00:00
我正试图找到一种方法,通过只获取由
LastUpdated
列确定的最新值,来去除
ID
值的所有重复出现。我正试图使用DataWeave实现这一点,但到目前为止还没有成功。我愿意将逻辑写入自定义Java类,但对如何实现这一点也知之甚少

我期望的输出如下所示:

ID|LastUpdated
01|01/12/2016 09:00:00
01|01/12/2016 09:45:00
02|01/12/2016 09:00:00
02|01/12/2016 09:45:00
03|01/12/2016 09:00:00
ID|LastUpdated
01|01/12/2016 09:45:00
02|01/12/2016 09:45:00
03|01/12/2016 09:00:00
任何帮助或指导都将不胜感激

编辑:值得注意的是,我希望入站文件相当大(多达000行),因此我需要了解解决方案的性能


编辑:可以在Mulesoft论坛上找到使用DataWeave的解决方案。

如果日期/小时总是按CSV排序,如您给出的示例所示,您可以将所有ID上的参考作为键保留在地图中,只需更新与ID对应的值:

public static void main(String[] arg){
    // I replace all the CSV reading by this list for the example
    ArrayList<String> lines = new ArrayList<>();
    lines.add("01|01/12/2016 09:00:00");
    lines.add("01|01/12/2016 09:45:00");
    lines.add("02|01/12/2016 09:00:00");
    lines.add("02|01/12/2016 09:45:00");
    lines.add("03|01/12/2016 09:00:00");
    Iterator it = lines.iterator();
    
    Map<String, String> lastLines = new HashMap<String, String>();
    while (it.hasNext()) { // Iterator on the CVS lines here
        String s = (String)it.next();
        String id = s.substring(0,  s.indexOf("|"));
        String val = s.substring(s.indexOf("|") + 1 , s.length());
        lastLines.put(id, val);
    }
    Iterator<String> keys = lastLines.keySet().iterator();
    while (keys.hasNext()) {
        String id = (String) keys.next();
        System.out.println(id + "|" + lastLines.get(id));
    }
}
publicstaticvoidmain(字符串[]arg){
//对于示例,我用此列表替换所有CSV读数
ArrayList行=新的ArrayList();
行。添加(“01 | 01/12/2016 09:00:00”);
行。添加(“01 | 01/12/2016 09:45:00”);
行。添加(“02 | 01/12/2016 09:00:00”);
行。添加(“02 | 01/12/2016 09:45:00”);
行。添加(“03 | 01/12/2016 09:00:00”);
迭代器it=lines.Iterator();
Map lastLines=newhashmap();
而(it.hasNext()){//Iterator在这里的CVS行上
String s=(String)it.next();
字符串id=s.substring(0,s.indexOf(“|”);
字符串val=s.substring(s.indexOf(“|”)+1,s.length();
lastLines.put(id,val);
}
迭代器键=lastLines.keySet().Iterator();
while(keys.hasNext()){
String id=(String)keys.next();
System.out.println(id+“|”+lastLines.get(id));
}
}
这将产生:

01 | 01/12/2016 09:45:00

02 | 01/12/2016 09:45:00

03 | 01/12/2016 09:00:00

如果CSV记录可以按任何顺序排列,则需要添加日期验证,以仅保留每个id的最新日期

private static final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");

public static void main(String... args) {
    // I replace all the CSV reading by this list for the example
    ArrayList<String> lines = new ArrayList<>();
    
    lines.add("01|01/12/2016 09:45:00");
    lines.add("01|01/12/2016 09:00:00");
    lines.add("02|01/12/2016 09:00:00");
    lines.add("02|01/12/2016 09:45:00");
    lines.add("03|01/12/2016 09:00:00");
    Iterator it = lines.iterator();

    Map<String, String> lastLines = new HashMap<String, String>();
    while (it.hasNext()) { // Iterator on the CVS lines here
        String s = (String)it.next();
        String id = s.substring(0,  s.indexOf("|"));
        String val = s.substring(s.indexOf("|") + 1 , s.length());
        if(lastLines.containsKey(id)){
            try{
                Date storeDate = sdf.parse(lastLines.get(id));
                Date readDate = sdf.parse(val);
                if(readDate.getTime() > storeDate.getTime())
                    lastLines.put(id, val);
            }catch(ParseException pe){
                pe.printStackTrace();
            }
        }else{
            lastLines.put(id, val);
        }
    }
    Iterator<String> keys = lastLines.keySet().iterator();
    while (keys.hasNext()) {
        String id = (String) keys.next();
        System.out.println(id + "|" + lastLines.get(id));
    }

}
private static final SimpleDateFormat sdf=新的SimpleDateFormat(“dd/MM/yyyy hh:MM:ss”);
公共静态void main(字符串…参数){
//对于示例,我用此列表替换所有CSV读数
ArrayList行=新的ArrayList();
行。添加(“01 | 01/12/2016 09:45:00”);
行。添加(“01 | 01/12/2016 09:00:00”);
行。添加(“02 | 01/12/2016 09:00:00”);
行。添加(“02 | 01/12/2016 09:45:00”);
行。添加(“03 | 01/12/2016 09:00:00”);
迭代器it=lines.Iterator();
Map lastLines=newhashmap();
而(it.hasNext()){//Iterator在这里的CVS行上
String s=(String)it.next();
字符串id=s.substring(0,s.indexOf(“|”);
字符串val=s.substring(s.indexOf(“|”)+1,s.length();
if(lastLines.containsKey(id)){
试一试{
datestoredate=sdf.parse(lastLines.get(id));
Date readDate=sdf.parse(val);
if(readDate.getTime()>storeDate.getTime())
lastLines.put(id,val);
}捕获(解析异常pe){
pe.printStackTrace();
}
}否则{
lastLines.put(id,val);
}
}
迭代器键=lastLines.keySet().Iterator();
while(keys.hasNext()){
String id=(String)keys.next();
System.out.println(id+“|”+lastLines.get(id));
}
}

我不确定您当前使用的日期格式。您可能需要更改解析器的格式
“dd/MM/yyyy hh:MM:ss”
。您可以找到相关文档

如果日期/小时始终按CSV排序,如您给出的示例所示,您可以将所有ID上的参考作为键保留在地图中,只需更新与ID对应的值:

public static void main(String[] arg){
    // I replace all the CSV reading by this list for the example
    ArrayList<String> lines = new ArrayList<>();
    lines.add("01|01/12/2016 09:00:00");
    lines.add("01|01/12/2016 09:45:00");
    lines.add("02|01/12/2016 09:00:00");
    lines.add("02|01/12/2016 09:45:00");
    lines.add("03|01/12/2016 09:00:00");
    Iterator it = lines.iterator();
    
    Map<String, String> lastLines = new HashMap<String, String>();
    while (it.hasNext()) { // Iterator on the CVS lines here
        String s = (String)it.next();
        String id = s.substring(0,  s.indexOf("|"));
        String val = s.substring(s.indexOf("|") + 1 , s.length());
        lastLines.put(id, val);
    }
    Iterator<String> keys = lastLines.keySet().iterator();
    while (keys.hasNext()) {
        String id = (String) keys.next();
        System.out.println(id + "|" + lastLines.get(id));
    }
}
publicstaticvoidmain(字符串[]arg){
//对于示例,我用此列表替换所有CSV读数
ArrayList行=新的ArrayList();
行。添加(“01 | 01/12/2016 09:00:00”);
行。添加(“01 | 01/12/2016 09:45:00”);
行。添加(“02 | 01/12/2016 09:00:00”);
行。添加(“02 | 01/12/2016 09:45:00”);
行。添加(“03 | 01/12/2016 09:00:00”);
迭代器it=lines.Iterator();
Map lastLines=newhashmap();
而(it.hasNext()){//Iterator在这里的CVS行上
String s=(String)it.next();
字符串id=s.substring(0,s.indexOf(“|”);
字符串val=s.substring(s.indexOf(“|”)+1,s.length();
lastLines.put(id,val);
}
迭代器键=lastLines.keySet().Iterator();
while(keys.hasNext()){
String id=(String)keys.next();
System.out.println(id+“|”+lastLines.get(id));
}
}
这将产生:

01 | 01/12/2016 09:45:00

02 | 01/12/2016 09:45:00

03 | 01/12/2016 09:00:00

如果CSV记录可以按任何顺序排列,则需要添加日期验证,以仅保留每个id的最新日期

private static final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");

public static void main(String... args) {
    // I replace all the CSV reading by this list for the example
    ArrayList<String> lines = new ArrayList<>();
    
    lines.add("01|01/12/2016 09:45:00");
    lines.add("01|01/12/2016 09:00:00");
    lines.add("02|01/12/2016 09:00:00");
    lines.add("02|01/12/2016 09:45:00");
    lines.add("03|01/12/2016 09:00:00");
    Iterator it = lines.iterator();

    Map<String, String> lastLines = new HashMap<String, String>();
    while (it.hasNext()) { // Iterator on the CVS lines here
        String s = (String)it.next();
        String id = s.substring(0,  s.indexOf("|"));
        String val = s.substring(s.indexOf("|") + 1 , s.length());
        if(lastLines.containsKey(id)){
            try{
                Date storeDate = sdf.parse(lastLines.get(id));
                Date readDate = sdf.parse(val);
                if(readDate.getTime() > storeDate.getTime())
                    lastLines.put(id, val);
            }catch(ParseException pe){
                pe.printStackTrace();
            }
        }else{
            lastLines.put(id, val);
        }
    }
    Iterator<String> keys = lastLines.keySet().iterator();
    while (keys.hasNext()) {
        String id = (String) keys.next();
        System.out.println(id + "|" + lastLines.get(id));
    }

}
private static final SimpleDateFormat sdf=新的SimpleDateFormat(“dd/MM/yyyy hh:MM:ss”);
公共静态void main(字符串…参数){
//对于示例,我用此列表替换所有CSV读数
ArrayList行=新的ArrayList();
行。添加(“01 | 01/12/2016 09:45:00”);
行。添加(“01 | 01/12/2016 09:00:00”);
行。添加(“02 | 01/12/2016 09:00:00”);
行。添加(“02 | 01/12/2016 09:45:00”);
行。添加(“03 | 01/12/2016 09:00:00”);
迭代器it=lines.Iterator();
Map lastLines=newhashmap();
而(it.hasNext()){//Iterator在这里的CVS行上
String s=(String)it.next();
字符串id=s.substring(0,s.indexOf(“|”);
字符串val=s.substring(s.indexOf(“|”)+1,s.length();
if(lastLines.containsKey(id)){
试一试{
datestoredate=sdf.parse(lastLines.get(id));
日期