简体   繁体   中英

Jena queryParseException

I am new to Jena and SPAQL , trying to run jena in eclipse with below code , getting Query Parse Exception. This Query is executing fine on http://dbpedia.org/sparql

What I want is Birth Place

Exception

com.hp.hpl.jena.query.QueryParseException: Line 1, column 84: Unresolved prefixed name: dbpedia-owl:birthPlace

Query

PREFIX res: <http://dbpedia.org/resource/>
SELECT DISTINCT ?string 
WHERE {
  res:David_Cameron dbpedia-owl:birthPlace ?string .
}

Java Code

import org.apache.jena.atlas.logging.Log;

import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.sparql.engine.http.QueryExceptionHTTP;

public class GetDateOfBirth {
    private String service = null;

    public GetDateOfBirth(String service)
    {
        this.service = service;     
    }

    public void TestConnection(){
        QueryExecution qe = QueryExecutionFactory.sparqlService(service, "ASK {}");
        try{
            if(qe.execAsk())
            {
                System.out.println(service + " is UP");
            }
        }catch(QueryExceptionHTTP e){
            e.printStackTrace();
            System.out.println(service + "is Down");
        }
        finally {
            qe.close();
        }
    }
    public ResultSet executeQuery(String queryString) throws Exception {
        QueryExecution qe = QueryExecutionFactory.sparqlService(service, queryString);
        return qe.execSelect();         
    }

    public static void main(String[] args) {
        Log.setCmdLogging() ;
        String sparqlService = "http://dbpedia.org/sparql";

        /*
         * More query examples here:
         * http://sparql.bioontology.org/examples
         */
        String query = "PREFIX res: <http://dbpedia.org/resource/>" +
                      " SELECT ?dob  WHERE {  res:David_Cameron dbpedia-owl:birthPlace ?string .}";


        try {
            GetDateOfBirth con= new GetDateOfBirth(sparqlService);
            ResultSet results = con.executeQuery(query);
            for ( ; results.hasNext() ; ) {
                QuerySolution soln = results.nextSolution() ;
                System.out.println(soln.getResource("?dob").toString());
            }
        } catch (Exception e) {
                    e.printStackTrace();
        }


    }

}

Just like you define the prefix PREFIX res: <http://dbpedia.org/resource/> , you need to specify a prefix for dbpedia-owl . Using DBPedia's Predefined Namespace Prefixes , I assume the updated query would look like this:

PREFIX res: <http://dbpedia.org/resource/>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
SELECT DISTINCT ?string 
WHERE {
  res:David_Cameron dbpedia-owl:birthPlace ?string .
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM