Writing SPARQL queries with Jena to query for IRIs

2019-03-06 09:38发布

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!

1条回答
何必那么认真
2楼-- · 2019-03-06 10:26

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:

SELECT distinct ?label WHERE { 
  ?brasil owl:sameAs <http://pt.dbpedia.org/resource/Brasil> ;
          rdfs:label ?label .
  filter( langMatches(lang(?label),"pt") )
}

then your uri must have been http://pt.dbpedia.org/resource/Brasil, so you would have been (trying) to retrieve data with

Model model = ModelFactory.createDefaultModel().read( uri );

and 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:

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>

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.

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 |
---------------
查看更多
登录 后发表回答