RDF4j和GraphDB存储库连接

时间:2018-01-16 20:34:54

标签: 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;
    }

}

错误代码为:&#34;缺少参数:query&#34;,服务器响应为:&#34; 400 Bad Request&#34;

1 个答案:

答案 0 :(得分:3)

问题出在这一行:

Repository repository = new SPARQLRepository("http://localhost:7200/repositories/Feed");

您正在使用SPARQLRepository访问RDF4J / GraphDB三元组,并且您只为其提供SPARQL 查询端点。根据{{​​3}},这意味着它将使用该端点进行查询和更新。但是,RDF4J Server(以及GraphDB)有一个单独的SPARQL更新端点(请参阅the documentation)。您的更新失败,因为SPARQLRepository尝试将其发送到查询端点,而不是更新端点。

一种解决方法是明确设置更新端点:

Repository repository = new SPARQLRepository("http://localhost:7200/repositories/Feed", "http://localhost:7200/repositories/Feed/statements");

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