【问题标题】:Why is adding an RDF dump (InputStream) to a RDF4J repository so slow (in Java)?为什么将 RDF 转储 (InputStream) 添加到 RDF4J 存储库如此缓慢(在 Java 中)?
【发布时间】:2017-12-13 08:59:14
【问题描述】:

我正在从 Web 加载一个 RDF 臀部作为 InputStream,其中包含 120 到 1500 个三元组。平均而言,清除上下文大约需要半秒,而添加三元组大约需要 74 秒(对于 120 个三元组)。 RDFXML 序列化的物理文件大小在 6KB 到 195KB 之间。

InputStream input = ...
try (RepositoryConnection conn = db.getConnection()) {
    try {
        conn.clear(context);
        conn.add(input, "", RDFFormat.RDFXML, context);
    } catch (Exception e) {
        e.printStackTrace();
    } 
}

存储库初始化如下:

RemoteRepositoryManager manager = new RemoteRepositoryManager(serverUrl);
manager.initialize();
db = manager.getRepository("repo");

【问题讨论】:

  • “来自网络作为输入流”:您可以尝试将网络中的所有内容加载到临时文件中,然后将此临时文件添加到您的存储库中吗?也许 rdf4j 为每个三元组建立一个连接,这就是为什么需要这么长时间

标签: sesame triplestore rdf4j


【解决方案1】:

您可以尝试以下方法:

  1. 检查互联网下游,例如使用本地文件测试相同的代码。
  2. 检查互联网上游,例如使用内存回购 Repository repo = new SailRepository(new MemoryStore());
  3. 在 JAVA_OPTS 中使用 -Xmx 为您的 Java 应用程序提供足够的内存
  4. 不确定conn.clear(context); 的用途。据我了解it will remove all triples in the context?

在我这里,将 10,000,000 个 2.7G rdf 转储从 wikidata 加载到内存中的 repo 大约需要 5 分钟(我使用 export MAVEN_OPTS=-Xmx7000m 运行 maven 测试)。这使得每秒 ~33333 三倍 - 如果我计算正确;-)。

@Test
public void variant3() throws MalformedURLException, IOException {
    Repository repo = new SailRepository(new MemoryStore());
    repo.initialize();
    IRI context = repo.getValueFactory().createIRI("info/mycontext:context1");
    RDFFormat format = RDFFormat.NTRIPLES;
    System.out.println("Load zip file of format " + format);
    try (InputStream in = new URL(
                    "https://tools.wmflabs.org/wikidata-exports/rdf/exports/20160801/wikidata-terms.nt.gz")
                                    .openConnection().getInputStream();
                    NotifyingRepositoryConnectionWrapper con = new NotifyingRepositoryConnectionWrapper(repo,
                                    repo.getConnection());) {
        RepositoryConnectionListenerAdapter myListener = new RepositoryConnectionListenerAdapter() {
            private long count = 0;
            @Override
            public void add(RepositoryConnection arg0, Resource arg1, IRI arg2, Value arg3, Resource... arg4) {
                count++;
                if (count % 100000 == 0)
                    System.out.println("Add statement number " + count + "\n" + arg1 + " " + arg2 + " " + arg3);
            }
        };
        con.addRepositoryConnectionListener(myListener);
        con.add(in, "", format,context);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

【讨论】:

  • 最后,这是答案的一部分。另一部分是我打开了直接类型推断,不知何故,随着 KB 的增长,它变得越来越慢。
猜你喜欢
  • 1970-01-01
  • 2013-10-19
  • 1970-01-01
  • 2017-01-07
  • 2020-10-14
  • 2020-01-03
  • 2017-11-23
  • 2013-11-29
  • 1970-01-01
相关资源
最近更新 更多