Jena TDB to store and query using API
Asked Answered
V

2

6

I am new to both Jena-TDB and SPARQL, so it might be a silly question. I am using tdb-0.9.0, on Windows XP.

I am creating the TDB model for my trail_1.rdf file. My understanding here(correct me if I am wrong) is that the following code will read the given rdf file in TDB model and also stores/load (not sure what's the better word) the model in the given directory D:\Project\Store_DB\data1\tdb:

// open TDB dataset
String directory = "D:\\Project\\Store_DB\\data1\\tdb";
Dataset dataset = TDBFactory.createDataset(directory);

Model tdb = dataset.getDefaultModel();

// read the input file
String source = "D:\\Project\\Store_DB\\tmp\\trail_1.rdf";
FileManager.get().readModel( tdb, source);

tdb.close();
dataset.close();

Is this understanding correct?


As per my understanding since now the model is stored at D:\Project\Store_DB\data1\tdb directory, I should be able to run query on it at some later point of time.

So to query the TDB Store at D:\Project\Store_DB\data1\tdb I tried following, but it prints nothing:

String directory = "D:\\Project\\Store_DB\\data1\\tdb" ;
Dataset dataset = TDBFactory.createDataset(directory) ;

Iterator<String> graphNames = dataset.listNames();
while (graphNames.hasNext()) {
    String graphName = graphNames.next();
    System.out.println(graphName);
}

I also tried this, which also did not print anything:

    String directory = "D:\\Project\\Store_DB\\data1\\tdb" ;
    Dataset dataset = TDBFactory.createDataset(directory) ;

    String sparqlQueryString = "SELECT (count(*) AS ?count) { ?s ?p ?o }" ;

    Query query = QueryFactory.create(sparqlQueryString) ;
    QueryExecution qexec = QueryExecutionFactory.create(query, dataset) ;
    ResultSet results = qexec.execSelect() ;
    ResultSetFormatter.out(results) ;

What am I doing incorrect? Is there anything wrong with my understanding that I have mentioned above?

Ventral answered 19/6, 2012 at 13:15 Comment(0)
M
5

For part (i) of your question, yes, your understanding is correct.

For part (ii), the reason that listNames does not return any results is because you have not put your data into a named graph. In particular,

Model tdb = dataset.getDefaultModel();

means that you are storing data into TDB's default graph, i.e. the graph with no name. If you wish listNames to return something, change that line to:

Model tdb = dataset.getNamedGraph( "graph42" );

or something similar. You will, of course, then need to refer to that graph by name when you query the data.

If your goal is simply to test whether or not you have successfully loaded data into the store, try the command line tools bin/tdbdump (Linux) or bat\tdbdump.bat (Windows).

For part (iii), I tried your code on my system, pointing at one of my TDB images, and it works just as one would expect. So: either the TDB image you're using doesn't have any data in it (test with tdbdump), or the code you actually ran was different to the sample above.

Melaniamelanic answered 20/6, 2012 at 9:7 Comment(0)
E
3

The problem in your part 1 code is, I think, you are not committing the data .

Try with this version of your part 1 code:

   String directory = "D:\\Project\\Store_DB\\data1\\tdb";
   Dataset dataset = TDBFactory.createDataset(directory);

   Model tdb = dataset.getDefaultModel();

   // read the input file
   String source = "D:\\Project\\Store_DB\\tmp\\trail_1.rdf";
   FileManager.get().readModel( tdb, source);

   dataset.commit();//INCLUDE THIS STAMEMENT

   tdb.close();
   dataset.close();

and then try with your part 3 code :) ....

Egad answered 27/2, 2014 at 12:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.