Jena TDB使用API​​存储和查询 [英] Jena TDB to store and query using API

查看:166
本文介绍了Jena TDB使用API​​存储和查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Jena-TDB和SPARQL的新手,所以这可能是一个愚蠢的问题。我在Windows XP上使用tdb-0.9.0。

I am new to both Jena-TDB and SPARQL, so it might be a silly question. I am using tdb-0.9.0, on Windows XP.

我正在为我的 trail_1.rdf 文件。我在这里的理解(如果我错了,请纠正我)是以下代码将读取TDB模型中给定的rdf文件,并且还存储/加载(不确定什么是更好的单词)给定目录中的模型 D:\Project\Store_DB\data1\tdb

I am creating the TDB model for my trail_1.rdf file. My understanding here(correct me if I am wrong) is that the following code will read the given rdf file in TDB model and also stores/load (not sure what's the better word) the model in the given directory D:\Project\Store_DB\data1\tdb:

// open TDB dataset
String directory = "D:\\Project\\Store_DB\\data1\\tdb";
Dataset dataset = TDBFactory.createDataset(directory);

Model tdb = dataset.getDefaultModel();

// read the input file
String source = "D:\\Project\\Store_DB\\tmp\\trail_1.rdf";
FileManager.get().readModel( tdb, source);

tdb.close();
dataset.close();

这种理解是否正确?

根据我的理解,从现在起模型存储在 D:\Project \Store_DB\data1 \tdb 目录中,我应该可以在稍后的某个时间点对它运行查询。

As per my understanding since now the model is stored at D:\Project\Store_DB\data1\tdb directory, I should be able to run query on it at some later point of time.

所以在 D:\ Project注册TDB商店\\Store_DB\data1\tdb 我试过以下,但它什么都不打印:

So to query the TDB Store at D:\Project\Store_DB\data1\tdb I tried following, but it prints nothing:

String directory = "D:\\Project\\Store_DB\\data1\\tdb" ;
Dataset dataset = TDBFactory.createDataset(directory) ;

Iterator<String> graphNames = dataset.listNames();
while (graphNames.hasNext()) {
    String graphName = graphNames.next();
    System.out.println(graphName);
}






我也试过这个,它也没有打印任何东西:


I also tried this, which also did not print anything:

    String directory = "D:\\Project\\Store_DB\\data1\\tdb" ;
    Dataset dataset = TDBFactory.createDataset(directory) ;

    String sparqlQueryString = "SELECT (count(*) AS ?count) { ?s ?p ?o }" ;

    Query query = QueryFactory.create(sparqlQueryString) ;
    QueryExecution qexec = QueryExecutionFactory.create(query, dataset) ;
    ResultSet results = qexec.execSelect() ;
    ResultSetFormatter.out(results) ;

我做错了什么?我上面提到的理解有什么不妥吗?

What am I doing incorrect? Is there anything wrong with my understanding that I have mentioned above?

推荐答案

对于你的问题的第(i)部分,是的,你的理解是正确的。

For part (i) of your question, yes, your understanding is correct.

对于第(ii)部分, listNames 的原因不会返回任何结果是因为你没有将您的数据放入命名图中。特别是,

For part (ii), the reason that listNames does not return any results is because you have not put your data into a named graph. In particular,

Model tdb = dataset.getDefaultModel();

表示您将数据存储到TDB的默认图表中,即没有名称的图表。如果您希望 listNames 返回一些内容,请将该行更改为:

means that you are storing data into TDB's default graph, i.e. the graph with no name. If you wish listNames to return something, change that line to:

Model tdb = dataset.getNamedGraph( "graph42" );

或类似的东西。当然,当您查询数据时,您需要按名称引用该图表。

or something similar. You will, of course, then need to refer to that graph by name when you query the data.

如果您的目标只是测试您是否已成功加载数据进入商店,尝试命令行工具 bin / tdbdump (Linux)或 bat\tdbdump.bat ( Windows)。

If your goal is simply to test whether or not you have successfully loaded data into the store, try the command line tools bin/tdbdump (Linux) or bat\tdbdump.bat (Windows).

对于第(iii)部分,我在我的系统上尝试了你的代码,指向我的一个TDB图像,它就像人们期望的那样工作。所以:你正在使用的TDB图像中没有任何数据(使用tdbdump测试),或者你实际运行的代码与上面的示例不同。

For part (iii), I tried your code on my system, pointing at one of my TDB images, and it works just as one would expect. So: either the TDB image you're using doesn't have any data in it (test with tdbdump), or the code you actually ran was different to the sample above.

这篇关于Jena TDB使用API​​存储和查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆