使用SPARQL和Jena查询DBpedia [英] Querying DBpedia with SPARQL and Jena
问题描述
我不明白如何使用耶拿(Jena)查询DBpedia.在类似此处(清单4)的模型中初始化如下:
I cannot understand how can I query DBpedia using Jena. In the tutorials like here(Listing 4) model is initialized as follows:
// Open the bloggers RDF graph from the filesystem
InputStream in = new FileInputStream(new File("bloggers.rdf"));
// Create an empty in-memory model and populate it from the graph
Model model = ModelFactory.createMemModelMaker().createModel();
model.read(in,null); // null base URI, since model URIs are absolute
in.close();
假设我想编写一个查询,该查询将列出巴黎的教堂.在SPARQL中,它看起来像(取自此邮件列表消息):
Let's say I want to write a query that will list churches in Paris. In SPARQL it will look like (taken from this mailing list message):
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
FILTER ( lang(?n) = "fr" )
FILTER ( lang(?d) = "fr" )
}
此查询在Java中的外观如何?特别是,我对模型对象的初始化方式很感兴趣.
How will this query look in Java? Particularly, I'm interested in how the model object is initialized.
推荐答案
浏览了无数页面之后,我找到了答案.也许我没有清楚地问过这个问题,但是无论如何,下面是对我有用的代码.
After browsing tons and tons of pages I found the answer. Perhaps I didn't ask the question clearly enough, but anyway below is the code that worked for me.
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"+
" FILTER ( lang(?n) = "fr" )"+
" FILTER ( lang(?d) = "fr" )"+
" }"
// now creating query object
Query query = QueryFactory.create(queryString);
// initializing queryExecution factory with remote service.
// **this actually was the main problem I couldn't figure out.**
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);
//after it goes standard query execution and result processing which can
// be found in almost any Jena/SPARQL tutorial.
try {
ResultSet results = qexec.execSelect();
for (; results.hasNext();) {
// Result processing is done here.
}
}
finally {
qexec.close();
}
我在 www的dbpedia讨论中找到了这个答案.mail-archive.com页面.
这篇关于使用SPARQL和Jena查询DBpedia的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!