简体   繁体   English

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

[英]Is it possible to create a Sail repository with sparql endpoints?

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.我有本地 GraphDB SailRepository 的 config.ttl 文件,但我还想不仅通过直接 java 调用而且通过 SPARQL 端点访问它。 My config.ttl file looks like this:我的 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" ;
            ...
        ]
    ].

This configuration is a combination of a SPARQL and a Sail repository ( https://rdf4j.org/documentation/reference/configuration/ 1.1 and 1.3).此配置是 SPARQL 和 Sail 存储库的组合( https://rdf4j.org/documentation/reference/configuration/1.1和 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虽然可以通过 org.eclipse.rdf4j.repository.sail.SailRepository 进行访问,但我无法通过 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:但是通过 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");
    }
}

Exception:例外:

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

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. GraphDB 服务器需要先在 localhost:7200 上运行,SailRepository 应该内置到 GraphDB 的本地数据目录中,通常在 ~/.graphdb/data 上。 The SailRepository is just the "repository part", it does not provide any server. SailRepository 只是“存储库部分”,它不提供任何服务器。 This is the beauty of SailRepositorie's that they can be mounted on any server which implements its specifications.这就是 SailRepositorie 的美妙之处,它们可以安装在任何实现其规范的服务器上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM