【问题标题】:Querying DBpedia with SPARQL and Jena使用 SPARQL 和 Jena 查询 DBpedia
【发布时间】:2009-10-29 14:46:33
【问题描述】:

我不明白如何使用 Jena 查询 DBpedia。在here(Listing 4) 之类的教程中,模型初始化如下:

// 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 中,它看起来像(取自 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 中的外观如何?特别是,我对模型对象的初始化方式很感兴趣。

【问题讨论】:

  • 您想知道如何在 Jena 中运行 SPARQL 查询,还是想知道如何执行与该查询等效的查询?
  • 是的。实际上,我找到了我正在寻找的答案,但我很乐意看到您的答案。谢谢。

标签: sparql semantic-web jena dbpedia


【解决方案1】:

在浏览了大量页面后,我找到了答案。也许我没有足够清楚地问这个问题,但无论如何下面是对我有用的代码。

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();
}

我在dbpedia-discussion of www.mail-archive.com page找到的这个答案。

【讨论】:

  • 这个答案中的代码不太正确,并且包含一些细微的错误。像"SELECT DISTINCT ?m ?n ?p ?d"+ "WHERE {" 这样的字符串连接会导致“SELECT DISTINCT ... ?dWHERE {` 带有一个名为dWHERE 的变量(这仍然是合法的,因为SPARQL 中的WHERE 是可选的;即select ?x { } 和select ?x WHERE {} 相同)。
  • 在" FILTER ( lang(?n) = "fr" )"+ 中的语言标签中需要转义。但最重要的是,语言标签应与 langMatches 进行比较:filter langMatches(lang(?n),'fr').
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多