Search code examples
javajenalinkedmdb

How to use Jena TDB to store local version of Linked Movie Database


I have a local version of LinkedMDB that is in N-Triples format and want to query it. Now, I want to use Jena TDB, which can store the data that can be used for querying later. I checked the documentation for TDB Java API, but was unable to load the N-Triples file and then query with SPARQL. I've used the following code:

String directory = "E:\\Applications\\tdb-0.8.9\\TDB-0.8.9\\bin\\tdb";
        Dataset dataset = TDBFactory.createDataset(directory);

        // assume we want the default model, or we could get a named model here
        Model tdb = dataset.getDefaultModel();

        // read the input file - only needs to be done once
        String source = "E:\\Applications\\linkedmdb-18-05-2009-dump.nt";
        FileManager.get().readModel( tdb, source, "N-TRIPLES" );

and got the following Exception

Exception in thread "main" com.hp.hpl.jena.tdb.base.file.FileException: Not a directory: E:\Applications\tdb-0.8.9\TDB-0.8.9\bin\tdb
    at com.hp.hpl.jena.tdb.base.file.Location.<init>(Location.java:83)
    at com.hp.hpl.jena.tdb.TDBFactory.createDataset(TDBFactory.java:79)
    at tutorial.Temp.main(Temp.java:14)

Solution

  • Reading into a TDB-backed Model from Java is straightforward, see the TDB wiki for details. For example, you could:

    // open TDB dataset
    String directory = "./tdb";
    Dataset dataset = TDBFactory.createDataset(directory);
    
    // assume we want the default model, or we could get a named model here
    Model tdb = dataset.getDefaultModel();
    
    // read the input file - only needs to be done once
    String source = "path/to/input.nt";
    FileManager.get().readModel( tdb, source, "N-TRIPLES" );
    
    // run a query
    String q = "select * where {?s ?p ?o} limit 10";
    Query query = QueryFactory.create(q);
    QueryExecution qexec = QueryExecutionFactory.create(query, tdb);
    ResultSet results = qexec.execSelect();
    ... etc ...
    

    As user205512 mentioned, you can use tdbloader2 from the command line on a Linux or Mac, which will be faster on large RDF files. Once the TDB indexes have been created, you can copy the files to other machines. So you can load the data on a Linux server, then ship all the files inside the tdb directory to your Windows machine to continue development.

    To run tdbloader from the command line on your Windows machine, you'll need something like cygwin to allow you to run Unix-style scripts. You'll also need to set the environment variable TDBROOT.