RDF4j 和 GraphDB 存储库连接

Val*_*ina 1 graphdb rdf4j

我的 rdf4j 有问题:我想从我的 GraphDB 存储库中删除“Feed”所有带有feed:hashCode谓词的三元组。

第一个查询验证是否存在一个三元组,其中url参数作为主语,feed:hashCode作为谓语,并且hash参数具有宾语,并且它有效。如果我的存储库中不存在此语句,则第二个查询开始,它应该删除所有带有feed:hashCode谓词和url主语的三元组,但它不起作用,问题是什么?

这是代码:

public static boolean updateFeedQuery(String url, String hash) throws RDFParseException, RepositoryException, IOException{
    Boolean result=false;
    Repository repository = new SPARQLRepository("http://localhost:7200/repositories/Feed");
    repository.initialize();

    try {
        try (RepositoryConnection conn = repository.getConnection()) {
            BooleanQuery feedUrlQuery = conn.prepareBooleanQuery(
                    // @formatter:off
                    "PREFIX : <http://purl.org/rss/1.0/>\n" +
                    "PREFIX feed: <http://feed.org/>\n" +
                    "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
                    "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" +
                    "ASK{\n" +
                    "<"+url+"> feed:hashCode \""+hash+"\";\n"+
                    "rdf:type :channel.\n" +
                    "}"
                    // @formatter:on
            );

            result = feedUrlQuery.evaluate();

            //the feed is new or updated
            if(result == false) {

                Update removeOldHash = conn.prepareUpdate(
                        // @formatter:off
                        "PREFIX feed: <http://feed.org/>\n" +
                        "DELETE WHERE{\n"+
                        "<"+url+"> feed:hashCode ?s.\n" +
                        "}"
                        // @formatter:on
                        );
                removeOldHash.execute();
            }

        }
    }
    finally {
                 repository.shutDown();
                 return result;
    }

}
Run Code Online (Sandbox Code Playgroud)

错误代码为:“缺少参数:查询”,服务器响应为:“400 Bad Request”

Jee*_*tra 5

问题出在这一行:

Repository repository = new SPARQLRepository("http://localhost:7200/repositories/Feed");
Run Code Online (Sandbox Code Playgroud)

您用于SPARQLRepository访问 RDF4J/GraphDB 三元组存储,并且仅向其提供 SPARQL查询端点。根据文档,这意味着它将使用该端点进行查询和更新。但是,RDF4J Server(以及 GraphDB)具有用于 SPARQL 更新的单独端点(请参阅REST API 文档)。您的更新失败,因为SPARQLRepository尝试将其发送到查询端点,而不是更新端点。

解决方法之一是显式设置更新端点:

Repository repository = new SPARQLRepository("http://localhost:7200/repositories/Feed", "http://localhost:7200/repositories/Feed/statements");
Run Code Online (Sandbox Code Playgroud)

然而,SPARQLRepository它的真正目的是作为访问(非 RDF4J)SPARQL 端点(例如 DBPedia,或您自己控制之外的某个端点或运行不同的三重存储实现)的代理类。由于 GraphDB 完全兼容 RDF4J,因此您确实应该使用HTTPRepository来访问它。HTTPRepository实现完整的 RDF4J REST API,它扩展了基本的 SPARQL 协议,这将使您的客户端-服务器通信更加高效。有关如何有效访问远程 RDF4J/GraphDB 存储的更多详细信息,请参阅有关存储库 API 的 RDF4J 编程一章。