使用Jena TDB在本地加载DBpedia吗?


问题内容

我需要对DBpedia执行查询:

SELECT DISTINCT ?poi ?lat ?long ?photos ?template ?type ?label WHERE {
  ?poi  <http://www.w3.org/2000/01/rdf-schema#label> ?label .
  ?poi <http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?lat .
  ?poi <http://www.w3.org/2003/01/geo/wgs84_pos#long> ?long .
  ?poi <http://dbpedia.org/property/hasPhotoCollection> ?photos .                      
  OPTIONAL {?poi <http://dbpedia.org/property/wikiPageUsesTemplate> ?template } .
  OPTIONAL {?poi <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?type } .
  FILTER ( ?lat > x && ?lat < y &&
           ?long > z && ?long < ω && 
           langMatches( lang(?label), "EN" ))
}

我猜测此信息分散在不同的转储(.nt)文件中,并且SPARQL端点以某种方式为我们提供了结果集。我需要在本地下载这些不同的.nt文件(并非所有DBpedia),仅执行一次查询并将结果存储在本地(我不想使用SPARQL端点)。

  • 我应该在耶拿的哪一部分进行此运行?

对此帖子的阅读有些困惑:

因此,您可以将整个DBPedia数据加载到磁盘上的单个TDB位置(即单个目录)中。这样,您可以在其上运行SPARQL查询。

  • 如果得到三个.nt DBpedia文件,以耶拿的话,如何将DBpedia加载到单个TDB位置?我们如何将上述查询应用于这些.nt文件?(任何代码都会有所帮助。)

  • 例如,这是错误的吗?

     String tdbDirectory = "C:\\TDB";
     String dbdump1 = "C:\\Users\\dump1_en.nt";
     String dbdump2 = "C:\\Users\\dump2_en.nt";
     String dbdump3 = "C:\\Users\\dump3_en.nt";
     Dataset dataset = TDBFactory.createDataset(tdbDirectory);
     Model tdb = dataset.getDefaultModel(); //<-- What is the default model?Should I care?
     //Model tdb = TDBFactory.createModel(tdbdirectory) ;//<--is this prefered?
     FileManager.get().readModel( tdb, dbdump1, "N-TRIPLES" );
     FileManager.get().readModel( tdb, dbdump2, "N-TRIPLES" );
     FileManager.get().readModel( tdb, dbdump3, "N-TRIPLES" );
     String q = "my big fat query";
     Query query = QueryFactory.create(q);
            QueryExecution qexec = QueryExecutionFactory.create(query, tdb);
            ResultSet results = qexec.execSelect();
             while (results.hasNext()) {
             //do something significant with it
     }
    qexec.close()
    tdb.close() ;
    dataset.close();
  • 在上面的代码中,我们使用了"dataset.getDefaultModel"(获取默认图作为Jena Model)。这句话有效吗?我们是否需要创建一个数据集来执行查询,还是应该使用TDBFactory.createModel(tdbdirectory)

问题答案:

要让Jena在本地建立索引:

/** The Constant tdbDirectory. */
public static final String tdbDirectory = "C:\\TDBLoadGeoCoordinatesAndLabels";

/** The Constant dbdump0. */
public static final String dbdump0 = "C:\\Users\\Public\\Documents\\TDB\\dbpedia_3.8\\dbpedia_3.8.owl";

/** The Constant dbdump1. */
public static final String dbdump1 = "C:\\Users\\Public\\Documents\\TDB\\geo_coordinates_en\\geo_coordinates_en.nt";

 ...

Model tdbModel = TDBFactory.createModel(tdbDirectory);<\n>

/*Incrementally read data to the Model, once per run , RAM > 6 GB*/
FileManager.get().readModel( tdbModel, dbdump0);
FileManager.get().readModel( tdbModel, dbdump1, "N-TRIPLES");
FileManager.get().readModel( tdbModel, dbdump2, "N-TRIPLES");
FileManager.get().readModel( tdbModel, dbdump3, "N-TRIPLES");
FileManager.get().readModel( tdbModel, dbdump4, "N-TRIPLES");
FileManager.get().readModel( tdbModel, dbdump5, "N-TRIPLES");
FileManager.get().readModel( tdbModel, dbdump6, "N-TRIPLES");
tdbModel.close();

查询耶拿:

String queryStr = "dbpedia query ";

Dataset dataset = TDBFactory.createDataset(tdbDirectory);
Model tdb = dataset.getDefaultModel();

Query query = QueryFactory.create(queryStr);
QueryExecution qexec = QueryExecutionFactory.create(query, tdb);

/*Execute the Query*/
ResultSet results = qexec.execSelect();

while (results.hasNext()) {
    // Do something important
}

qexec.close();
tdb.close() ;