繁体   English   中英

无法使用Apache Jena运行特定的SPARQL查询

[英]Cannot run particular SPARQL query using Apache Jena

我正在尝试使用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项目 它在dbpedia sparql端点上运行正常( 结果 ),但是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);
                }
            }
        }

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

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

暂无
暂无

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

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