我的情况很简单:我有一个 RDF (N3) 文件,我想将其上传到 sparql-auth-endpoint。我使用 Jena ARQ,但不幸的是它不起作用;我没有收到任何错误,但图表没有改变。问题可能出在哪里?我的代码:
HttpContext httpContext = new BasicHttpContext();
CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(new AuthScope(AuthScope.ANY_HOST,
AuthScope.ANY_PORT), new UsernamePasswordCredentials("user", "pass"));
httpContext.setAttribute(ClientContext.CREDS_PROVIDER, provider);
GraphStore graphStore = GraphStoreFactory.create() ;
UpdateRequest request = UpdateFactory.read("turtle.ttl");
/*another possible query, same result - no errors but the graph doesn't change
request.add(new UpdateCreate("graph")) ;
request.add(new UpdateLoad("file:turtle_2.ttl", "graph")) ;
request.add("prefix dcterms: <http://purl.org/dc/terms/>"
+ "INSERT DATA { GRAPH <graph> {<http://example/book3> dcterms:title \"A new book\" }}");
UpdateAction.execute(request, graphStore) ;
*/
UpdateProcessor processor = UpdateExecutionFactory
.createRemote(request, "http://sparql-endpoint/sparql-auth");
((UpdateProcessRemote)processor).setHttpContext(httpContext);
processor.execute();
如何修改代码以将该数据上传到 sparql-endpoint?
编辑 1
这是一个较小的示例,也不起作用。我可以在浏览器中通过 sparql-endpoint 创建一个新图,但在耶拿我不能;没有错误,没有新图表。
UpdateRequest request = UpdateFactory.create();
request.add(new UpdateCreate("http://linkeddata.com/graph"));
UpdateProcessor processor = UpdateExecutionFactory
.createRemote(request, "http://sparql-endpoint/sparql-auth");
((UpdateProcessRemote)processor).setHttpContext(httpContext);
processor.execute();
编辑2
使用带有 sparql-auth-endpoint 的 Virtuoso 服务器,我更改了这些行,并且效果更好。
UpdateProcessor processor = UpdateExecutionFactory
.createRemoteForm(request, "http://sparql-endpoint/sparql-auth");
((UpdateProcessRemoteForm)processor).setHttpContext(httpContext);
processor.execute();
嗯,它可以在 localhost:8890 上运行,但不能在另一个 virtuoso 服务器上运行
Exception in thread "main" org.apache.jena.atlas.web.HttpException: 400 Bad Request
at org.apache.jena.riot.web.HttpOp.httpResponse(HttpOp.java:425)
at org.apache.jena.riot.web.HttpOp.execHttpPostForm(HttpOp.java:316)
at com.hp.hpl.jena.sparql.modify.UpdateProcessRemoteForm.execute(UpdateProcessRemoteForm.java:75)
at bakalarska_prace.UpVirtuoso.main(UpVirtuoso.java:108) -> processor.execute()
最佳答案
您在这行中读到的实际 SPARQL 更新是什么:
UpdateRequest request = UpdateFactory.read("turtle.ttl");
如果不知道实际的 SPARQL 更新是什么,就很难说出了什么问题。
猜测...
根据您注释掉的示例,您是否尝试执行以下操作:
LOAD <file:local.ttl> INTO GRAPH <http://graph>
如果是这种情况,那么这将不起作用,远程服务器无权访问您的本地文件系统,因此该命令将不起作用。
此外,file:local.ttl
实际上是一个无效的 URI,文件 URI 应该类似于 file:///local.ttl
。
编辑
根据评论,我认为您的问题可能在于所使用的图形 URI,在您的所有示例中,您都使用相对 URI,因此您的数据可能不会出现在您期望的图形中。您的数据可能已被插入,但没有插入到您期望的图表中,如果您没有太多数据,您可以尝试以下查询来转储命名图表中的所有数据,以查看数据是否已到达某处:
SELECT * WHERE { GRAPH ?g { ?s ?p ?o } }
或者,如果您希望商店中有少量命名图表,下面将列出命名图表,以便您可以查看是否有任何意外的图表:
SELECT DISTINCT ?g WHERE { GRAPH ?g { } }
一般来说,使用绝对 URI 来确保您在所需的图表中创建所需的数据始终是一个好主意,相对 URI 很容易出错,因为它们可以根据它们所在的上下文解析为不同的绝对 URI已解决。
关于rdf - Jena SPARQL 更新不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16487746/