我正在尝试使用Jena API从DBpedia端点获取给定类的所有实例。我注意到结果集限制为10000个项目。因此,对于那些具有超过10000的类,我编写了以下代码。但是,此代码引发异常( HttpException:500 HTTP 500错误,导致查询:SPARQL请求失败)。当类的实例少于40000个时(例如City),代码可以正常运行。对于那些拥有超过40000个类的类(例如person),将抛出执行。有人可以帮我解决这个问题吗?
例外
HttpException: 500 HTTP 500 error making the query: SPARQL Request Failed
at org.apache.jena.sparql.engine.http.HttpQuery.rewrap(HttpQuery.java:370)
at org.apache.jena.sparql.engine.http.HttpQuery.execGet(HttpQuery.java:336)
at org.apache.jena.sparql.engine.http.HttpQuery.exec(HttpQuery.java:288)
at org.apache.jena.sparql.engine.http.QueryEngineHTTP.execResultSetInner(QueryEngineHTTP.java:351)
at org.apache.jena.sparql.engine.http.QueryEngineHTTP.execSelect(QueryEngineHTTP.java:344)
at org.jgnn.testes.dbpedia.DBpediaSPARQLDemo.getNumberOfInstancesByClassAndEndpoint(DBpediaSPARQLDemo.java:1637)
at org.jgnn.testes.dbpedia.DBpediaSPARQLDemo.getNumberOfInstancesByClass(DBpediaSPARQLDemo.java:1602)
at org.jgnn.testes.dbpedia.DBpediaSPARQLDemo.main(DBpediaSPARQLDemo.java:103)
public static int getNumberOfInstancesByClassAndEndpoint(String dbo_class,String regex_dbr, String sparqlEnpoint){
int offset=0;
int limit=10000;
//total number of instances
int numberOfInstances=0;
//number of instances of the iteration
int numberOfInstancesOfIter=1;
String dbo_class="dbo:Person";
while (numberOfInstancesOfIter!=0) {
String queryString="PREFIX dbo:<http://dbpedia.org/ontology/>\n"+
"SELECT DISTINCT ?x \n"+
"WHERE \n"+
"{ "+
"?x a "+dbo_class+ " ."+
"} ORDER BY ?x LIMIT "+
limit+" OFFSET "+ offset;
org.apache.jena.query.Query query = QueryFactory.create(queryString.toString());
// Remote execution.
try ( QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query) ) {
// Set the DBpedia specific timeout.
((QueryEngineHTTP)qexec).setTimeout(120, TimeUnit.SECONDS, 120, TimeUnit.SECONDS);
// Execute.
ResultSet rs = qexec.execSelect();
while(rs.hasNext()){
QuerySolution qs=rs.next();
}
//how many instances in this iteration?
numberOfInstancesOfIter=rs.getRowNumber();
//update instance number
numberOfInstances+=numberOfInstancesOfIter;
//it ends if the number of intances of iteration is less than the limit
if(numberOfInstancesOfIter < limit ){
numberOfInstancesOfIter=0;
}
if (!(numberOfInstances==0)){
System.out.println("ok");
}
java.util.concurrent.TimeUnit.SECONDS.sleep(2);
} catch (Exception e) {
e.printStackTrace();
}
offset+=limit;
}//while
System.out.println("Number of instances of "+dbo_class +" is :"+numberOfInstances);