我正在使用Jena编写一个SPARQL
查询,以从作为方法参数接收的URI中获取rdfs:label
属性。该方法只接收像http://pt.dbpedia.org/..
这样的URI。它应该向我返回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>
如果你想下载一点数据并对其进行查询,请注意
- http://pt.dbpedia.org/resource/Brasil重定向到
- http://pt.dbpedia.org/page/Brasil
后一个页面的底部有下载数据的链接,例如
- http://pt.dbpedia.org:8890/data/Brasil.rdf
如果您要下载该文件,那么您的查询可能会起作用(当然,uri
将不再相同)。
不过,您在我之前的回答中使用的查询是为DBpedia主端点设计的,而不是为葡萄牙语端点设计的。您可以从主数据库下载Brasil的数据,方法是:http://dbpedia.org/resource/Brazil并遵循如上所述的相同重定向和下载链接,但更好的选择是实际针对主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 |
---------------