【问题标题】:Apache Jena - got 3 identical results for one queryApache Jena - 一个查询得到 3 个相同的结果
【发布时间】:2014-12-16 12:44:45
【问题描述】:

这是我在 StackOverflow 上的第一个问题。 我正在使用 Apache Jena 来查询 DBPedia,我得到的结果很奇怪。 这是我的代码,带有一个简单的查询:

    String sparqlQuery = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> select ?o where { <http://dbpedia.org/ontology/Agent> rdfs:subClassOf  ?o}";
    System.out.println("Query : " + sparqlQuery);
    Query query = QueryFactory.create(sparqlQuery);
    QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query); 
    ResultSet results = qexec.execSelect();
    ResultSetFormatter.out(System.out, results, query);
    qexec.close();

这就是我得到的回应:

<http://www.w3.org/2002/07/owl#Thing> <http://www.w3.org/2002/07/owl#Thing> <http://www.w3.org/2002/07/owl#Thing>

知道为什么我没有得到一个资源吗?我尝试了其他资源,同样的问题。

感谢您的帮助,祝您有美好的一天!

【问题讨论】:

  • 可能它不能证明 3 个实例的相等性或其他属性。

标签: java sparql jena


【解决方案1】:

查询被发送到 dbpedia,所以它给出了三个答案。 Jena 仅格式化结果。

这可能是因为在不同的命名图中有 3 个三元组 - 默认的 dbpedia 图是所有命名图的并集。

试试:

select *{ GRAPH ?g { <http://dbpedia.org/ontology/Agent> rdfs:subClassOf  ?o} }

同时检查返回的结果:使用 wget 或 curl 发出查询并查看返回的字节。

(您显示的响应与 ResultSetFormatter 输出不对应)

【讨论】:

    【解决方案2】:

    这很奇怪,当我在http://dbpedia.org/sparql 上测试您的查询时,我只有一个资源。

    你能检查一下你的ResultSet 中有三个结果吗?

    int count = 0;
    while( results.hasNext() ){
      results.next();
      count++;
    }
    System.out.println("I see "+count+" rows !");
    

    作为解决方法,您可以使用 DISTINCT 关键字:

    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
    SELECT DISTINCT ?o
    WHERE { 
      <http://dbpedia.org/ontology/Agent> rdfs:subClassOf  ?o
    }
    

    【讨论】:

    • 是的,我做了同样的测试,也得到了一个资源。 “我看到 3 行!”,所以这不是显示问题;)我想我会使用 DISTINCT,如果有人有解释,请告诉我!谢谢尼齐尔。
    猜你喜欢
    • 2021-11-13
    • 2020-02-12
    • 1970-01-01
    • 2014-08-18
    • 2022-11-10
    • 1970-01-01
    • 2013-08-30
    • 2023-04-02
    • 1970-01-01
    相关资源
    最近更新 更多