使用 Apache Jena 查询 DBPedia 时出错
Error while querying DBPedia using Apache Jena
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 的网络服务页面上触发了相同的查询,我会得到以下结果ERROR.But,它给出了所需的结果。
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.It 上使用 DBPedia Virtuso sparql 运行相同的查询,则会给出结果。
我是 Apche jena 的新手 请帮忙!!!!
Virtuoso 接受非标准语法。 {n,m} 语法是 属性 路径的早期提案之一,但它没有被 SPARQL 1.1 标准接受。因此,您的查询实际上不是合法的 SPARQL 1.1(或 SPARQL 1.0)。您可以使用 sparql.org's query validator.
检查您的查询是否合法
我认为您也可以使用 Jena 的 API 要求它 不 首先验证查询,在这种情况下您将能够发送您的查询。例如,参见 this comment:
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
或这个 answers.semanticweb.com 问题:jena throws QueryParsingException on correct but non-standard SPARQL。在那个问题中,OP 通过直接创建一个 QueryEngineHTTP 找到了解决方案,代码如下:
QueryEngineHTTP qe = new QueryEngineHTTP("http://dbpedia.org/sparq","select ...");
ResultSet rs = qe.execSelect();
我找到了问题的解决方法
void std_query()
{
String query = "select distinct ?super where {?super (^skos:broader){0,3} category:Nationalist_parties, category:New_Delhi}";
QueryEngineHTTP qe = new QueryEngineHTTP("http://dbpedia.org/sparql", query);
try {
com.hp.hpl.jena.query.ResultSet results = qe.execSelect();
while(results.hasNext())
{
QuerySolution s=results.nextSolution();
String strg=s.getResource("?super").toString();
System.out.println("########### Standard Sparql Result #########");
System.out.println(strg);
}
//ResultSetFormatter.out(System.out, results, query);
}
finally {
qe.close();
}
}
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 的网络服务页面上触发了相同的查询,我会得到以下结果ERROR.But,它给出了所需的结果。
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.It 上使用 DBPedia Virtuso sparql 运行相同的查询,则会给出结果。 我是 Apche jena 的新手 请帮忙!!!!
Virtuoso 接受非标准语法。 {n,m} 语法是 属性 路径的早期提案之一,但它没有被 SPARQL 1.1 标准接受。因此,您的查询实际上不是合法的 SPARQL 1.1(或 SPARQL 1.0)。您可以使用 sparql.org's query validator.
检查您的查询是否合法我认为您也可以使用 Jena 的 API 要求它 不 首先验证查询,在这种情况下您将能够发送您的查询。例如,参见 this comment:
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
或这个 answers.semanticweb.com 问题:jena throws QueryParsingException on correct but non-standard SPARQL。在那个问题中,OP 通过直接创建一个 QueryEngineHTTP 找到了解决方案,代码如下:
QueryEngineHTTP qe = new QueryEngineHTTP("http://dbpedia.org/sparq","select ...");
ResultSet rs = qe.execSelect();
我找到了问题的解决方法
void std_query()
{
String query = "select distinct ?super where {?super (^skos:broader){0,3} category:Nationalist_parties, category:New_Delhi}";
QueryEngineHTTP qe = new QueryEngineHTTP("http://dbpedia.org/sparql", query);
try {
com.hp.hpl.jena.query.ResultSet results = qe.execSelect();
while(results.hasNext())
{
QuerySolution s=results.nextSolution();
String strg=s.getResource("?super").toString();
System.out.println("########### Standard Sparql Result #########");
System.out.println(strg);
}
//ResultSetFormatter.out(System.out, results, query);
}
finally {
qe.close();
}
}