Search code examples
javardfsparqldbpedia

Query SPARQL to DBPedia using Java code


I would like to get the URIs of the page on DBPedia that have a label equal to "London". That is, when I query DBPedia, if a page the property rdfs:label with the value "London", then I want to get its URI, e.g., http://dbpedia.org/resource/London. I'm using the following Java code, but I get no results. What am I doing wrong here?

String strings = "London";
String service = "http://dbpedia.org/sparql";
String query = "PREFIX dbo:<http://dbpedia.org/ontology/>"
        + "PREFIX : <http://dbpedia.org/resource/>"
        + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#/>"
        + "select ?URI where {?URI rdfs:label "+strings+".}";
QueryExecution qe=QueryExecutionFactory.sparqlService(service, query);
ResultSet rs = qe.execSelect();
while (rs.hasNext()){
    QuerySolution s= rs.nextSolution();
    System.out.println(s.getResource("?URI").toString());
}

Solution

  • If you take a query like this to the DBpedia endpoint, you'll get a parse error:

    select ?resource where {
      ?resource rdfs:label London
    }
    
    Virtuoso 37000 Error SP030: SPARQL compiler, line 4: syntax error at 'London' before '}'
    
    SPARQL query:
    define sql:big-data-const 0 
    #output-format:text/html
    define sql:signal-void-variables 1 define input:default-graph-uri <http://dbpedia.org> select ?resource where {
      ?resource rdfs:label London
    }
    

    You need to put your strings inside single or double quotes. So you'd have a query like this, which parses correctly, but still has no results:

    select ?resource where {
      ?resource rdfs:label "London"
    }
    

    SPARQL results

    Much of the time, when you're working with DBpedia, it will help a lot to browse the data by hand to find out what's there, and then base your queries on that knowledge. In this case, you're interested in http://dbpedia.org/page/London. If you put that in a web browser, you'll see a bunch of the information London. If you scroll to the bottom, you can click on the N3/Turtle to see the same information in the Turtle serialization, which is very close to the SPARQL syntax. If you search in that page for rdfs:label, you'll see:

    dbpedia:London  rdfs:label  "Londres"@fr ,
            "London"@en ,
            "London"@sv ,
            "Londra"@it ,
            "\u30ED\u30F3\u30C9\u30F3"@ja ,
            "\u4F26\u6566"@zh ,
            "Londres"@es ,
            "Londyn"@pl ,
            "Londen"@nl ,
            "Londres"@pt ,
            "\u041B\u043E\u043D\u0434\u043E\u043D"@ru ,
            "London"@de ;
    

    Most (perhaps all) of the labels in DBpedia have language tags, so you actually need to search for things that have the label "London"@en. For instance, you can use this query to get thes results:

    select ?resource where {
      ?resource rdfs:label "London"@en
    }
    

    SPARQL results

    Most of your code for the query is right, but you just need to be using the proper syntax for literals, and need to know what the data in DBpedia looks like. This is also a good case for parameterized SPARQL strings. There's an example in get latitude and longitude of a place dbpedia, but the short idea is that you should be able to write

    select ?resource where {
      ?resource rdfs:label ?label
    }
    

    as a ParmeterizedSparqlString, and then use an API method to replace ?label with the literal that you want (in this case, "London"@en) without having to do any string concatenation. The ParameterizedSparqlString will also ensure that all the quoting is done correctly, and that the literal is formatted in the right way. Here's an example:

    import com.hp.hpl.jena.query.ParameterizedSparqlString;
    import com.hp.hpl.jena.query.QueryExecution;
    import com.hp.hpl.jena.query.QueryExecutionFactory;
    import com.hp.hpl.jena.query.ResultSet;
    import com.hp.hpl.jena.query.ResultSetFactory;
    import com.hp.hpl.jena.query.ResultSetFormatter;
    import com.hp.hpl.jena.rdf.model.Literal;
    import com.hp.hpl.jena.rdf.model.ResourceFactory;
    
    public class LondonExample {
        public static void main(String[] args) {
            ParameterizedSparqlString qs = new ParameterizedSparqlString( "" +
                    "prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#>\n" +
                    "\n" +
                    "select ?resource where {\n" +
                    "  ?resource rdfs:label ?label\n" +
                    "}" );
    
            Literal london = ResourceFactory.createLangLiteral( "London", "en" );
            qs.setParam( "label", london );
    
            System.out.println( qs );
    
            QueryExecution exec = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", qs.asQuery() );
    
            // Normally you'd just do results = exec.execSelect(), but I want to 
            // use this ResultSet twice, so I'm making a copy of it.  
            ResultSet results = ResultSetFactory.copyResults( exec.execSelect() );
    
            while ( results.hasNext() ) {
                // As RobV pointed out, don't use the `?` in the variable
                // name here. Use *just* the name of the variable.
                System.out.println( results.next().get( "resource" ));
            }
    
            // A simpler way of printing the results.
            ResultSetFormatter.out( results );
        }
    }
    
    prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#>
    
    select ?resource where {
      ?resource rdfs:label "London"@en
    }
    http://dbpedia.org/resource/London
    http://dbpedia.org/resource/Category:London
    http://www.ontologyportal.org/SUMO#LondonUnitedKingdom
    http://sw.opencyc.org/2008/06/10/concept/en/London
    http://sw.opencyc.org/2008/06/10/concept/Mx8Ngx4rvWhwppwpEbGdrcN5Y29ycA-GTG9uZG9uHiu9WLbVnCkRsZ2tw3ljb3Jw
    http://wikidata.dbpedia.org/resource/Q1442133
    http://wikidata.dbpedia.org/resource/Q261303
    http://wikidata.dbpedia.org/resource/Q79348
    http://wikidata.dbpedia.org/resource/Q92561
    http://wikidata.dbpedia.org/resource/Q3258936
    http://wikidata.dbpedia.org/resource/Q84
    http://wikidata.dbpedia.org/resource/Q6669759
    http://wikidata.dbpedia.org/resource/Q6669762
    http://wikidata.dbpedia.org/resource/Q6669763
    http://wikidata.dbpedia.org/resource/Q6669771
    http://wikidata.dbpedia.org/resource/Q586353
    http://wikidata.dbpedia.org/resource/Q1310705
    http://wikidata.dbpedia.org/resource/Q1749384
    http://wikidata.dbpedia.org/resource/Q3836562
    http://wikidata.dbpedia.org/resource/Q3836563
    http://wikidata.dbpedia.org/resource/Q3836565
    http://wikidata.dbpedia.org/resource/Q1001456
    http://wikidata.dbpedia.org/resource/Q5712562
    http://wikidata.dbpedia.org/resource/Q3061911
    http://wikidata.dbpedia.org/resource/Q6669774
    http://wikidata.dbpedia.org/resource/Q6669754
    http://wikidata.dbpedia.org/resource/Q6669757
    http://wikidata.dbpedia.org/resource/Q6669761
    http://wikidata.dbpedia.org/resource/Q6669767
    http://wikidata.dbpedia.org/resource/Q6669769
    http://wikidata.dbpedia.org/resource/Q2477346
    ---------------------------------------------------------------------------------------------------------------
    | resource                                                                                                    |
    ===============================================================================================================
    | <http://dbpedia.org/resource/London>                                                                        |
    | <http://dbpedia.org/resource/Category:London>                                                               |
    | <http://www.ontologyportal.org/SUMO#LondonUnitedKingdom>                                                    |
    | <http://sw.opencyc.org/2008/06/10/concept/en/London>                                                        |
    | <http://sw.opencyc.org/2008/06/10/concept/Mx8Ngx4rvWhwppwpEbGdrcN5Y29ycA-GTG9uZG9uHiu9WLbVnCkRsZ2tw3ljb3Jw> |
    | <http://wikidata.dbpedia.org/resource/Q1442133>                                                             |
    | <http://wikidata.dbpedia.org/resource/Q261303>                                                              |
    | <http://wikidata.dbpedia.org/resource/Q79348>                                                               |
    | <http://wikidata.dbpedia.org/resource/Q92561>                                                               |
    | <http://wikidata.dbpedia.org/resource/Q3258936>                                                             |
    | <http://wikidata.dbpedia.org/resource/Q84>                                                                  |
    | <http://wikidata.dbpedia.org/resource/Q6669759>                                                             |
    | <http://wikidata.dbpedia.org/resource/Q6669762>                                                             |
    | <http://wikidata.dbpedia.org/resource/Q6669763>                                                             |
    | <http://wikidata.dbpedia.org/resource/Q6669771>                                                             |
    | <http://wikidata.dbpedia.org/resource/Q586353>                                                              |
    | <http://wikidata.dbpedia.org/resource/Q1310705>                                                             |
    | <http://wikidata.dbpedia.org/resource/Q1749384>                                                             |
    | <http://wikidata.dbpedia.org/resource/Q3836562>                                                             |
    | <http://wikidata.dbpedia.org/resource/Q3836563>                                                             |
    | <http://wikidata.dbpedia.org/resource/Q3836565>                                                             |
    | <http://wikidata.dbpedia.org/resource/Q1001456>                                                             |
    | <http://wikidata.dbpedia.org/resource/Q5712562>                                                             |
    | <http://wikidata.dbpedia.org/resource/Q3061911>                                                             |
    | <http://wikidata.dbpedia.org/resource/Q6669774>                                                             |
    | <http://wikidata.dbpedia.org/resource/Q6669754>                                                             |
    | <http://wikidata.dbpedia.org/resource/Q6669757>                                                             |
    | <http://wikidata.dbpedia.org/resource/Q6669761>                                                             |
    | <http://wikidata.dbpedia.org/resource/Q6669767>                                                             |
    | <http://wikidata.dbpedia.org/resource/Q6669769>                                                             |
    | <http://wikidata.dbpedia.org/resource/Q2477346>                                                             |
    ---------------------------------------------------------------------------------------------------------------