Writing SPARQL queries with Jena to query for IRIs like: http://pt.dbpedia.org/

我是研究僧i 提交于 2019-12-02 12:16:14
Joshua Taylor

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 |
---------------
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!