SPARQL 查询 returns 没有来自特定 RDF 图
SPARQL Query returns nothing from a specific RDF graph
在 python 中使用 RDFLib 创建 RDF 图以应用传感器 ontology(我为此使用了传感器 ontology,还使用了命名空间和 Bnode,这是一个空白节点表示未为其提供 URI 或文字的资源)。我尝试使用 sparql 查询 java 中的数据,因此我必须先使用 Jena TDB 存储图形,然后我执行了一个非常简单的查询,即:
String qs1 = "SELECT * {?s ?p ?o} LIMIT 10" ;
我用了
String source = "/path/graph.rdf";
FileManager.get().readModel( tdb, source);
dataset.begin(ReadWrite.READ) ;
String qs1 = "SELECT * {?s ?o ?p } " ;
try(QueryExecution qExec = QueryExecutionFactory.create(qs1, dataset)) {
ResultSet rs = qExec.execSelect() ;
ResultSetFormatter.outputAsJSON(rs) ;
}`
执行查询并观察json格式的数据。
我面临的问题是 returns 什么都没有!
这是输出:
{
"head": {
"vars": [ "s" , "o" , "p" ]
} ,
"results": {
"bindings": [
]
}
}
我做了一个简单的代码来验证是否存储了数据:
StmtIterator iter = tdb.listStatements();
// print out the predicate, subject and object of each statement
while (iter.hasNext()) {
Statement stmt = iter.nextStatement(); // get next statement
Resource subject = stmt.getSubject(); // get the subject
Property predicate = stmt.getPredicate(); // get the predicate
RDFNode object = stmt.getObject(); // get the object
System.out.print(subject.toString());
System.out.print(" " + predicate.toString() + " ");
if (object instanceof Resource) {
System.out.print(object.toString());
} else {
// object is a literal
System.out.print(" \"" + object.toString() + "\"");
}
System.out.println(" .");
}
而且它们确实存储在 TDB 数据库中。这是一些输出,其中包括 Bnode 的奇怪表示,根据一些文章,它是 TDB 与 Bnode 反应的方式,这使得它看起来像那样。
6f98bd70:1543430b66e:-7fc3 http://www.loa-cnr.it/ontologies/DUL.owl#hasDataValue "37^^file:///data/rbe/workspace/openmtc-python/openmtc-gevent/xsd.float" .
-6f98bd70:1543430b66e:-7fc2 http://purl.oclc.org/NET/UNIS/fiware/iot-lite#hasunit http://purl.oclc.org/NET/ssnx/qu/unit#hPa .
-6f98bd70:1543430b66e:-7fc2 http://www.loa-cnr.it/ontologies/DUL.owl#hasDataValue "996.94^^file:///data/rbe/workspace/openmtc-python/openmtc-gevent/xsd.float" .
-6f98bd70:1543430b66e:-7fc1 http://purl.oclc.org/NET/UNIS/fiware/iot-lite#hasunit http://purl.oclc.org/NET/ssnx/qu/unit# .
-6f98bd70:1543430b66e:-7fc1 http://www.loa-cnr.it/ontologies/DUL.owl#hasDataValue "OK^^file:///data/rbe/workspace/openmtc-python/openmtc-gevent/xsd.float" .
-6f98bd70:1543430b66e:-7fc0 http://purl.oclc.org/NET/UNIS/fiware/iot-lite#hasunit http://purl.oclc.org/NET/ssnx/qu/unit#C .
-6f98bd70:1543430b66e:-7fc0 http://www.loa-cnr.it/ontologies/DUL.owl#hasDataValue "24.2^^file:///data/rbe/workspace/openmtc-python/openmtc-gevent/xsd.float" .
我还尝试了另一个使用朋友的朋友的图表 ontology,它工作正常且正确。
是否有可能是 Bnode 导致了这个问题?
尝试:SELECT * { { ?s ?p ?o } UNION { GRAPH ?g { ?s ?p ?o } } }
您的评论表明数据在命名图表中,但您只询问了 unnamed/default 图表的查询。建议的查询可找到数据集中任何位置的所有内容。
正如@AndyS 所说,建议的查询工作正常。如果您不想使用 union 部分,只需按照 Andy 的建议添加您需要的图形名称即可。应该是这样的:
QueryExecution qExec = QueryExecutionFactory.create(qs1, YourGraphNameHERE));
ResultSet rs = qExec.execSelect() ;
ResultSetFormatter.outputAsJSON(rs) ;
在 python 中使用 RDFLib 创建 RDF 图以应用传感器 ontology(我为此使用了传感器 ontology,还使用了命名空间和 Bnode,这是一个空白节点表示未为其提供 URI 或文字的资源)。我尝试使用 sparql 查询 java 中的数据,因此我必须先使用 Jena TDB 存储图形,然后我执行了一个非常简单的查询,即:
String qs1 = "SELECT * {?s ?p ?o} LIMIT 10" ;
我用了
String source = "/path/graph.rdf";
FileManager.get().readModel( tdb, source);
dataset.begin(ReadWrite.READ) ;
String qs1 = "SELECT * {?s ?o ?p } " ;
try(QueryExecution qExec = QueryExecutionFactory.create(qs1, dataset)) {
ResultSet rs = qExec.execSelect() ;
ResultSetFormatter.outputAsJSON(rs) ;
}`
执行查询并观察json格式的数据。 我面临的问题是 returns 什么都没有! 这是输出:
{
"head": {
"vars": [ "s" , "o" , "p" ]
} ,
"results": {
"bindings": [
]
}
}
我做了一个简单的代码来验证是否存储了数据:
StmtIterator iter = tdb.listStatements();
// print out the predicate, subject and object of each statement
while (iter.hasNext()) {
Statement stmt = iter.nextStatement(); // get next statement
Resource subject = stmt.getSubject(); // get the subject
Property predicate = stmt.getPredicate(); // get the predicate
RDFNode object = stmt.getObject(); // get the object
System.out.print(subject.toString());
System.out.print(" " + predicate.toString() + " ");
if (object instanceof Resource) {
System.out.print(object.toString());
} else {
// object is a literal
System.out.print(" \"" + object.toString() + "\"");
}
System.out.println(" .");
}
而且它们确实存储在 TDB 数据库中。这是一些输出,其中包括 Bnode 的奇怪表示,根据一些文章,它是 TDB 与 Bnode 反应的方式,这使得它看起来像那样。
6f98bd70:1543430b66e:-7fc3 http://www.loa-cnr.it/ontologies/DUL.owl#hasDataValue "37^^file:///data/rbe/workspace/openmtc-python/openmtc-gevent/xsd.float" .
-6f98bd70:1543430b66e:-7fc2 http://purl.oclc.org/NET/UNIS/fiware/iot-lite#hasunit http://purl.oclc.org/NET/ssnx/qu/unit#hPa .
-6f98bd70:1543430b66e:-7fc2 http://www.loa-cnr.it/ontologies/DUL.owl#hasDataValue "996.94^^file:///data/rbe/workspace/openmtc-python/openmtc-gevent/xsd.float" .
-6f98bd70:1543430b66e:-7fc1 http://purl.oclc.org/NET/UNIS/fiware/iot-lite#hasunit http://purl.oclc.org/NET/ssnx/qu/unit# .
-6f98bd70:1543430b66e:-7fc1 http://www.loa-cnr.it/ontologies/DUL.owl#hasDataValue "OK^^file:///data/rbe/workspace/openmtc-python/openmtc-gevent/xsd.float" .
-6f98bd70:1543430b66e:-7fc0 http://purl.oclc.org/NET/UNIS/fiware/iot-lite#hasunit http://purl.oclc.org/NET/ssnx/qu/unit#C .
-6f98bd70:1543430b66e:-7fc0 http://www.loa-cnr.it/ontologies/DUL.owl#hasDataValue "24.2^^file:///data/rbe/workspace/openmtc-python/openmtc-gevent/xsd.float" .
我还尝试了另一个使用朋友的朋友的图表 ontology,它工作正常且正确。 是否有可能是 Bnode 导致了这个问题?
尝试:SELECT * { { ?s ?p ?o } UNION { GRAPH ?g { ?s ?p ?o } } }
您的评论表明数据在命名图表中,但您只询问了 unnamed/default 图表的查询。建议的查询可找到数据集中任何位置的所有内容。
正如@AndyS 所说,建议的查询工作正常。如果您不想使用 union 部分,只需按照 Andy 的建议添加您需要的图形名称即可。应该是这样的:
QueryExecution qExec = QueryExecutionFactory.create(qs1, YourGraphNameHERE));
ResultSet rs = qExec.execSelect() ;
ResultSetFormatter.outputAsJSON(rs) ;