Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SPARQL query using Jena producing no results — but works online

Basically I have the following query, and it works in all the online SPARQL testers without a problem, but when using Java and Jena 2.6.4, I never get any results. I've written the values into the query for demonstration purposes.

PREFIX  g:    <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  onto: <http://dbpedia.org/ontology/>

SELECT  ?subject ?stadium ?lat ?long
WHERE
  { ?subject g:lat ?lat .
    ?subject g:long ?long .
    ?subject <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> onto:Stadium .
    ?subject rdfs:label ?stadium
    FILTER ( ( ( ( ( ?lat >= 52.4814 ) && ( ?lat <= 57.4814 ) ) && ( ?long >= -1.89358 ) ) && ( ?long <= 3.10642 ) ) && ( lang(?stadium) = "en" ) )
  }
LIMIT   5

Some Java, note I've tried accessing this a few different ways, however I'm using SPARQL throughout the project and have no problems.

Query query = QueryFactory.create(s2); //s2 = the query above
QueryExecution qExe = QueryExecutionFactory.create(query, model);
ResultSet resultsRes = qExe.execSelect();

try {
  while (resultsRes.hasNext()) {                
    QuerySolution soln = resultsRes.nextSolution();
    //never any results
  }
} catch (Exception ex) {
  System.out.println(ex);
}
like image 441
Ash Avatar asked May 16 '12 21:05

Ash


1 Answers

Unless you are loading the entire DBpedia dataset into a local model, to get the same effect as running the query in the DBpedia SPARQL form you will have to send your query to the DBpedia SPARQL end point. I ran the following modification of your program:

package example;

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

public class AshTest
{
    public static void main( String[] args ) {
        String s2 = "PREFIX  g:    <http://www.w3.org/2003/01/geo/wgs84_pos#>\n" +
                "PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
                "PREFIX  onto: <http://dbpedia.org/ontology/>\n" +
                "\n" +
                "SELECT  ?subject ?stadium ?lat ?long\n" +
                "WHERE\n" +
                "  { ?subject g:lat ?lat .\n" +
                "    ?subject g:long ?long .\n" +
                "    ?subject <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> onto:Stadium .\n" +
                "    ?subject rdfs:label ?stadium\n" +
                "    FILTER ( ( ( ( ( ?lat >= 52.4814 ) && ( ?lat <= 57.4814 ) ) && ( ?long >= -1.89358 ) ) && ( ?long <= 3.10642 ) ) && ( lang(?stadium) = \"en\" ) )\n" +
                "  }\n" +
                "LIMIT   5\n" +
                "";

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

and got the following result:

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| subject                                                       | stadium                              | lat                                                 | long                                                  |
======================================================================================================================================================================================================================
| <http://dbpedia.org/resource/Welford_Road_Stadium>            | "Welford Road Stadium"@en            | "52.6242"^^<http://www.w3.org/2001/XMLSchema#float> | "-1.13306"^^<http://www.w3.org/2001/XMLSchema#float>  |
| <http://dbpedia.org/resource/Hillsborough_Stadium>            | "Hillsborough Stadium"@en            | "53.4114"^^<http://www.w3.org/2001/XMLSchema#float> | "-1.50056"^^<http://www.w3.org/2001/XMLSchema#float>  |
| <http://dbpedia.org/resource/Gateshead_International_Stadium> | "Gateshead International Stadium"@en | "54.9611"^^<http://www.w3.org/2001/XMLSchema#float> | "-1.57972"^^<http://www.w3.org/2001/XMLSchema#float>  |
| <http://dbpedia.org/resource/Filbert_Street>                  | "Filbert Street"@en                  | "52.6236"^^<http://www.w3.org/2001/XMLSchema#float> | "-1.14056"^^<http://www.w3.org/2001/XMLSchema#float>  |
| <http://dbpedia.org/resource/Craven_Park,_Hull>               | "Craven Park, Hull"@en               | "53.7539"^^<http://www.w3.org/2001/XMLSchema#float> | "-0.264722"^^<http://www.w3.org/2001/XMLSchema#float> |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
like image 65
Ian Dickinson Avatar answered Oct 01 '22 03:10

Ian Dickinson