0

在使用 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是否可能导致此问题?

4

2 回答 2

2

尝试:SELECT * { { ?s ?p ?o } UNION { GRAPH ?g { ?s ?p ?o } } }

您的评论表明数据位于命名图中,但您只询问了未命名/默认图的查询。建议的查询在数据集中的任何位置查找所有内容。

于 2016-04-20T21:15:43.657 回答
0

正如@AndyS 所说,建议的查询工作正常。如果您不想使用联合部分,只需按照 Andy 的建议添加所需图形的名称即可。它应该是这样的:

QueryExecution qExec = QueryExecutionFactory.create(qs1, YourGraphNameHERE));
            ResultSet rs = qExec.execSelect() ;
             ResultSetFormatter.outputAsJSON(rs) ;
于 2016-04-22T18:26:40.560 回答