微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用SPARQL和Jena查询DBpedia

我不明白如何使用Jena查询DBpedia.在像 here(清单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中如何看?特别是,我对模型对象的初始化感兴趣.

解决方法

浏览吨和吨的页面后,我找到了答案.也许我没有清楚地提出这个问题,但下面是对我有用的代码.
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发现.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Mip是什么意思以及作用有哪些
怎么测试Mip页面运行情况
MIP安装的具体步骤有哪些
HTML添加超链接、锚点的方法及作用详解(附视频)
MIP的规则有哪些
Mip轮播图组件中的重要属性讲解
Mip的内联框架组件是什么
怎么创建初始的MIP配置及模板文件
HTML实现多选框及无法提交多数据的原因分析(附视频)
HTML如何设置复选框、单选框以及默认选项?(图文+视频)