在python中使用RDFLib创建RDF图以应用传感器本体之后(我曾用于该传感器本体,还使用了名称空间和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格式的数据。
我面临的问题是它什么也不返回!
这是输出:
{
"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" .
我还尝试了另一个使用朋友本体的朋友的图,它可以正常工作。
Bnode是否可能导致此问题?
最佳答案
尝试:SELECT * { { ?s ?p ?o } UNION { GRAPH ?g { ?s ?p ?o } } }
您的注释表明数据在命名图中,但是您仅查询了未命名/默认图。建议的查询可以找到数据集中任何地方的所有内容。