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


    }

}

最佳答案

就像定义前缀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 .
}

10-07 19:34
查看更多