Jena TDB 中的 GRAPH 查询:CLI 工作/代码失败
GRAPH Queries in Jena TDB: CLI works / Code fails
我创建了一个 TDB 数据集并将数据加载到其中。我想执行以下查询:
prefix skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?s WHERE { GRAPH ?g { ?s skos:broader ?o }} LIMIT 100
在命令行中,此查询运行并return使用以下方法获得所需结果:
tdbquery --loc=<path_to_dataset> --file <path_to_query_file>
但是,我在 Java 代码中执行完全相同的查询时遇到问题:
String pathToRepo = "<path_to_dataset>";
// open the dataset
dataset = TDBFactory.createDataset(pathToRepo);
model = dataset.getDefaultModel();
String queryString = "PREFIX skos: <http://www.w3.org/2004/02/skos/core#>\n"
+ "SELECT ?s WHERE { GRAPH ?g { ?s skos:broader ?o } } LIMIT 100";
Query query = QueryFactory.create(queryString);
QueryExecution qe = QueryExecutionFactory.create(query, model);
ResultSet results = qe.execSelect();
while (results.hasNext()) {
QuerySolution result = results.next();
System.out.println(result.get("s").toString());
}
qe.close();
Java 代码运行但不会 return 任何结果。
为什么会这样?我需要更改什么?
我使用的数据是 public 你可以在这里找到它:http://webisa.webdatacommons.org/(向下滚动直到你在数据转储)。
我是 Jena TDB 的新手,所以我希望这个问题不要太愚蠢,也不要太难回答。感谢您的帮助!
正如AKSW正确指出的那样,可以通过查询数据集而不是默认模型来获得所需的结果,即
QueryExecution qe = QueryExecutionFactory.create(query, dataset);
我创建了一个 TDB 数据集并将数据加载到其中。我想执行以下查询:
prefix skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?s WHERE { GRAPH ?g { ?s skos:broader ?o }} LIMIT 100
在命令行中,此查询运行并return使用以下方法获得所需结果:
tdbquery --loc=<path_to_dataset> --file <path_to_query_file>
但是,我在 Java 代码中执行完全相同的查询时遇到问题:
String pathToRepo = "<path_to_dataset>";
// open the dataset
dataset = TDBFactory.createDataset(pathToRepo);
model = dataset.getDefaultModel();
String queryString = "PREFIX skos: <http://www.w3.org/2004/02/skos/core#>\n"
+ "SELECT ?s WHERE { GRAPH ?g { ?s skos:broader ?o } } LIMIT 100";
Query query = QueryFactory.create(queryString);
QueryExecution qe = QueryExecutionFactory.create(query, model);
ResultSet results = qe.execSelect();
while (results.hasNext()) {
QuerySolution result = results.next();
System.out.println(result.get("s").toString());
}
qe.close();
Java 代码运行但不会 return 任何结果。 为什么会这样?我需要更改什么?
我使用的数据是 public 你可以在这里找到它:http://webisa.webdatacommons.org/(向下滚动直到你在数据转储)。
我是 Jena TDB 的新手,所以我希望这个问题不要太愚蠢,也不要太难回答。感谢您的帮助!
正如AKSW正确指出的那样,可以通过查询数据集而不是默认模型来获得所需的结果,即
QueryExecution qe = QueryExecutionFactory.create(query, dataset);