【问题标题】:Cannot run particular SPARQL query using Apache Jena无法使用 Apache Jena 运行特定的 SPARQL 查询
【发布时间】:2017-04-07 16:29:14
【问题描述】:

我正在尝试使用 Apache Jena 运行以下查询

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX vrank:<http://purl.org/voc/vrank#>
PREFIX dbp-ont:<http://dbpedia.org/ontology/>
PREFIX dbp-prop:<http://dbpedia.org/property/>
SELECT distinct (SAMPLE(?slabel) AS ?sublabel) (SAMPLE (?plabel) AS ?predlabel) (SAMPLE(?olabel) AS ?oblabel) ?v 
FROM <http://dbpedia.org/> 
FROM <http://people.aifb.kit.edu/ath/#DBpedia_PageRank> 
WHERE {
    {   <http://dbpedia.org/resource/Yao_Ming> ?p ?o.
        FILTER regex(str(?o),"http://dbpedia.org/resource","i").
        FILTER (?p != dbp-ont:wikiPageWikiLink && ?p != <http://purl.org/dc/terms/subject> 
                && ?p != dbp-prop:wikiPageUsesTemplate && ?p != rdfs:seeAlso 
                && ?p != <http://www.w3.org/2002/07/owl#differentFrom> 
                && ?p != <http://dbpedia.org/ontology/wikiPageDisambiguates> && ?p != <http://dbpedia.org/ontology/wikiPageRedirects> ).
        OPTIONAL {?o rdfs:label ?olabel. FILTER langmatches( lang(?olabel), "EN" ). }.
        OPTIONAL {?p rdfs:label ?plabel. FILTER langmatches( lang(?plabel), "EN" ).}.
        OPTIONAL {<http://dbpedia.org/resource/Yao_Ming> rdfs:label ?slabel. FILTER langmatches( lang(?slabel), "EN" ).}.
        OPTIONAL {?o vrank:hasRank ?r. ?r vrank:rankValue ?v}.
    } 
UNION
    {   ?s ?p <http://dbpedia.org/resource/Yao_Ming>.
        FILTER regex(str(?s),"http://dbpedia.org/resource","i").
        FILTER (?p != dbp-ont:wikiPageWikiLink && ?p != <http://purl.org/dc/terms/subject> 
                && ?p != dbp-prop:wikiPageUsesTemplate && ?p != rdfs:seeAlso 
                && ?p != <http://www.w3.org/2002/07/owl#differentFrom> 
                && ?p != <http://dbpedia.org/ontology/wikiPageDisambiguates> && ?p != <http://dbpedia.org/ontology/wikiPageRedirects> ).
        OPTIONAL {?s rdfs:label ?slabel.   FILTER langmatches( lang(?slabel), "EN" ). }.
        OPTIONAL {?p rdfs:label ?plabel.  FILTER langmatches( lang(?plabel), "EN" ).}.
        OPTIONAL {<http://dbpedia.org/resource/Yao_Ming> rdfs:label ?olabel. FILTER langmatches( lang(?olabel), "EN" ).}.
        OPTIONAL {?s vrank:hasRank ?r. ?r vrank:rankValue ?v}.
    }
} group by ?v order by desc (?v)

此查询取自LinkSUM project。它在 dbpedia sparql 端点上运行良好(results),但 Jena 没有返回任何行。

这是代码

import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.query.ResultSetFactory;
import org.apache.jena.sparql.engine.http.QueryEngineHTTP;

        String query = "..."; // The previously mentioned query
        String DBPEDIA_SPARQL_SERVICE = "http://dbpedia.org/sparql/";
        QueryEngineHTTP qExec = (QueryEngineHTTP) QueryExecutionFactory.sparqlService(DBPEDIA_SPARQL_SERVICE , query);
        ResultSet resultSet = null;
        try {
            resultSet = qExec.execSelect();
            resultSet = ResultSetFactory.copyResults(resultSet);
        } catch (Exception e) {
            // Report exception
        } finally {
            qExec.close();
        }

        String subLabel = "sublabel";
        String predLabel = "predlabel";
        String obLabel = "oblabel";
        String vRank = "v";

        if (resultSet != null) {
            while (resultSet.hasNext()) {
                QuerySolution result = resultSet.next();
                if (result != null) {
                    System.out.println(subLabel);
                    System.out.println(predLabel);
                    System.out.println(obLabel);
                    System.out.println(vRank);
                }
            }
        }

我已经使用相同的代码运行了多个查询,但是这个没有返回任何结果。

【问题讨论】:

  • 在所有代码路径上打印出一些东西。可能是 tesultSet 为 null,因为“// 报告异常”。你也没有“?default-graph-uri=http%3A%2F%2Fdbpedia.org”。
  • 正如@AndyS 所说,对异常情况不做任何事情是不好的做法。并写qExec.addDefaultGraph("http://dbpedia.org");
  • 我实际上正在记录异常。我只是在这里排除了该代码。另外,我会尝试添加默认图表
  • 默认图表成功了! @AndyS 你应该让你的评论成为答案。我会接受的。

标签: java sparql jena apache-jena


【解决方案1】:

DBpedia 在服务 URL 中需要 ?default-graph-uri=http%3A%2F%2Fdbpedia.org 或有时需要 qExec.addDefaultGraph("http://dbpedia.org");。在查询中使用FROM 时似乎就是这种情况。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多