Here is a code example that show how to query DBPedia SPARQL endpoint using Jena

String query=
"PREFIX p: "+
"PREFIX dbpedia: "+
"PREFIX category: "+
"PREFIX rdfs: "+
"PREFIX skos: "+
"PREFIX geo: "+

"SELECT DISTINCT ?m ?n ?p ?d"+
"WHERE {"+
" ?m rdfs:label ?n."+
" ?m skos:subject ?c."+
" ?c skos:broader category:Churches_in_Paris."+
" ?m p:abstract ?d."+
" ?m geo:point ?p"+
" FILTER ( lang(?n) = "fr" )"+
" FILTER ( lang(?d) = "fr" )"+
" }"

// now creating query object
Query query = QueryFactory.create(queryString);
// initializing queryExecution factory with remote service.
// **this actually was the main problem I couldn't figure out.**
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);

//after it goes standard query execution and result processing which can
// be found in almost any Jena/SPARQL tutorial.
try {
ResultSet results = qexec.execSelect();
for (; results.hasNext();) {

// Result processing is done here.
}
}
finally {
qexec.close();
}

Advertisements

One thought on “Querying DBPedia using Jena API

  1. Hello. When I apply the same procedure I get an error such as:

    Exception in thread “main” org.apache.http.conn.ssl.SSLInitializationException: Failure initializing default system SSL context

    Caused by: java.io.IOException: Keystore was tampered with, or password was incorrect

    Caused by: java.security.UnrecoverableKeyException: Password verification failed

    Do you have any suggestion?

    Thanks,

    My code is:

    package connectingurl;

    import com.hp.hpl.jena.query.*;

    public class DBpediaQuery {

    public static void main( String[] args ) {

    String s2 = “PREFIX yago: \n” +
    “PREFIX onto: \n” +
    “PREFIX rdf: \n” +
    “PREFIX dbpedia: \n” +
    “PREFIX owl: \n” +
    “PREFIX dbpedia-owl: \n” +
    “PREFIX rdfs: \n” +
    “PREFIX dbpprop: \n” +
    “PREFIX foaf: \n” +

    “SELECT DISTINCT *\n” +
    “WHERE {\n” +
    “?city rdf:type dbpedia-owl:PopulatedPlace .\n” +
    “?city rdfs:label ?label.\n” +
    “?city dbpedia-owl:country ?country .\n” +
    “?country dbpprop:commonName ?country_name.\n” +

    “OPTIONAL { ?city foaf:isPrimaryTopicOf ?web }.\n” +

    “FILTER ( lang(?label) = ‘en’ && regex(?country, ‘Germany’) && regex(?label, ‘Homburg’)) \n” +
    “} \n” +
    “”;

    Query query = QueryFactory.create(s2); //s2 = the query above
    QueryExecution qExe = QueryExecutionFactory.sparqlService( “http://dbpedia.org/sparql”, query );
    //QueryExecution qExe = QueryExecutionFactory.create( query );
    ResultSet results = qExe.execSelect();
    ResultSetFormatter.out(System.out, results, query) ;

    }

    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s