使用Apache Jena查询DBPedia时出错 [英] Error while querying DBPedia using Apache Jena

查看:110
本文介绍了使用Apache Jena查询DBPedia时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.sql.ResultSet;
import java.sql.SQLException;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSetFormatter;

public class sparq {
    public static void main (String[] args) throws SQLException
{

    String queryString=
            "PREFIX dbpedia: <http://dbpedia.org/resource/>"+
            "PREFIX category: <http://dbpedia.org/resource/Category:>"+
            "PREFIX skos: <http://www.w3.org/2004/02/skos/core#>"+
            "PREFIX dcterms: <http://purl.org/dc/terms/>"+
            "select distinct ?super where {"+
                  "?super (^skos:broader){0,4} category:Nationalist_parties, category:New_Delhi"+
                "}";



            // now creating query object

            com.hp.hpl.jena.query.Query query = QueryFactory.create(queryString);
            // initializing queryExecution factory with remote service.
            // **this actually was the main problem I couldn't figure out.**
            QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);


            //after it goes standard query execution and result processing which can
            // be found in almost any Jena/SPARQL tutorial.
            try {
                com.hp.hpl.jena.query.ResultSet results = qexec.execSelect();
                while(results.hasNext())
                {
                    QuerySolution s=results.nextSolution();
                    String strg=s.getResource("?x").toString();
                    System.out.println(strg);
                }
                //ResultSetFormatter.out(System.out, results, query);  
            }
            finally {
               qexec.close();
            }

}
}

使用Eclipse-Apache Jena执行上述查询时,出现以下错误.但是,如果在dbpedia sparql virtuso的Web服务页面上触发了相同的查询,则会得到所需的结果.

While Executing the above query using Eclipse-Apache Jena,I get the following ERROR.But if the same query is fired on webservice page of dbpedia sparql virtuso, It gives a desired result.

log4j:WARN No appenders could be found for logger (org.apache.jena.riot.system.stream.JenaIOEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " "{" "{ "" at line 1, column 249.
Was expecting one of:
    <IRIref> ...
    <PNAME_NS> ...
    <PNAME_LN> ...
    <BLANK_NODE_LABEL> ...
    <VAR1> ...
    <VAR2> ...
    "true" ...
    "false" ...
    <INTEGER> ...
    <DECIMAL> ...
    <DOUBLE> ...
    <INTEGER_POSITIVE> ...
    <DECIMAL_POSITIVE> ...
    <DOUBLE_POSITIVE> ...
    <INTEGER_NEGATIVE> ...
    <DECIMAL_NEGATIVE> ...
    <DOUBLE_NEGATIVE> ...
    <STRING_LITERAL1> ...
    <STRING_LITERAL2> ...
    <STRING_LITERAL_LONG1> ...
    <STRING_LITERAL_LONG2> ...
    "(" ...
    <NIL> ...
    "[" ...
    <ANON> ...
    "+" ...
    "*" ...
    "/" ...
    "|" ...
    "?" ...

    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 sparq.main(sparq.java:49)

但是,如果在Web上使用DBPedia Virtuso sparql运行相同的查询,则会给出结果. 我是Apche jena的新手,请帮助!!!!

But If the same query runs on using DBPedia Virtuso sparql on web.It gives the result. I am new to Apche jena Please Help!!!!

推荐答案

Virtuoso接受非标准语法. {n,m} 语法是关于属性路径的较早提议之一,但并未被SPARQL 1.1标准接受.结果,您的查询实际上不是合法的SPARQL 1.1(或SPARQL 1.0).您可以使用 sparql.org的查询验证器来检查您的查询是否合法.

Virtuoso accepts non standard syntax. The {n,m} syntax was in one of the earlier proposals for property paths, but it did not get accepted into the SPARQL 1.1 standard. As a result, your query isn't actually legal SPARQL 1.1 (or SPARQL 1.0). You can check whether your query is legal with sparql.org's query validator.

我认为您也可以使用Jena的API要求它先验证查询,在这种情况下,您将能够发送查询.参见例如此评论:

I think you can also use Jena's API to ask it to not validate the query first, in which case you'll be able to send your query. See, for instance, this comment:

如果您使用耶拿(Jena)发送具有Virtuoso特定功能的查询, 您需要直接创建一个QueryEngineHTTP(这是一个 QueryExecution),并仅提供2个字符串(端点和查询) 细绳.否则,Jena会在本地验证查询,但该查询无效 SPARQL因此失败. — AndyS

If you are using Jena to send a query with Virtuoso-specific features, you need to direct create a QueryEngineHTTP (which is a QueryExecution) and provide just the 2 strings, endpoint and query string. Otherwise, Jena validates the query locally but it isn't valid SPARQL hence it fails. — AndyS Sep 24 '14 at 10:48

或此answer.semanticweb.com问题:耶拿在正确但非标准的SPARQL上抛出QueryParsingException .在该问题中,OP通过使用以下代码直接创建QueryEngineHTTP来找到解决方案:

or this answers.semanticweb.com question: jena throws QueryParsingException on correct but non-standard SPARQL. In that question, OP found a solution by creating a QueryEngineHTTP directly, with code like this:

QueryEngineHTTP qe = new QueryEngineHTTP("http://dbpedia.org/sparq","select ...");
ResultSet rs = qe.execSelect();

这篇关于使用Apache Jena查询DBPedia时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆