Jena queryParseException

时间:2014-09-02 15:43:07

标签: java eclipse sparql jena dbpedia

我是Jena和SPAQL的新手,尝试使用以下代码在eclipse中运行jena,获得Query Parse Exception。此查询在http://dbpedia.org/sparql

上正常执行

我想要的是出生地

异常

com.hp.hpl.jena.query.QueryParseException:第1行第84列:未解析的前缀名称:dbpedia-owl:birthPlace

查询

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

Java代码

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();
        }


    }

}

1 个答案:

答案 0 :(得分:4)

就像定义前缀PREFIX res: <http://dbpedia.org/resource/>一样,您需要为dbpedia-owl指定前缀。使用DBPedia's Predefined Namespace Prefixes,我假设更新的查询将如下所示:

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