Search code examples
javasparqljenadbpedia

Querying the DBPedia ontology using sparql, java, Jena API


I am trying to query the DBPedia using sparql from a javacode For some query it works perfectly anf for another one it doesn't work. I don't think that I have an error in my query because I already tested it in the DBPedia aparql endpoint. Here is my java code:

package ja1;

import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.util.*;

    public class Q_DBP_Online {

        public static void main(String[]args)
            {
                sparqlTest();
            }

    public static void sparqlTest()
       {             
         /*String queryString = "SELECT ?o WHERE {"+
                                "?s ?p ?o ."+
                                "} LIMIT 10";*/
         String str="Obama";
         String queryString = "PREFIX pr:<http://xmlns.com/foaf/0.1/>" +
                              "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+ 
                              "SELECT DISTINCT ?s ?label WHERE {" +                                        
                              "?s rdfs:label ?label . "+
                              "?s a pr:Person."+
                              "FILTER (lang(?label) = 'en'). "+
                              "?label bif:contains"+str+" ."+
                              "}";

         Query query = QueryFactory.create(queryString);        
         QueryExecution qexec =         QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);
         try
          {
            ResultSet results = qexec.execSelect();
            while(results.hasNext()){
           QuerySolution soln = results.nextSolution();
           //Literal name = soln.getLiteral("x");
           System.out.println(soln);
                }
             }
         finally{
              qexec.close();
          }

    }
}

So the first query which is commented runs perfectly and the second one doesn't run and I get this message in netbeans:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/rodwan/Desktop/Th_Pr/apache-jena-2.12.1/lib/slf4j-log4j12-1.7.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/rodwan/Desktop/Th_Pr/pellet-2.3.1/lib/jena/slf4j-log4j12-1.6.4.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]

Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " "(" "( "" at line 1, column 169.
Was expecting one of:
    "values" ...
    "graph" ...
    "optional" ...
    "minus" ...
    "bind" ...
    "service" ...
    "filter" ...
    "{" ...
    "}" ...
    ";" ...
    "," ...
    "." ...

    at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.perform(ParserSPARQL11.java:102)
    at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.parse$(ParserSPARQL11.java:53)
    at com.hp.hpl.jena.sparql.lang.SPARQLParser.parse(SPARQLParser.java:37)
    at com.hp.hpl.jena.query.QueryFactory.parse(QueryFactory.java:148)
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:80)
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:53)
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:41)
    at ja1.Q_DBP_Online.sparqlTest(Q_DBP_Online.java:38)
    at ja1.Q_DBP_Online.main(Q_DBP_Online.java:18)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

----------------------------------

Solution

  • I have tried your example, but it seems there are some missing spaces. The following was working for me on DBPedia:

    PREFIX pr:<http://xmlns.com/foaf/0.1/>
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    SELECT DISTINCT ?s ?label WHERE {?s rdfs:label ?label . ?s a pr:Person . FILTER (lang(?label) = 'en') . ?label bif:contains "Obama" .}
    

    The translation to Java would look like this:

    String queryString = "PREFIX pr:<http://xmlns.com/foaf/0.1/>\n" +
                                  "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"+
                                  "SELECT DISTINCT ?s ?label WHERE {" +                              "?s rdfs:label ?label . "+
                                  "?s a pr:Person . "+
                                  "FILTER (lang(?label) = 'en') . "+
                                  "?label bif:contains \""+str+"\" ."+
                                  "}";
    

    Hope this helps.