Univocity 解析器 - 处理具有奇怪构造的行

Univocity parser - Handling lines with weird constructs

我正在尝试找出使用大学解析器处理 CSV 日志文件的最佳方法,其中的行如下所示,

"23.62.3.74",80,"testUserName",147653,"Log Collection Device 100","31/02/15 00:05:10 GMT",-1,"10.37.255.3", "TCP","destination_ip=192.62.3.74|product_id=0071|option1_type=(s-dns)|proxy_machine_ip=10.1.255.3"

如您所见,这是一个逗号分隔的文件,但最后一列有一堆以其字段名称为前缀的值。我的要求是从普通字段中捕获值,并且 有选择地从这最后一个大领域。

我知道 Univocity 中的主详细信息行处理器,但我怀疑它是否属于该类别。你能指导我正确的方向吗?

注意:如果我实现行处理器,我可以处理 rowProcessed(String[] row, ParsingContext context) 中的名称前缀字段,但如果可能,我正在寻找 Univocity 原生的东西?

谢谢, R

解析器中没有任何原生的东西。可能最简单的方法就是像你提到的那样 RowProcessor

为了让您的生活更轻松,您可以尝试做的一件事是使用 CsvParser 的另一个实例来解析最后一条记录:

//initialize a parser for the pipe separated bit
CsvParserSettings detailSettings = new CsvParserSettings();
detailSettings.getFormat().setDelimiter('=');
detailSettings.getFormat().setLineSeparator("|");
CsvParser detailParser = new CsvParser(detailSettings);

//here is the content of the last column (assuming you got it from the parser)
String details = "destination_ip=192.62.3.74|product_id=0071|option1_type=(s-dns)|proxy_machine_ip=10.1.255.3";

//The result will be a list of pairs
List<String[]> pairs = detailParser.parseAll(new StringReader(details));

//You can add the pairs to a map
Map<String, String> map = new HashMap<String, String>();
for (String[] pair : pairs) {
    map.put(pair[0], pair[1]);
}

//this should print: {destination_ip=192.62.3.74, product_id=0071, proxy_machine_ip=10.1.255.3, option1_type=(s-dns)}
System.out.println(map);

这不会非常快,但如果该输入可以具有随机的列名称和与之关联的值,至少可以很容易地使用地图。