是否可以使用 sparql 端点创建 Sail 存储库?

发布于 2025-01-14 04:09:07 字数 5992 浏览 0 评论 0原文

我有本地 GraphDB SailRepository 的 config.ttl 文件,但我还想不仅通过直接 java 调用而且还通过 SPARQL 端点来访问它。我的 config.ttl 文件如下所示:

# RDF4J configuration template for a GraphDB Free repository

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
@prefix rep: <http://www.openrdf.org/config/repository#>.
@prefix sr: <http://www.openrdf.org/config/repository/sail#>.
@prefix sail: <http://www.openrdf.org/config/sail#>.
@prefix owlim: <http://www.ontotext.com/trree/owlim#>.
@prefix sparql: <http://www.openrdf.org/config/repository/sparql#>.

[] a rep:Repository ;
    rep:repositoryID "test" ;
    rep:repositoryImpl [
        rep:repositoryType "graphdb:FreeSailRepository" ;
        sparql:query-endpoint <http://localhost:7200/repositories/test> ;
        sparql:update-endpoint <http://localhost:7200/repositories/test/statements> ;
        sr:sailImpl [
            sail:sailType "graphdb:FreeSail" ;
            ...
        ]
    ].

此配置是 SPARQL 和 Sail 存储库的组合 (https ://rdf4j.org/documentation/reference/configuration/ 1.1 和 1.3)。虽然可以通过 org.eclipse.rdf4j.repository.sail.SailRepository 进行访问,但我无法通过 org.eclipse.rdf4j.repository.sparql.SPARQLRepository 访问我的本地存储库

这是完整的代码:

private static LocalRepositoryManager repositoryManager;
private static RepositoryConnection embeddedRepoCon;
private static SPARQLProtocolSession session;

@BeforeClass
public static void init() {
    try {
        //Create local repo
        File baseDir = new File("target","GraphDB");
        if (!baseDir.exists())
            baseDir.mkdirs();
        repositoryManager = new LocalRepositoryManager(baseDir);
        repositoryManager.init();
        if(new File("target/GraphDB/repositories/test").exists()) {
            repositoryManager.removeRepository("test");
            System.out.println("Repository removed.");
        }

        //Add repository config to repository manager
        InputStream config = TestRDFStarTimestampingPlugin.class.getResourceAsStream("/repo-defaults.ttl");
        Model repo_config_graph = Rio.parse(config, "", RDFFormat.TURTLE);
        Resource repositoryNode = Models.subject(repo_config_graph.filter(null, RDF.TYPE, RepositoryConfigSchema.REPOSITORY)).orElse(null);
        RepositoryConfig repositoryConfig = RepositoryConfig.create(repo_config_graph, repositoryNode);
        repositoryManager.addRepositoryConfig(repositoryConfig);

        //Initialize repo
        //Repository repo = repositoryManager.getRepository("test");
        SailRepository repo = (SailRepository) repositoryManager.getRepository("test");
        repo.init();

        //Establish connection to repo
        embeddedRepoCon = repo.getConnection();

    } catch (RDFHandlerException | RDFParseException | IOException | RepositoryConfigException | RepositoryException  e) {
        System.err.println("The GraphDB repository will not be created.");
        System.err.println(e.getMessage());
    }

}

以下测试通过

   @Test
    public void repoSailConnectionTest() {
        //Test queries
        BooleanQuery query = embeddedRepoCon.prepareBooleanQuery("ask from <http://example.com/testGraph> { ?s ?p ?o }");
        boolean hasResults = query.evaluate();
        assertFalse("No triples should be in the graph yet.", hasResults);
        System.out.println("Result from ask query: " + hasResults);
        System.out.println("Read queries are executable against the embedded repository");

        // Test update statements
        String updateString;
        updateString = "clear graph <http://example.com/testGraph>";
        embeddedRepoCon.prepareUpdate(updateString).execute();
        embeddedRepoCon.commit();

        updateString = "delete data {graph <http://example.com/testGraph> " +
                "{<http://example.com/s/v1> <http://example.com/p/v2> <http://example.com/o/v3>}}";
        embeddedRepoCon.prepareUpdate(updateString).execute();
        embeddedRepoCon.commit();
        System.out.println("Write statements are executable against the embedded repository");

但通过 SPARQLRepository 测试进行访问未通过,因为连接被拒绝:

@Test
public void repoSPARQLConnectionTest() {
    //Test queries
    SPARQLRepository repo = new SPARQLRepository("http://localhost:7200/repositories/test");
    repo.init();
    try (RepositoryConnection connection = repo.getConnection()) {
        BooleanQuery query = connection.prepareBooleanQuery("ask from <http://example.com/testGraph> { ?s ?p ?o }");
        boolean hasResults = query.evaluate();
        assertFalse("No triples should be in the graph yet.", hasResults);
        System.out.println("Result from ask query: " + hasResults);
        System.out.println("Read queries are executable against the embedded repository");
    }
    repo.shutDown();

    // Test update statements
    repo = new SPARQLRepository("http://localhost:7200/repositories/test/statements");
    repo.init();
    try (RepositoryConnection connection = repo.getConnection()) {
        String updateString;
        updateString = "clear graph <http://example.com/testGraph>";
        connection.begin();
        connection.prepareUpdate(updateString).execute();
        connection.commit();

        updateString = "delete data {graph <http://example.com/testGraph> " +
                "{<http://example.com/s/v1> <http://example.com/p/v2> <http://example.com/o/v3>}}";
        connection.prepareUpdate(updateString).execute();
        connection.commit();
        System.out.println("Write statements are executable against the embedded repository");
    }
}

异常:

org.eclipse.rdf4j.query.QueryEvaluationException: Connect to localhost:7200 [localhost/127.0.0.1] failed: Connection refused (Connection refused)

I have config.ttl file for a local GraphDB SailRepository but I also want to access it not only via direct java invocation but also via SPARQL endpoints. My config.ttl file looks like this:

# RDF4J configuration template for a GraphDB Free repository

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
@prefix rep: <http://www.openrdf.org/config/repository#>.
@prefix sr: <http://www.openrdf.org/config/repository/sail#>.
@prefix sail: <http://www.openrdf.org/config/sail#>.
@prefix owlim: <http://www.ontotext.com/trree/owlim#>.
@prefix sparql: <http://www.openrdf.org/config/repository/sparql#>.

[] a rep:Repository ;
    rep:repositoryID "test" ;
    rep:repositoryImpl [
        rep:repositoryType "graphdb:FreeSailRepository" ;
        sparql:query-endpoint <http://localhost:7200/repositories/test> ;
        sparql:update-endpoint <http://localhost:7200/repositories/test/statements> ;
        sr:sailImpl [
            sail:sailType "graphdb:FreeSail" ;
            ...
        ]
    ].

This configuration is a combination of a SPARQL and a Sail repository (https://rdf4j.org/documentation/reference/configuration/ 1.1 and 1.3). While an access via org.eclipse.rdf4j.repository.sail.SailRepository is possible I cannot access my local repository via org.eclipse.rdf4j.repository.sparql.SPARQLRepository

Here is the full code:

private static LocalRepositoryManager repositoryManager;
private static RepositoryConnection embeddedRepoCon;
private static SPARQLProtocolSession session;

@BeforeClass
public static void init() {
    try {
        //Create local repo
        File baseDir = new File("target","GraphDB");
        if (!baseDir.exists())
            baseDir.mkdirs();
        repositoryManager = new LocalRepositoryManager(baseDir);
        repositoryManager.init();
        if(new File("target/GraphDB/repositories/test").exists()) {
            repositoryManager.removeRepository("test");
            System.out.println("Repository removed.");
        }

        //Add repository config to repository manager
        InputStream config = TestRDFStarTimestampingPlugin.class.getResourceAsStream("/repo-defaults.ttl");
        Model repo_config_graph = Rio.parse(config, "", RDFFormat.TURTLE);
        Resource repositoryNode = Models.subject(repo_config_graph.filter(null, RDF.TYPE, RepositoryConfigSchema.REPOSITORY)).orElse(null);
        RepositoryConfig repositoryConfig = RepositoryConfig.create(repo_config_graph, repositoryNode);
        repositoryManager.addRepositoryConfig(repositoryConfig);

        //Initialize repo
        //Repository repo = repositoryManager.getRepository("test");
        SailRepository repo = (SailRepository) repositoryManager.getRepository("test");
        repo.init();

        //Establish connection to repo
        embeddedRepoCon = repo.getConnection();

    } catch (RDFHandlerException | RDFParseException | IOException | RepositoryConfigException | RepositoryException  e) {
        System.err.println("The GraphDB repository will not be created.");
        System.err.println(e.getMessage());
    }

}

Following test passes

   @Test
    public void repoSailConnectionTest() {
        //Test queries
        BooleanQuery query = embeddedRepoCon.prepareBooleanQuery("ask from <http://example.com/testGraph> { ?s ?p ?o }");
        boolean hasResults = query.evaluate();
        assertFalse("No triples should be in the graph yet.", hasResults);
        System.out.println("Result from ask query: " + hasResults);
        System.out.println("Read queries are executable against the embedded repository");

        // Test update statements
        String updateString;
        updateString = "clear graph <http://example.com/testGraph>";
        embeddedRepoCon.prepareUpdate(updateString).execute();
        embeddedRepoCon.commit();

        updateString = "delete data {graph <http://example.com/testGraph> " +
                "{<http://example.com/s/v1> <http://example.com/p/v2> <http://example.com/o/v3>}}";
        embeddedRepoCon.prepareUpdate(updateString).execute();
        embeddedRepoCon.commit();
        System.out.println("Write statements are executable against the embedded repository");

But the access via SPARQLRepository test does not pass because the connection gets refused:

@Test
public void repoSPARQLConnectionTest() {
    //Test queries
    SPARQLRepository repo = new SPARQLRepository("http://localhost:7200/repositories/test");
    repo.init();
    try (RepositoryConnection connection = repo.getConnection()) {
        BooleanQuery query = connection.prepareBooleanQuery("ask from <http://example.com/testGraph> { ?s ?p ?o }");
        boolean hasResults = query.evaluate();
        assertFalse("No triples should be in the graph yet.", hasResults);
        System.out.println("Result from ask query: " + hasResults);
        System.out.println("Read queries are executable against the embedded repository");
    }
    repo.shutDown();

    // Test update statements
    repo = new SPARQLRepository("http://localhost:7200/repositories/test/statements");
    repo.init();
    try (RepositoryConnection connection = repo.getConnection()) {
        String updateString;
        updateString = "clear graph <http://example.com/testGraph>";
        connection.begin();
        connection.prepareUpdate(updateString).execute();
        connection.commit();

        updateString = "delete data {graph <http://example.com/testGraph> " +
                "{<http://example.com/s/v1> <http://example.com/p/v2> <http://example.com/o/v3>}}";
        connection.prepareUpdate(updateString).execute();
        connection.commit();
        System.out.println("Write statements are executable against the embedded repository");
    }
}

Exception:

org.eclipse.rdf4j.query.QueryEvaluationException: Connect to localhost:7200 [localhost/127.0.0.1] failed: Connection refused (Connection refused)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮云落日 2025-01-21 04:09:07

GraphDB 服务器需要首先在 localhost:7200 上运行,并且 SailRepository 应构建到 GraphDB 的本地数据目录中,通常位于 ~/.graphdb/data 上。 SailRepository 只是“存储库部分”,它不提供任何服务器。这就是 SailRepositorie 的优点,它们可以安装在任何实现其规范的服务器上。

The GraphDB server needs to be running on localhost:7200 first and the SailRepository should be built into GraphDB's local data directory, which is usually on ~/.graphdb/data. The SailRepository is just the "repository part", it does not provide any server. This is the beauty of SailRepositorie's that they can be mounted on any server which implements its specifications.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文