首页 > 解决方案 > 从 csv 文件中读取值并识别唯一名称

问题描述

我正在使用 talend 函数和组件从 csv 文件中读取和提取值,并在特定列 A 下查找唯一值。

csv 文件包含: CSV

在这种情况下,我想读取用作标题的 NAME 列,并仅识别该名称类别中的唯一名称(Tom、Jason、Roy、Bianca)并将其存储在一个数组中,我可以使用它们来检查来自新文件的传入名称存在或与从上面的 csv 文件中提取的名称匹配。

工作设计:

tJava

  int n = 600;  
  int i=0;
  String line="";
  int linenumber=0;
  int index=0;
       
 try(BufferedReader br = new BufferedReader(new FileReader((String)globalMap.get("tFileList_1_CURRENT_FILEPATH"))))
 { 
 while ((line = br.readLine()) != null && linenumber< n )  
 {
    String[] tokens = line.split(",");
    System.out.println("Columns=>" + tokens[0]);
  }
 }

context.name 将是包含所有唯一名称的全局变量是否有任何其他更有效的方法来处理 csv 文件并从列 A 中仅提取唯一名称并将其存储在我们可以对其进行验证的列表/数组中?

感谢您的投入。谢谢

我想最好的选择是使用 br.readLine() 并使用拆分函数(“,”)存储值并将其存储在哈希集中。然后 hashset.contains 验证新文件中的传入名称是否与存储的 hashset 值匹配,并相应地决定数据流。

我现在需要在 A 列下找到 UNIQUE 条目,我不希望标题显示在我的输出中。

输出:列名称列 Tom Columns Jason Columns Tom Columns Roy Columns Bianca

标签: javaarrayscsvinputtalend

解决方案


My advice would be:

  1. Create 2 HashSet, 'unique' and 'duplicate'.
  2. for each line, split it with a '.' being the delimiter and index the first element (being the name on that row)
  3. if duplicate.contains(name) == true, then continue to next name
  4. if unique.contains(name) == true, then unique.remove(name), duplicate.add(name), else unique.add(name)
  5. at the end, the unqiue hashset contains all the unique names.

推荐阅读