如何使用Jena TDB存储链接电影数据库的本地版本
我有一个本地版本的LinkedMDB,它是N-Triples格式,想要查询它。 现在,我想使用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.(Location.java:83) at com.hp.hpl.jena.tdb.TDBFactory.createDataset(TDBFactory.java:79) at tutorial.Temp.main(Temp.java:14)
从Java读取TDB支持的Model
很简单,有关详细信息,请参阅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
。
您不需要任何Java代码来执行此操作( tdbloader2
更快):
bin/tdbloader2 --loc /path/to/tdb/store imdb.nt
将加载在n-triple文件中。 您可以使用以下方式查询:
bin/tdbquery --loc /path/to/tdb/store "select ...."
有关tdb命令行工具的更多信息,请参见此处
假设“nt format”实际上是“N-Triple”,那么如果lang
是"N-Triple"
,那么Jena Model.read(is, base, lang)
方法将加载N-Triple格式。
有关更多详细信息,请参阅Jena教程文档 。