Question

I am trying to load a turtle file(.ttl) to a Repository using OpenRDF/Sesame framework, and the code that I wrote is like this-

public class RepositoryAPI {

public static void main(String[] args) {

    File dataDir = new File("C:\\Temp\\sesameRepo\\KB_Exemplar");
    try {
        Repository repo = createRepository(dataDir);
        addRDFToRepository(repo);
    } catch (RepositoryException e) {
        e.printStackTrace();
    }
}

private static Repository createRepository(File dataDir) throws RepositoryException {
    Repository repos = new SailRepository(new MemoryStore(dataDir));
    repos.initialize();
    return repos;
}

private static void addRDFToRepository(Repository repo) {
    File file = new File("C:\\LDMVelocityWork\\KB_Quality_Exemplar_BE_Ownership.ttl");
    String baseURI = "http://data.jpmchase.net/cib/cia/KBQualityExemplar";
    try{
        RepositoryConnection con = repo.getConnection();
        try{
            con.add(file, baseURI, RDFFormat.RDFXML);
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
              con.close();
           }
    }
    catch (OpenRDFException e) {
        }
}

}

The Directory named KB_Exemplar is getting produced but inside the the file that is created is not having any proper value. The content of that file, named 'memorystore.data' is something weird. Can some one please check and let me know if I am writing the code right.

Was it helpful?

Solution

There is a lot that looks strange about your code.

Firstly you state that you are trying to load a Turtle file but you have the following code:

File file = new File("C:\\LDMVelocityWork\\KB_Quality_Exemplar_BE_Ownership.ttl");
String baseURI = "http://data.jpmchase.net/cib/cia/KBQualityExemplar";
try{
    RepositoryConnection con = repo.getConnection();
    try{
        con.add(file, baseURI, RDFFormat.RDFXML);
    } catch (IOException e) {
        e.printStackTrace();
    }
    finally {
          con.close();
       }
}
catch (OpenRDFException e) {
    }

Which tells Sesame to read from your given Turtle file but treat it as RDF/XML on this line:

con.add(file, baseURI, RDFFormat.RDFXML);

This is almost certainly throwing an error but you don't do anything in your outer catch block (where you catch OpenRDFException) so you are hiding this error and almost certainly no data is actually getting added to your repository.

Your code should most likely be doing the following add() call:

con.add(file, baseURI, RDFFormat.TURTLE);

Plus you need to add some actual error handling logic to your outer catch block so you aren't hiding errors.

Secondly you state the following:

The Directory named KB_Exemplar is getting produced but inside the the file that is created is not having any proper value. The content of that file, named 'memorystore.data' is something weird

The Sesame repository format is not intended to be human readable, it's a binary representation of the RDF data and indices into that data.

If you wish to get the data back out in a human readable format then you can write code to do that. For example to get the data back out as Turtle you'd do something like the following:

conn.export(new TurtleWriter());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top