【问题标题】:RDF4j and GraphDB repository connectionRDF4j 和 GraphDB 存储库连接
【发布时间】:2018-01-16 20:34:54
【问题描述】:

我的 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;
    }

}

错误码为:“Missing parameter: query”,服务器响应为:“400 Bad Request”

【问题讨论】:

    标签: graphdb rdf4j


    【解决方案1】:

    问题出在这一行:

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

    您正在使用 SPARQLRepository 访问您的 RDF4J/GraphDB 三元存储,并且您只为它提供了一个 SPARQL 查询 端点。根据the documentation,这意味着它将使用该端点进行查询和更新。但是,RDF4J 服务器(以及因此的 GraphDB)有一个单独的 SPARQL 更新端点(参见REST API documentation)。您的更新失败,因为SPARQLRepository 尝试将其发送到查询端点,而不是更新端点。

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多