Jena TDB每次都会将所有数据加载到内存中吗?

时间:2017-02-16 01:15:58

标签: sparql jena tdb

我是耶拿的新手。我尝试使用TDB处理瑜伽数据集。数据集大约为200M,每次运行相同的查询时,都需要大约5分钟来加载数据,然后给出结果。我想知道我是否误解了TDB的任何部分?以下是我的代码。

String directory = "tdb";
Dataset dataset = TDBFactory.createDataset(directory);      
dataset.begin(ReadWrite.WRITE);
Model tdb = dataset.getDefaultModel();
//String source = "yagoMetaFacts.ttl";
//FileManager.get().readModel(tdb, source);
String queryString = "SELECT DISTINCT ?p WHERE { ?s ?p ?o. }";
Query query = QueryFactory.create(queryString);
try(QueryExecution qexec = QueryExecutionFactory.create(query, tdb)){
    ResultSet results = qexec.execSelect();
    ResultSetFormatter.out(System.out, results, query) ;
}
dataset.commit();    
dataset.end();

1 个答案:

答案 0 :(得分:2)

有两种方法可以通过API或CMD将数据加载到tdb中。非常感谢@ASKW和@AndyS

1通过API

加载数据

这些代码只需执行一次,尤其是readModel行,这需要很长时间。

String directory = "tdb";
Dataset dataset = TDBFactory.createDataset(directory);      
dataset.begin(ReadWrite.WRITE);
Model tdb = dataset.getDefaultModel();
String source = "yagoMetaFacts.ttl";
FileManager.get().readModel(tdb, source);
dataset.commit(); //Important!! This is to commit the data to tdb.   
dataset.end();

将数据加载到tdb后,我们可以使用以下代码进行查询。并且不必再次加载数据。

String directory = "path\\to\\tdb"; 
Dataset dataset = TDBFactory.createDataset(directory);
Model tdb = dataset.getDefaultModel(); 
String queryString = "SELECT DISTINCT ?p WHERE { ?s ?p ?o. }"; 
Query query = QueryFactory.create(queryString);
try(QueryExecution qexec = QueryExecutionFactory.create(query, tdb)){
     ResultSet results = qexec.execSelect();
     ResultSetFormatter.out(System.out, results, query) ;
}

2通过CMD

加载数据

加载数据

>tdbloader --loc=path\to\tdb path\to\dataset.ttl

查询

>tdbquery --loc=path\to\tdb --query=q1.rq

q1.rq是存储查询的文件 应该得到这样的结果

-------------------------------------------------------
| p                                                   |
=======================================================
| <http://yago-knowledge.org/resource/hasGloss>       |
| <http://yago-knowledge.org/resource/occursSince>    |
| <http://yago-knowledge.org/resource/occursUntil>    |
| <http://yago-knowledge.org/resource/byTransport>    |
| <http://yago-knowledge.org/resource/hasPredecessor> |
| <http://yago-knowledge.org/resource/hasSuccessor>   |
| <http://www.w3.org/2000/01/rdf-schema#comment>      |
-------------------------------------------------------