我尝试使用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);
}
}
}
我使用相同的代码运行了几个查询,但是这个查询无法返回任何结果。
答案 0 :(得分:1)
DBpedia在服务URL中需要?default-graph-uri=http%3A%2F%2Fdbpedia.org
,有时候需要qExec.addDefaultGraph("http://dbpedia.org");
。在查询中使用FROM
时,似乎就是这种情况。