【问题标题】:Jena queryParseException耶拿 queryParseException
【发布时间】:2014-09-02 15:43:06
【问题描述】:

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


    }

}

【问题讨论】:

    标签: java eclipse sparql jena dbpedia


    【解决方案1】:

    就像你定义前缀PREFIX res: &lt;http://dbpedia.org/resource/&gt;一样,你需要为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 .
    }
    

    【讨论】:

    • @ineffablep 查询在 DBpedia 端点上起作用的原因是,为方便起见,端点网站预定义了一堆命名空间,因此您不必输入它们。您可以从通过Namespace Prefixes 链接访问端点。
    猜你喜欢
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-13
    相关资源
    最近更新 更多