简体   繁体   English

SPARQL查询从特定的RDF图不返回任何内容

[英]SPARQL Query returns nothing from a specific RDF graph

After creating an RDF graph using RDFLib in python to apply a sensor ontology ( I used for that a sensor ontology, used also namespace and Bnode which is a blank node representing a resource for which a URI or literal is not given). 在python中使用RDFLib创建RDF图以应用传感器本体之后(我曾经使用过传感器本体,还使用了名称空间和Bnode,后者是一个空白节点,代表未提供URI或文字的资源)。 I tried to query the data in java using sparql therefore I had to store the graph using Jena TDB first then I executed a really simple query which is : 我尝试使用sparql在Java中查询数据,因此必须先使用Jena TDB存储图形,然后执行一个非常简单的查询,即:

String qs1 = "SELECT * {?s ?p ?o} LIMIT 10" ;

and I used 我用了

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) ;
     }` 

to execute the query and observe the data in json format. 执行查询并观察json格式的数据。 The problem I m facing is that it returns nothing! 我面临的问题是它什么也不返回! this is the output: 这是输出:

{
  "head": {
    "vars": [ "s" , "o" , "p" ]
  } ,
  "results": {
    "bindings": [

    ]
  }
}

I made a simple code to verify if the data are stored : 我编写了一个简单的代码来验证是否存储了数据:

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(" .");
        } 

and indeed they are stored on the TDB database. 并且确实将它们存储在TDB数据库中。 This is some of the output, which include a bizarres representation of the Bnode and according to some articles its the way the TDB react with Bnode which makes it looks like that. 这是其中的一些输出,其中包括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" .

I also tried another graph which uses the friend of a friend ontology and it works fine and correctly. 我还尝试了另一个使用朋友本体的朋友的图,它可以正常工作。 is it possible that the Bnode is causing this issue ? Bnode是否可能导致此问题?

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

Your comments suggest the data is in a named graph but you asked the query of the unnamed/default graph only. 您的注释表明数据在命名图中,但是您仅查询了未命名/默认图。 The query suggested finds everything, anywhere in the dataset. 建议的查询可以找到数据集中任何地方的所有内容。

As @AndyS montionned the query suggested works fine. 在@AndyS指示下,建议的查询工作正常。 In case you don't want to use the union part just do as Andy suggested by adding the name of the graph you need. 如果您不想使用并集部分,只需按照安迪建议的方法添加所需图形的名称即可。 It should be like this : 应该是这样的:

QueryExecution qExec = QueryExecutionFactory.create(qs1, YourGraphNameHERE));
            ResultSet rs = qExec.execSelect() ;
             ResultSetFormatter.outputAsJSON(rs) ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM