問題描述
我正在編寫一個應用程序,它處理大量具有深層節(jié)點結構的 xml 文件 (>1000).使用 woodstox (Event API) 大約需要 6 秒來解析一個包含 22.000 個節(jié)點的文件.
I'm writing an application which processes a lot of xml files (>1000) with deep node structures. It takes about six seconds with with woodstox (Event API) to parse a file with 22.000 Nodes.
算法被放置在與用戶交互的過程中,其中只有幾秒鐘的響應時間是可以接受的.所以我需要改進如何處理xml文件的策略.
The algorithm is placed in a process with user interaction where only a few seconds response time are acceptable. So I need to improve the strategy how to handle the xml files.
- 我的進程分析 xml 文件(僅提取幾個節(jié)點).
- 處理提取的節(jié)點并將新結果寫入新數據流(生成帶有修改節(jié)點的文檔副本).
現在我正在考慮一種多線程解決方案(在 16 Core+ 硬件上可以更好地擴展).我想到了以下策略:
Now I'm thinking about a multithreaded solution (which scales better on 16 Core+ hardware). I thought about the following stategies:
- 創(chuàng)建多個解析器并在 xml 源上并行運行它們.
- 重寫我的解析算法線程保存以僅使用解析器的一個實例(工廠,...)
- 將 XML 源拆分成塊并將這些塊分配給多個處理線程(map-reduce xml - 串行)
- 優(yōu)化我的算法(StAX 解析器比 woodstox 更好?)/使用內置并發(fā)的解析器
我想同時提高整體性能和每個文件"的性能.
您有解決此類問題的經驗嗎?最好的方法是什么?
Do you have experience with such problems? What is the best way to go?
推薦答案
這一點很明顯:只需創(chuàng)建幾個解析器并在多個線程中并行運行它們.
This one is obvious: just create several parsers and run them in parallel in multiple threads.
看看 Woodstox 性能(暫時關閉,試試 google 緩存).
Take a look at Woodstox Performance (down at the moment, try google cache).
如果您的 XML 結構是可預測的,則可以做到這一點:如果它有很多相同的頂級元素.例如:
This can be done IF structure of your XML is predictable: if it has a lot of same top-level elements. For instance:
<element>
<more>more elements</more>
</element>
<element>
<other>other elements</other>
</element>
在這種情況下,您可以創(chuàng)建簡單的拆分器來搜索 <element>
并將此部分提供給特定的解析器實例.這是一種簡化的方法:在現實生活中,我會使用 RandomAccessFile 來查找起點 (<element>
),然后創(chuàng)建僅對文件的一部分進行操作的自定義 FileInputStream.
In this case you could create simple splitter that searches <element>
and feeds this part to a particular parser instance. That's a simplified approach: in real life I'd go with RandomAccessFile to find start stop points (<element>
) and then create custom FileInputStream that just operates on a part of file.
看看 Aalto.創(chuàng)造伍德斯托克斯的人.這是該領域的專家 - 不要重新發(fā)明輪子.
Take a look at Aalto. The same guys that created Woodstox. This are experts in this area - don't reinvent the wheel.
這篇關于Java 中的并行 XML 解析的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!