我有一个本地版本的LinkedMDB,它是N-Triples格式的,并且想要查询它。现在,我想使用Jena TDB,它可以存储稍后用于查询的数据。我检查了documentation for 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)发布于 2011-04-12 06:54:56
从Java读取TDB支持的Model非常简单,详细信息请参阅the 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。
发布于 2011-04-11 23:08:59
你不需要任何java代码来做这件事(tdbloader2更快):
bin/tdbloader2 --loc /path/to/tdb/store imdb.nt将加载到n-triple文件中。您可以使用以下命令进行查询:
bin/tdbquery --loc /path/to/tdb/store "select ...."有关tdb命令行工具here的更多信息。
发布于 2011-04-11 23:00:08
假设"nt格式“实际上是”N-三元组“,那么如果lang为"N-Triple",那么Jena Model.read(is, base, lang)方法将加载N-三元组格式。
有关更多详细信息,请参阅Jena tutorial document。
https://stackoverflow.com/questions/5622890
复制相似问题