I'm trying to query jena TDB in java. My code follows. It seems that my TDB dataset is empty because model.size()
(where model
is the dataset default model) return 0. I'm sure it isn't empty, though, because I can query it with Fuseki with my sparqlEndpoint.
String directory = "//var//www//fuseki//TDB" ;
Dataset dataset = TDBFactory.createDataset(directory) ;
Model model = dataset.getDefaultModel();
String sparqlQueryString = "SELECT (count(*) AS ?count) { ?s ?p ?o }" ;
Query query = QueryFactory.create(sparqlQueryString) ;
QueryExecution qexec = QueryExecutionFactory.create(query, model);
ResultSet results = qexec.execSelect() ;
ResultSetFormatter.out(results) ;
qexec.close();
System.err.printf("Model size is: %s\n", model.size());
It's seems my TDB is empty because model.size() return 0. I'm sure it isn't empty because I can query it with fuseki with my sparqlEndpoint.
Things can exist without having contents. The length of the string ""
is 0, but it's still a string. You didn't show the results of your query
SELECT (count(*) AS ?count) { ?s ?p ?o }
Did it return 0, or something else? Also, when you do
Model model = dataset.getDefaultModel();
you're only getting the default model of the dataset, but a dataset can contain a number of named graphs, in addition to the default graph. If you do have triples in your dataset, they might be in named graphs, in which case you could do something like
select (count(*) as ?count) { graph ?g { ?s ?p ?o } }
I don't know offhand how Fuseki handles the default graph, but it might be that the default graph of the dataset is configured (I think this can be customized) to be the union of the named graphs in the dataset. When that's the case, the result of getDefaultModel
might still be an empty model. I'm speculating on that point, but it's a conceivable way that you could get results from the default graph while still having the default model be empty.
来源:https://stackoverflow.com/questions/24918966/query-jena-tdb-store