首页 文章

RDF4j和GraphDB存储库连接

提问于
浏览
2

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

}

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

1 回答

  • 3

    问题出在这一行:

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

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

    解决方法之一是明确设置更新 endpoints :

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

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

相关问题