gpt4 book ai didi

java - 特殊情况下的xml文件查询

转载 作者:行者123 更新时间:2023-12-01 09:21:44 27 4
gpt4 key购买 nike

我从 Stackoverflow 收集了 2 个大文件,名为 posts.xmlquestions.txt,其结构如下:

posts.xml:

<posts>
<row Id="4" PostTypeId="1" AcceptedAnswerId="7" CreationDate="2008-07-31T21:42:52.667" Score="322" ViewCount="21888" Body="..."/>
<row Id="6" PostTypeId="1" AcceptedAnswerId="31" CreationDate="2008-07-31T22:08:08.620" Score="140" ViewCount="10912" Body="..." />
...
</posts>

帖子可以是问题或答案(两者)

问题.txt:

Id,CreationDate,CreationDatesk,Score
123,2008-08-01 16:08:52,20080801,48
126,2008-08-01 16:10:30,20080801,33
...

我只想查询帖子一次,并使用 lucene 对所选行(其 ID 位于 questions.txt 文件中)建立索引。由于xml文件非常大(大约50GB),因此查询和索引的时间对我来说很重要。

现在的问题是:如何找到 posts.xml 中所有在 questions.txt 中重复的选定行

这是我到目前为止的方法:

SAXParserDemo.java:

public class SAXParserDemo {
public static void main(String[] args){

try {
File inputFile = new File("D:\\University\\Information Retrieval 2\\Hws\\Hw1\\files\\Posts.xml");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
UserHandler userhandler = new UserHandler();
saxParser.parse(inputFile, userhandler);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Handler.java:

public class Handler extends DefaultHandler {

public void getQuestiondId() {
ArrayList<String> qIDs = new ArrayList<String>();
BufferedReader br = null;
try {
String qId;
br = new BufferedReader(new FileReader("D:\\University\\Information Retrieval 2\\Hws\\Hw1\\files\\Q.txt"));
while ((qId = br.readLine()) != null) {
qId = qId.split(",")[0]; //this is question id
findAndIndexOnPost(qId); //find this id on posts.xml then index it!
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private void findAndIndexOnPost(String qID) {

}

@Override
public void startElement(String uri,
String localName, String qName, Attributes attributes)
throws SAXException {
if (qName.equalsIgnoreCase("row")) {
System.out.println(attributes.getValue("Id"));
switch (attributes.getValue("PostTypeId")) {
case "1":
String id = attributes.getValue("Id");
break;
case "2":
break;
default:
break;
}

}
}
}

更新:

我需要在每次迭代中将指针保留在 xml 文件上。但对于 SAX,我不知道如何做到这一点。

最佳答案

你需要做的是:

  • 读取 TXT 文件(可能一个简单的流就可以了)。
  • 添加全部Id值为 List<Integer> questionIds - 一一。您必须手动解析它们(使用正则表达式或 String.indexOf() )。
  • 在您的处理程序实现中,只需比较 if questionIds.contains(givenId) .
  • 通过简单的 REST 请求 (POST/PUT) 将接收到的对象(从 XML)发送到 Elastic Search。

哒哒!您的数据现已使用 lucene 建立索引。

此外,更改将数据传递到 SAX 解析器的方式。而不是给它一个 File ,创建 InputStream 的实现您可以将其捐赠给 saxParser.parse(inputStream, userhandler); 。有关获取流中位置的信息:Given a Java InputStream, how can I determine the current offset in the stream? .

关于java - 特殊情况下的xml文件查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40129855/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com