Sparql查询不返回uri / object

时间:2014-10-11 17:07:00

标签: java rdf sparql endpoint dbpedia

所以,我正在做一个简单的查询。我想要某个dbpedia uri的所有uri / property / object,例如:http://dbpedia.org/resource/Roger_Federer。问题是,它只返回它的属性,正如我们在结果集上看到的那样。

类别:

public class SemanticCrawlerImpl implements SemanticCrawler {   
    public void search(Model graph, String resourceURI) {
        graph.read(resourceURI);

        // Create a new query
        String queryString = 
            "SELECT ?url ?property ?object" +
            "WHERE {" +
            "      <"+resourceURI+"> ?property ?object ." + 
            "}";

        Query query = QueryFactory.create(queryString);

        System.out.println("----------------------");

        System.out.println("Query Result Sheet");

        System.out.println("----------------------");

        // Execute the query and obtain results
        QueryExecution qe = QueryExecutionFactory.create(query, graph);
        com.hp.hpl.jena.query.ResultSet results =  qe.execSelect();

        // Output query results    
        ResultSetFormatter.out(System.out, results, query);

        qe.close();
    }
}

结果集

| url | property                                                  | objectWHERE |
=================================================================================
|     | <http://dbpedia.org/property/medaltemplatesTitle>         |             |
|     | <http://dbpedia.org/ontology/thumbnail>                   |             |
|     | <http://www.w3.org/2002/07/owl#sameAs>                    |             |
|     | <http://www.w3.org/2002/07/owl#sameAs>                    |             |
|     | <http://www.w3.org/2002/07/owl#sameAs>                    |             |
|     | <http://www.w3.org/2002/07/owl#sameAs>                    |             |
|     | <http://dbpedia.org/property/name>                        |             |
|     | <http://dbpedia.org/property/daviscupresult>              |             |
|     | <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>         |             |
|     | <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>         |             |
|     | <http://dbpedia.org/property/hopmancupresult>             |             |
|     | <http://dbpedia.org/ontology/wikiPageExternalLink>        |             |
|     | <http://purl.org/dc/terms/subject>                        |             |
|     | <http://www.w3.org/2002/07/owl#sameAs>                    |             |
|     | <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>         |             |
|     | <http://purl.org/dc/terms/subject>                        |             |
|     | <http://www.w3.org/2002/07/owl#sameAs>                    |             |
|     | <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>         |             |
|     | <http://dbpedia.org/ontology/height>                      |             |
|     | <http://purl.org/dc/terms/subject>                        |             |
|     | <http://www.w3.org/2002/07/owl#sameAs>                    |             |
|     | <http://dbpedia.org/ontology/birthYear>                   |             |

1 个答案:

答案 0 :(得分:2)

您的查询中存在拼写错误,缺少空格。请注意,输出应该是&#34; object&#34;,而不是&#34; objectWHERE&#34;。此外,您不需要?url变量,因为您没有在查询中使用它。在代码中替换此片段,它将起作用:

 // Create a new query
        String queryString = 
            "SELECT ?property ?object " +
            "WHERE {" +
            "      <"+resourceURI+"> ?property ?object ." + 
            "}";

        Query query = QueryFactory.create(queryString);

另外,我不明白为什么你首先在本地读取资源然后针对本地图发出sparql查询。为什么不直接针对dbpedia端点呢? 像这样:

String resourceURI = "http://dbpedia.org/resource/Roger_Federer";
String queryString = 
            "SELECT ?property ?object " +
            "WHERE {" +
            "      <"+resourceURI+"> ?property ?object ." + 
            "}";
Query query = QueryFactory.create(queryString);
//System.out.println(queryIn);
QueryExecution qe = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);
ResultSet results = qe.execSelect(); 
ResultSetFormatter.out(System.out, results, query);
qe.close();

我认为它更清洁。