如何使用 Jena TDB 存储链接电影数据库的本地版本

发布于 2024-10-31 08:12:14 字数 1161 浏览 1 评论 0原文

我有一个 N-Triples 格式的 LinkedMDB 本地版本,想要查询它。现在,我想使用Jena TDB,它可以存储可以用于以后查询的数据。我检查了 TDB Java API 文档,但无法加载 N- Triples 文件,然后使用 SPARQL 进行查询。我使用了以下代码:

String directory = "E:\\Applications\\tdb-0.8.9\\TDB-0.8.9\\bin\\tdb";
        Dataset dataset = TDBFactory.createDataset(directory);

        // assume we want the default model, or we could get a named model here
        Model tdb = dataset.getDefaultModel();

        // read the input file - only needs to be done once
        String source = "E:\\Applications\\linkedmdb-18-05-2009-dump.nt";
        FileManager.get().readModel( tdb, source, "N-TRIPLES" );

并得到以下异常

Exception in thread "main" com.hp.hpl.jena.tdb.base.file.FileException: Not a directory: E:\Applications\tdb-0.8.9\TDB-0.8.9\bin\tdb
    at com.hp.hpl.jena.tdb.base.file.Location.<init>(Location.java:83)
    at com.hp.hpl.jena.tdb.TDBFactory.createDataset(TDBFactory.java:79)
    at tutorial.Temp.main(Temp.java:14)

I have a local version of LinkedMDB that is in N-Triples format and want to query it. Now, I want to use Jena TDB, which can store the data that can be used for querying later. I checked the documentation for TDB Java API, but was unable to load the N-Triples file and then query with SPARQL. I've used the following code:

String directory = "E:\\Applications\\tdb-0.8.9\\TDB-0.8.9\\bin\\tdb";
        Dataset dataset = TDBFactory.createDataset(directory);

        // assume we want the default model, or we could get a named model here
        Model tdb = dataset.getDefaultModel();

        // read the input file - only needs to be done once
        String source = "E:\\Applications\\linkedmdb-18-05-2009-dump.nt";
        FileManager.get().readModel( tdb, source, "N-TRIPLES" );

and got the following Exception

Exception in thread "main" com.hp.hpl.jena.tdb.base.file.FileException: Not a directory: E:\Applications\tdb-0.8.9\TDB-0.8.9\bin\tdb
    at com.hp.hpl.jena.tdb.base.file.Location.<init>(Location.java:83)
    at com.hp.hpl.jena.tdb.TDBFactory.createDataset(TDBFactory.java:79)
    at tutorial.Temp.main(Temp.java:14)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤凫 2024-11-07 08:12:14

从 Java 读取 TDB 支持的模型非常简单,请参阅 TDB wiki 了解详细信息。例如,您可以:

// open TDB dataset
String directory = "./tdb";
Dataset dataset = TDBFactory.createDataset(directory);

// assume we want the default model, or we could get a named model here
Model tdb = dataset.getDefaultModel();

// read the input file - only needs to be done once
String source = "path/to/input.nt";
FileManager.get().readModel( tdb, source, "N-TRIPLES" );

// run a query
String q = "select * where {?s ?p ?o} limit 10";
Query query = QueryFactory.create(q);
QueryExecution qexec = QueryExecutionFactory.create(query, tdb);
ResultSet results = qexec.execSelect();
... etc ...

正如 user205512 提到的,您可以在 Linux 或 Mac 上从命令行使用 tdbloader2,这对于大型 RDF 文件会更快。创建 TDB 索引后,您可以将文件复制到其他计算机。因此,您可以在 Linux 服务器上加载数据,然后将 tdb 目录中的所有文件发送到 Windows 计算机以继续开发。

要在 Windows 计算机上从命令行运行 tdbloader,您需要类似 cygwin允许您运行 Unix 风格的脚本。您还需要设置环境变量TDBROOT

Reading into a TDB-backed Model from Java is straightforward, see the TDB wiki for details. For example, you could:

// open TDB dataset
String directory = "./tdb";
Dataset dataset = TDBFactory.createDataset(directory);

// assume we want the default model, or we could get a named model here
Model tdb = dataset.getDefaultModel();

// read the input file - only needs to be done once
String source = "path/to/input.nt";
FileManager.get().readModel( tdb, source, "N-TRIPLES" );

// run a query
String q = "select * where {?s ?p ?o} limit 10";
Query query = QueryFactory.create(q);
QueryExecution qexec = QueryExecutionFactory.create(query, tdb);
ResultSet results = qexec.execSelect();
... etc ...

As user205512 mentioned, you can use tdbloader2 from the command line on a Linux or Mac, which will be faster on large RDF files. Once the TDB indexes have been created, you can copy the files to other machines. So you can load the data on a Linux server, then ship all the files inside the tdb directory to your Windows machine to continue development.

To run tdbloader from the command line on your Windows machine, you'll need something like cygwin to allow you to run Unix-style scripts. You'll also need to set the environment variable TDBROOT.

陪你搞怪i 2024-11-07 08:12:14

您不需要任何 java 代码来执行此操作(tdbloader2 更快):

bin/tdbloader2 --loc /path/to/tdb/store imdb.nt

将加载 n-triple 文件。您可以使用以下方式查询:

bin/tdbquery --loc /path/to/tdb/store "select ...."

有关 tdb 命令行工具的更多信息此处

You don't need any java code to do this (tdbloader2 is faster):

bin/tdbloader2 --loc /path/to/tdb/store imdb.nt

will load in the n-triple file. You can query it using:

bin/tdbquery --loc /path/to/tdb/store "select ...."

More information on the tdb command line tools here.

蓝礼 2024-11-07 08:12:14

假设“nt格式”确实是“N-Triple”,那么Jena Model.read(is, base, lang) 如果 lang"N-Triple",方法将加载 N-Triple 格式。

有关更多详细信息,请参阅Jena 教程文档

Assuming that "nt format" is really "N-Triple", then the Jena Model.read(is, base, lang) method will load N-Triple format if lang is "N-Triple".

For more details, refer to the Jena tutorial document.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文