I have a question.
I am trying to load my RDF files on Jena TDB.
I have written this code:
public void store(){
String directory = "C:\\tdb";
String source = "C:\\file1.rdf";
String source1 = "C:\\file2.rdf";
Dataset dataset = openTDB(directory);
Model tdb = loadModel(source, dataset);
dataset.addNamedModel("File1", tdb);
Model tdb1 = loadModel(source1, dataset);
dataset.addNamedModel("File2", tdb1);
queryTDB(tdb, dataset);
queryTDB(tdb1, dataset);
tdb.close();
tdb1.close();
dataset.close();
}
public Dataset openTDB(String directory) {
// open TDB dataset
Dataset dataset = TDBFactory.createDataset(directory);
return dataset;
}
public Model loadModel(String source, Dataset dataset) {
Model tdb = dataset.getDefaultModel();
FileManager.get().readModel( tdb, source, "RDF/XML" );
return tdb;
}
In particular, I have two files and I want to load these files on Jena TDB. I have read on Internet that I can add a name to my models using "addNamedModel". In doing so, in the code above, I added the names "File1" and "File2".
Now, I want to query this dataset and I'm trying to write this code:
public void queryTDB(Model tdb, Dataset dataset) {
String queryStr = "SELECT * { ?s ?p ?o }";
Query query = QueryFactory.create(queryStr);
QueryExecution qexec = QueryExecutionFactory.create(query, tdb);
/*Execute the Query*/
ResultSet results = qexec.execSelect();
ResultSetFormatter.out(results) ;
qexec.close();
}
This code works, but I would like to know how I can get the query results only for the model named "File1" (or "File2"). In fact, with the query written so, I get the results of both models.
How I can realize it?
You're not using absolute IRIs to name you graphs, so I don't know offhand what your graph names will be. You can use a query like this to help find out what they are:
select * {
graph ?g {
?s ?p ?o
}
}
Once you've done that, you can continue using the graph ?g { … }
pattern, or use from named
to specify a graph in your query:
select *
from named <name-of-graph>
{
?s ?p ?o
}
select * {
graph <name-of-graph> {
?s ?p ?o
}
}
See 13.2 Specifying RDF Datasets and 13.3 Querying the Dataset for the full details and for more examples.