I'm using Jena to write a SPARQL
query to get the rdfs:label
property from a URI received as a method parameter. That method only receives URIs like: http://pt.dbpedia.org/..
It should return me the rdfs:label
, but it doesn't return me anything. I checked and it doesn't enter the while block
supposed to iterate the results. I even made a test with the URI: <http://pt.dbpedia.org/resource/Brasil>
, but it didn't work.
What may be the problem?
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;
}
The SPARQL
query is like that:
SELECT distinct ?label WHERE {
?brasil owl:sameAs <http://pt.dbpedia.org/resource/Brasil> ;
rdfs:label ?label .
filter( langMatches(lang(?label),"pt") )
}
Thank you!
I understand that this is a continuation of your earlier question, Should queries with URIs like http://pt.dbpedia.org/resource/.. be different from the ones with URIs like http://dbpedia.org/resource/..?. If you're getting the query:
then your
uri
must have beenhttp://pt.dbpedia.org/resource/Brasil
, so you would have been (trying) to retrieve data withand then you're trying to run a SPARQL query against the local data that you've downloaded. As I mentioned in the previous (linked) question, the queries that I provided were meant to be run across the SPARQL endpoints; they weren't based on downloading the data and querying locally.
Trying to download the data locally like this doesn't work, as the following program and its output show:
If you want to download a little bit of data and query against it, then note that
and that latter page has links at the bottom to download the data, e.g.,
If you were to download that file, then your query might work (but of course the
uri
would no longer be the same).The query you're using from my earlier answer, though, was designed for the main DBpedia endpoint, not the Portuguese endpoint. You might be able to download the data for Brasil from the main DBpedia by going to http://dbpedia.org/resource/Brazil and following the same redirect and download link as described above, but a better choice would be to actually run the query against the main DBpedia endpoint, http://dbpedia.org/sparql, as shown in the following code and its results.