基于使用SPARQL和DBPedia,我想检索位于巴黎的所有教堂的信息。
如果我运行下面的代码,它将返回null
。我无法弄清楚出了什么问题,因为没有错误信息。
package com.tests.example;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.RDFNode;
public class JenaTestDBPedia {
public static void main(String[] args) {
String queryString=
"PREFIX p: <http://dbpedia.org/property/>" +
"PREFIX dbpedia: <http://dbpedia.org/resource/>" +
"PREFIX category: <http://dbpedia.org/resource/Category:>" +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>" +
"PREFIX skos: <http://www.w3.org/2004/02/skos/core#>" +
"PREFIX geo: <http://www.georss.org/georss/>" +
"SELECT DISTINCT ?m ?n ?p ?d" +
"WHERE {" +
" ?m rdfs:label ?n." +
" ?m skos:subject ?c." +
" ?c skos:broader category:Churches_in_Paris." +
" ?m p:abstract ?d." +
" ?m geo:point ?p" +
" }";
Query query = QueryFactory.create(queryString);
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);
try {
ResultSet results = qexec.execSelect();
while(results.hasNext()) {
QuerySolution qs = results.next();
RDFNode type = qs.get("label");
System.out.println(type.toString());
}
}
finally {
qexec.close();
}
}
}
更新: 更正了查询(虽然它不起作用):
"SELECT DISTINCT ?m ?n ?p ?d \n" +
"WHERE { \n" +
" ?m rdfs:label ?n." +
" ?m skos:subject ?c." +
" ?c skos:broader category:Churches_in_Paris." +
" ?m p:abstract ?d." +
" ?m geo:point ?p" +
" }";
答案 0 :(得分:1)
我将专注于查询,而不是代码的其余部分:
skos:subject
,您需要dct:subject
p:abstract
,您需要dbo:abstract
更正的查询:
PREFIX category: <http://dbpedia.org/resource/Category:>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX geo: <http://www.georss.org/georss/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT DISTINCT ?m ?n ?p ?d WHERE {
?m rdfs:label ?n.
?m dct:subject ?c.
?c skos:broader category:Churches_in_Paris.
?m dbo:abstract ?d.
?m geo:point ?p
}
其他一些观点:
geo:point
OPTIONAL
,因为并非所有搜索结果都是答案 1 :(得分:0)
qs.get("label");
您没有选择名为label的变量。对于rdfs:label的值,您使用了属性?n
。您需要使用qs.get("n")
。
另外,请注意您的字符串连接有一些意想不到的效果,例如&#34;选择...?dWHERE {...&#34;,并且您的查询没有使用名为dWHERE的变量。