当我在 DBPedia 上执行查询时,我遇到了 SPARQL 的问题。
我有这个Java类:
public class example {
public static void main(String[] args) {
String value = "http://dbpedia.org/resource/McLeod's_Daughters_(season_8)";
String object = "tsmgo";
example le = new example();
QueryExecution qe = le.queryColumn(object, value);
ResultSet results = ResultSetFactory.copyResults( qe.execSelect() );
}
public QueryExecution queryColumn(String object, String string) {
ParameterizedSparqlString qs = new ParameterizedSparqlString( "" +
"prefix dbpediaont: <http://dbpedia.org/ontology/>\n" +
"prefix dbpedia: <http://dbpedia.org/resource/>\n" +
"prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
"prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
"\n" +
"select ?ob where {\n" +
"?subj rdfs:label ?ob\n" +
"FILTER (contains(?ob, ?obj) )\n" +
"}" );
Resource risorsa = ResourceFactory.createResource(string);
qs.setParam( "subj", risorsa );
Literal obj2 = ResourceFactory.createPlainLiteral(object);
qs.setParam( "obj", obj2 );
System.out.println( qs );
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( "ob" ));
}
// A simpler way of printing the results.
ResultSetFormatter.out( results );
return exec;
}
}
当我执行此代码时,我收到此错误:
Exception in thread "main" HttpException: 502
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.queryColumn(example.java:176)
at MyPackage.example.main(example.java:28)
此错误与帖子响应中报告的错误不同
我还尝试使用主题“ http://dbpedia.org/resource/Adriano_Celentano ”执行相同的查询,并且查询正确执行。特别是,我得到了一些查询的结果,所以并不是所有的查询都被拒绝。这种行为可能是由 DBPedia 的一些缓存机制给出的。为什么会出现这个错误?我在这里做错了什么?