![](/img/trans.png)
[英]Should queries with URIs like http://pt.dbpedia.org/resource/.. be different from the ones with URIs like http://dbpedia.org/resource/..?
[英]Writing SPARQL queries with Jena to query for IRIs like: http://pt.dbpedia.org/
我正在使用Jena编写SPARQL
查询,以从作为方法参数接收的URI中获取rdfs:label
属性。 该方法仅接收URI,例如: http://pt.dbpedia.org/..
: http://pt.dbpedia.org/..
它应该返回rdfs:label
,但不会返回任何东西。 我检查了一下,它没有进入应该迭代结果的while block
。 我什至对URI进行了测试: <http://pt.dbpedia.org/resource/Brasil>
,但是没有用。
可能是什么问题?
public String getLabel(String uri, String label) {
Model model = ModelFactory.createDefaultModel().read( uri );
RDFNode node;
String queryString = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " +
"PREFIX owl: <http://www.w3.org/2002/07/owl#>" +
"SELECT distinct ?label WHERE { " +
"?resource owl:sameAs <" + uri + "> ;" +
"rdfs:label ?label ." +
"filter( langMatches(lang(?label),'pt')) }";
Query query = QueryFactory.create(queryString);
QueryExecution qe = QueryExecutionFactory.create(query, model);
ResultSet r = qe.execSelect();
while( r.hasNext() ) {
QuerySolution querySolution = r.next();
node = querySolution.get("label");
label = node.toString();
}
return label;
}
SPARQL
查询是这样的:
SELECT distinct ?label WHERE {
?brasil owl:sameAs <http://pt.dbpedia.org/resource/Brasil> ;
rdfs:label ?label .
filter( langMatches(lang(?label),"pt") )
}
谢谢!
我了解这是您先前问题的延续,即使用URI的查询(例如http://pt.dbpedia.org/resource/ ..)应不同于使用URI的查询(例如http://dbpedia.org/resource/)。 。? 。 如果您要查询:
SELECT distinct ?label WHERE {
?brasil owl:sameAs <http://pt.dbpedia.org/resource/Brasil> ;
rdfs:label ?label .
filter( langMatches(lang(?label),"pt") )
}
那么您的uri
必须是http://pt.dbpedia.org/resource/Brasil
,因此您将一直在(尝试)使用
Model model = ModelFactory.createDefaultModel().read( uri );
然后尝试对下载的本地数据运行SPARQL查询。 正如我在上一个(链接的)问题中提到的那样,我提供的查询旨在跨SPARQL端点运行; 他们不是基于下载数据和本地查询。
像这样尝试在本地下载数据不起作用,因为以下程序及其输出显示:
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
public class BrasilExample {
public static void main(String[] args) {
final Model model = ModelFactory.createDefaultModel().read( "http://pt.dbpedia.org/resource/Brasil" );
model.write( System.out );
}
}
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" >
</rdf:RDF>
如果您想下载一点数据并进行查询,请注意
后一页的底部有链接可以下载数据,例如,
如果要下载该文件,则您的查询可能会起作用(但是uri
当然不再相同)。
不过,您从我先前的答案中使用的查询是针对主要DBpedia端点而不是葡萄牙语端点而设计的。 您可以访问http://dbpedia.org/resource/Brazil,并按照上述相同的重定向和下载链接,从主DBpedia下载Brasil的数据,但更好的选择是实际运行针对主要DBpedia端点http://dbpedia.org/sparql进行查询,如以下代码及其结果所示。
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
public class BrasilExample {
public static void main(String[] args) {
final String QUERY =
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
"PREFIX owl: <http://www.w3.org/2002/07/owl#>\n" +
"\n" +
"SELECT distinct ?label WHERE {\n" +
" ?brasil owl:sameAs <http://pt.dbpedia.org/resource/Brasil> ;\n" +
" rdfs:label ?label .\n" +
" filter( langMatches(lang(?label),\"pt\") )\n" +
"}";
final String ENDPOINT = "http://dbpedia.org/sparql";
final ResultSet rs = QueryExecutionFactory.sparqlService( ENDPOINT, QUERY ).execSelect();
ResultSetFormatter.out( rs );
}
}
---------------
| label |
===============
| "Brasil"@pt |
| "Brazil"@pt |
---------------
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.