我正在熟悉芝麻三重存储并尝试添加和检索数据等基本内容。当我使用 SailRepository 时一切正常,但是当我使用如下所示的 http 存储库时,我收到此错误:
repository initialized
Exception in thread "main" org.openrdf.repository.http.HTTPQueryEvaluationException:
at org.openrdf.repository.http.HTTPTupleQuery.evaluate(HTTPTupleQuery.java:59)
at servlet.sesame.Test.isStored(Test.java:28)
at servlet.sesame.Test.ADD(Test.java:33)
at servlet.sesame.Test.main(Test.java:54)
Caused by: org.openrdf.repository.RepositoryException:
at org.openrdf.http.client.HTTPClient.handleHTTPError(HTTPClient.java:953)
at org.openrdf.http.client.HTTPClient.sendTupleQueryViaHttp(HTTPClient.java:718)
at org.openrdf.http.client.HTTPClient.getBackgroundTupleQueryResult(HTTPClient.java:602)
at org.openrdf.http.client.HTTPClient.sendTupleQuery(HTTPClient.java:367)
at org.openrdf.repository.http.HTTPTupleQuery.evaluate(HTTPTupleQuery.java:53)
... 3 more
这是我的代码:
public class Test {
public static boolean isStored(String id, Repository rep) throws RepositoryException, QueryEvaluationException, MalformedQueryException
{
ValueFactory f = rep.getValueFactory();
URI testedIdURI = f.createURI("http://example.org/" + id);
// Check if
String request ="SELECT ?subject WHERE{<"+ testedIdURI +"> ?predicate ?object}";
TupleQueryResult reponse = rep.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, request).evaluate();
return reponse.hasNext();
}
public static void ADD(String id, Repository rep) throws RepositoryException, QueryEvaluationException, MalformedQueryException{
boolean is = isStored(id,rep);
if(is){
System.out.println("already exists");
}
else{
rep.getConnection().add(rep.getValueFactory().createURI("http://example.org/", id), RDF.TYPE,FOAF.PERSON);
System.out.println(id+" added");
}
}
public static void main(String[] args) throws RepositoryException,RDFHandlerException, MalformedQueryException, QueryEvaluationException {
Repository rep = new HTTPRepository("http://localhost:8080/openrdf-workbench/","Test");
//Repository rep = new SailRepository(new MemoryStore());
rep.initialize();
System.out.println("repository initialized");
ValueFactory f = rep.getValueFactory();
try{
ADD("Timon",rep);
ADD("Pumba",rep);
ADD("eddy",rep);
ADD("Pumba",rep);
ADD("Timon",rep);
rep.getConnection().add(f.createURI("http://example.org/", "Turin"),RDF.PREDICATE,f.createURI("http://example.org/", "Timon"));
rep.getConnection().add(f.createURI("http://example.org/", "Turin"),RDF.PREDICATE,f.createURI("http://example.org/", "Timon"));
rep.getConnection().add(f.createURI("http://example.org/", "Timon"),RDF.PREDICATE, f.createURI("http://example.org/", "eddy"));
rep.getConnection().add(f.createURI("http://example.org/", "Pumba"),RDF.PREDICATE, f.createURI("http://example.org/", "Timon"));
// RepositoryResult<Statement> statements = rep.getConnection().getStatements(null, null, null, true);
// Model model = Iterations.addAll(statements, new LinkedHashModel());
String request = "SELECT DISTINCT ?object WHERE{<" +f.createURI("http://example.org/", "Timon")+"> <"+ RDF.PREDICATE +"> ?object }";
rep.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, request);
TupleQueryResult res = rep.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, request).evaluate();
String s="";
while(res.hasNext())
{
BindingSet bs = res.next();
s+="\n " +(bs.getBinding("object").getValue().stringValue());
}
System.out.println(s);
}
finally
{
rep.getConnection().close();
}
}
}
最佳答案
您的代码有一些问题。直接的问题可能是由这一行引起的:
Repository rep = new HTTPRepository("http://localhost:8080/openrdf-workbench/","Test");
您在这里使用了错误的服务器 URL。正确的服务器 URL 是 http://localhost:8080/openrdf-sesame/
。改变这个应该可以解决眼前的问题。
除此之外,我还为您提供了一个技巧,可以让您的代码更健壮、更快且更具可扩展性。在代码中,您在方法之间传递 Repository 对象,并且每次执行更新或查询时都会创建一个新的 RepositoryConnection。这确实非常低效,更不用说您随后没有关闭任何连接。
因此,我建议您多重复使用 RepositoryConnection 对象,并在完成后正确关闭它。例如,代替:
rep.getConnection().add(f.createURI("http://example.org/", "Turin"),RDF.PREDICATE,f.createURI("http://example.org/", "Timon"));
rep.getConnection().add(f.createURI("http://example.org/", "Turin"),RDF.PREDICATE,f.createURI("http://example.org/", "Timon"));
rep.getConnection().add(f.createURI("http://example.org/", "Timon"),RDF.PREDICATE, f.createURI("http://example.org/", "eddy"));
rep.getConnection().add(f.createURI("http://example.org/", "Pumba"),RDF.PREDICATE, f.createURI("http://example.org/", "Timon"));
做这样的事情:
RepositoryConnection conn = rep.getConnection();
try {
conn.begin(); // start a transaction
conn.add(f.createURI("http://example.org/", "Turin"),RDF.PREDICATE,f.createURI("http://example.org/", "Timon"));
conn.add(f.createURI("http://example.org/", "Turin"),RDF.PREDICATE,f.createURI("http://example.org/", "Timon"));
conn.add(f.createURI("http://example.org/", "Timon"),RDF.PREDICATE, f.createURI("http://example.org/", "eddy"));
conn.add(f.createURI("http://example.org/", "Pumba"),RDF.PREDICATE, f.createURI("http://example.org/", "Timon"));
conn.commit();
}
finally {
conn.close();
}
关于jakarta-ee - 如何从 Sesame 服务器添加和检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25012401/