2016-09-20 34 views
1

我試圖找出使用大學解析器來處理CSV日誌文件用線條看起來像下面,單義解析器 - 怪異結構

「23.62.3.74」,80「 testUserName」最好的辦法處理線, 「日誌收集設備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

回答

1

沒有什麼本地的解析器這一點。可能最簡單的方法就是像你所提到的那樣使用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); 

這不會是非常快的,但至少它很容易工作如果該輸入可以具有與它們相關聯的隨機列名稱和值,則使用映射。

+0

感謝隊友..這是有用的,我有點擔心由於嚴格的性能要求的性能。如果我可能會去處理這個行處理器,並在看着「ConcurrentRowProcessor」,我可以試試看看我能擠出多少。乾杯.. R –

+1

很高興爲您提供幫助。我將嘗試想出一種更容易處理這種需求的方法,並將其添加到庫中。這類問題並不罕見。 –

+1

很好..是啊,這些日誌文件(通用事件格式)很常見,它們在線路本身很大。看到對這種格式的原生支持將會很有趣。再次像我上次說的那樣,當我得到一些空閒時間時,我可能會挖掘一些你的代碼庫,併爲你拍攝一個公關:) –