Search code examples
graphdbrdf4j

Create a repository on a remote server with RDF4J


I've been trying to create a new repository on a remote GraphDB server using RDF4J, but I'm having problems.

This runs, but is seemingly not correct

HTTPRepositoryConfig implConfig = new HTTPRepositoryConfig(address);
RepositoryConfig repoConfig = new RepositoryConfig("test", "test", implConfig);
Model m = new

However, based on the info I get from "edit repository" in the workbench, the result doesn't look right. All the values are empty, except for id and title.

This fails

I tried to copy the settings from an existing repository that I created on the workbench, but that failed with:

org.eclipse.rdf4j.repository.config.RepositoryConfigException: 
                         Unsupported repository type: owlim:MonitorRepository

The code for that attempt is inspired by the one found here . Except that the config file is based on an existing repo, as explained above. I also tried to config file provided in the example, but that failed aswell:

org.eclipse.rdf4j.repository.config.RepositoryConfigException: 
       Unsupported Sail type: graphdb:FreeSail

Anyone got any tips?

UPDATE As Henriette Harmse correctly pointed out, I should have provided my code, not simply linked to it. That way I might have discovered that I hadn't done a complete copy after all, but changed the important first bits that she points out in her answer. Full code below:

String address = "serveradr";
RemoteRepositoryManager repositoryManager = new RemoteRepositoryManager( address);
repositoryManager.initialize();

// Instantiate a repository graph model
TreeModel graph = new TreeModel();
InputStream config = Rdf4jHelper.class.getResourceAsStream("/repoconf2.ttl");
RDFParser rdfParser = Rio.createParser(RDFFormat.TURTLE);
rdfParser.setRDFHandler(new StatementCollector(graph));
rdfParser.parse(config, RepositoryConfigSchema.NAMESPACE);
config.close();

// Retrieve the repository node as a resource
Resource repositoryNode = graph.filter(null, RDF.TYPE, RepositoryConfigSchema.REPOSITORY).subjects().iterator().next();

// Create a repository configuration object and add it to the repositoryManager
RepositoryConfig repositoryConfig = RepositoryConfig.create(graph, repositoryNode);

It fails on the last line.

ANSWERED @HenrietteHarmse gives the correct method in her answer below. The error is caused by missing dependencies. Instead of using RDF4J directly, I should have used the graphdb-free-runtime.


Solution

  • There are a number of issues here:

    (1) RepositoryManager repositoryManager = new LocalRepositoryManager(new File(".")); will create a repository where ever your Java application is running from.

    (2) Changing to new LocalRepositoryManager(new File("$GraphDBInstall/data/repositories")) will cause the repository to be created under the control of GraphDB (assuming you have a local GraphDB instance) only if GraphDB is not running. If you start GraphDB after running your program, you will be able to see the repository in GraphDB workbench.

    (3) What you need to do is get the repository manager of the remote GraphDB, which can be done with RepositoryManager repositoryManager = RepositoryProvider.getRepositoryManager("http://IPAddressOfGraphDB:7200");.

    (4) In the way you have specified the config, you cause the RDF graph config to be lost. The correct way to specify it is:

    RepositoryConfig repositoryConfig = RepositoryConfig.create(graph, repositoryNode);
    repositoryManager.addRepositoryConfig(repositoryConfig);
    

    (5) A minor issue is that GraphUtil.getUniqueSubject(...) has been deprecated, for which you can use something like the following:

    Model model = graph.filter(null, RDF.TYPE, RepositoryConfigSchema.REPOSITORY);
    Iterator<Statement> iterator = model.iterator(); 
    if (!iterator.hasNext()) 
       throw new RuntimeException("Oops, no <http://www.openrdf.org/config/repository#> subject found!");
    Statement statement = iterator.next();
    Resource repositoryNode =  statement.getSubject();
    

    EDIT on 20180408:

    (5) Or you can use the compact option as @JeenBroekstra suggested in the comments:

    Models.subject(
        graph.filter(null, RDF.TYPE, RepositoryConfigSchema.REPOSITORY))
        .orElseThrow(() -> new RuntimeException("Oops, no <http://www.openrdf.org/config/repository#> subject found!")); 
    

    EDIT on 20180409:

    For convenience I have added the complete code example here.

    EDIT on 20180410:

    So the actual culprit turned out to be an incorrect pom.xml. The correct version is as below:

    <dependency>
      <groupId>com.ontotext.graphdb</groupId>
      <artifactId>graphdb-free-runtime</artifactId>
      <version>8.4.1</version>
    </dependency>