gpt4 book ai didi

java - 通过分页写入 Jena RDF 模型

转载 作者:行者123 更新时间:2023-12-01 13:15:51 24 4
gpt4 key购买 nike

我打算将 SQL 数据库中的数据转换为 RDF 转储。我定义了一个模型和一个本体。

Model model = ModelFactory.createDefaultModel();
OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, model);

ontModel 中定义了许多类。现在假设我的 SQL 数据库中有 10000 条记录,我想将它们加载到模型中并将其写入文件中。但是,我想在内存溢出的情况下进行分页。

int fromIndex = 0;
int toIndex = 10;

while(true) {
//1. get resources between fromIndex to toIndex from sql db
// if no more resources 'break'
//2. push these resources in model
//3. write the model to a file
RDFWriter writer = model.getWriter();
File file = new File(file_path);
FileWriter fileWriter = new FileWriter(file, true);
writer.write(this.model, fileWriter, BASE_URL);
model.close();

from = to+1;
to = to+10;

}

现在,如何将新资源追加到文件中的现有资源中。因为目前我看到本体被写入了两次并且抛出了异常

org.apache.jena.riot.RiotException: The markup in the document following the root element must be well-formed.

有办法解决这个问题吗?

最佳答案

Now, how does one append the new resources to the existing resources in the file. Because currently I see the ontology getting written twice and it throws an exception

模型是一组三元组。您可以向集合中添加更多三元组,但这与“附加”不太一样,因为模型不包含重复的三元组,并且集合没有指定的顺序,因此“附加”并不完全是正确的比喻。

Jena 可以处理相当大的模型,因此您可能首先看看是否可以创建模型并向其中添加所有内容,然后将模型写入文件。虽然谨慎是好事,但看看自己是否能做自己想做的事,而不需要经历太多的困难,这也不是一个坏主意。

如果您确实对内存模型有问题,您可以考虑使用 TDB 支持的模型,该模型将使用磁盘进行存储。您可以使用模型 API 或 SPARQL 查询对该模型进行增量更新,然后在某些序列化中提取模型。

还有一个选项,如果您确实想要附加到文件,这可能是最简单的选项,那就是使用 RDF 的非 XML 序列化,例如 Turtle 或 N-Triples。这些是基于文本的(N-Triples 是基于行的),因此将新内容附加到文件中不是问题。 Adding more individuals to existing RDF ontology 的答案中描述了这种方法。 .

关于java - 通过分页写入 Jena RDF 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22480470/

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