在Java代码中调用SPARQL查询(在DBPedia上)时出现HttpException错误

我使用Java代码的SPARQL端点有问题。

特别是,我有这个Java类:

public class example { public static void main(String[] args) { String value = "http://dbpedia.org/resource/Fred_Guy"; example exam = example(); QueryExecution qe = exam.query(value); ResultSet results = ResultSetFactory.copyResults( qe.execSelect() ); } public QueryExecution query(String stringa){ ParameterizedSparqlString qs = new ParameterizedSparqlString( "" + "prefix dbpediaont: \n" + "prefix rdf: \n" + "\n" + "select ?resource where {\n" + "?mat rdf:type ?resource\n" + "filter strstarts(str(?resource), dbpediaont:)\n" + "}" ); Resource risorsa = ResourceFactory.createResource(stringa); qs.setParam( "mat", risorsa ); QueryExecution exec = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", qs.asQuery() ); ResultSet results = ResultSetFactory.copyResults( exec.execSelect() ); while ( results.hasNext() ) { System.out.println( results.next().get( "resource" )); } // A simpler way of printing the results. ResultSetFormatter.out( results ); return exec; } } 

我想检索其谓词“RDF:type”的资源“ http://dbpedia.org/resource/Fred_Guy ”的对象。 但我有这个错误,我不明白:

 Exception in thread "main" HttpException: 500 at com.hp.hpl.jena.sparql.engine.http.HttpQuery.execGet(HttpQuery.java:340) at com.hp.hpl.jena.sparql.engine.http.HttpQuery.exec(HttpQuery.java:276) at com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP.execSelect(QueryEngineHTTP.java:345) at MyPackage.example.main(example.java:19) 

为什么我会收到此错误?

我正在尝试执行此查询

“ http://dbpedia.org/sparql ”

没有写strstarts我得到这个错误:

 Virtuoso 37000 Error SP031: SPARQL compiler: No one quad map pattern is suitable for GRAPH  { "http://dbpedia.org/resource/Fred_Guy"  ?resource } triple at line 7 SPARQL query: define sql:big-data-const 0 #output-format:text/html define sql:signal-void-variables 1 define input:default-graph-uri  prefix dbpediaont:  prefix rdf:  select ?resource where { "http://dbpedia.org/resource/Fred_Guy" rdf:type ?resource } 

我在这做错了什么?

我试着在Virtuoso中编写这段代码:

 prefix dbpediaont:  prefix rdf:  select ?resource where { dbpedia:Fred_Guy rdf:type ?resource } 

SPARQL结果

我如何用Jena代码编写它?

你在这里有两个问题。 在问题的最后,您使用的查询没有filter,但是与您的代码中嵌入的查询不同。 如果您使用DBpedia端点上的代码中嵌入的查询,您会收到一条非常明确的错误消息:

 Virtuoso 22023 Error SL001: The SPARQL 1.1 function STRSTARTS() needs a string value as 2d argument SPARQL query: define sql:big-data-const 0 #output-format:text/html define sql:signal-void-variables 1 define input:default-graph-uri  prefix dbpediaont:  prefix rdf:  select ?resource where { ?mat rdf:type ?resource filter strstarts(str(?resource), dbpediaont:) } 

关键是

SPARQL 1.1函数STRSTARTS()需要一个字符串值作为2d参数

您需要使用str()编写dbpediaont:因为它是IRI,而不是字符串:

 filter strstarts(str(?resource), str(dbpediaont:)) 
Interesting Posts