Error when I load RDF triples in TDB Triple Store

2019-06-11 02:44发布

问题:

I have a question for you:

I have loaded my file RDF in TDB Triple Store:

Dataset dataset = TDBFactory.createDataset(directory);
Model model = dataset.getNamedModel("http://nameFile");
TDBLoader.loadModel(model, file );

Now, I would like to realize a procedure which checks whether the graph is on the Triple Store or not.

I have written this code:

String queryStr = "select * {graph <http://nameFile> { ?s ?p ?o }}";

Dataset dataset = TDBFactory.createDataset(directory);

Query query = QueryFactory.create(queryStr);
QueryExecution qexec = QueryExecutionFactory.create(query, dataset);
qexec.getContext().set(TDB.symUnionDefaultGraph, true);

/*Execute the Query*/
ResultSet results = qexec.execSelect();

if (!results.hasNext()) {
    Model model = dataset.getNamedModel("http://nameFile");
    TDBLoader.loadModel(model, label);
} else {
    Model model = dataset.getNamedModel("http://nameFile");
}

StmtIterator stmti = model.listStatements();

while (stmti.hasNext()) {
    Statement statement = stmti.nextStatement();
    System.out.println(statement); 
}

I have seen that this code fails with this error:

Exception in thread "main" java.lang.UnsupportedOperationException: Quad: subject cannot be null

at com.hp.hpl.jena.sparql.core.Quad.(Quad.java:62)
at com.hp.hpl.jena.tdb.lib.TupleLib.quad(TupleLib.java:162)
at com.hp.hpl.jena.tdb.lib.TupleLib.quad(TupleLib.java:153)
at com.hp.hpl.jena.tdb.lib.TupleLib.access$100(TupleLib.java:45)
at com.hp.hpl.jena.tdb.lib.TupleLib$4.convert(TupleLib.java:87)
at com.hp.hpl.jena.tdb.lib.TupleLib$4.convert(TupleLib.java:83)
at org.apache.jena.atlas.iterator.Iter$4.next(Iter.java:322)
at org.apache.jena.atlas.iterator.Iter$4.next(Iter.java:322)
at org.apache.jena.atlas.iterator.Iter.next(Iter.java:920)
at com.hp.hpl.jena.util.iterator.WrappedIterator.next(WrappedIterator.java:94)
at com.hp.hpl.jena.util.iterator.Map1Iterator.next(Map1Iterator.java:45)
at com.hp.hpl.jena.util.iterator.WrappedIterator.next(WrappedIterator.java:94)
at com.hp.hpl.jena.rdf.model.impl.StmtIteratorImpl.next(StmtIteratorImpl.java:42)
at com.hp.hpl.jena.rdf.model.impl.StmtIteratorImpl.nextStatement(StmtIteratorImpl.java:52)

I get this error on this line:

Statement statement = stmti.nextStatement();

In particular, I've seen that are loaded into the triple store many triples of this type (in substitution of others):

s: null p: http://www.w3.org/2000/01/rdf-schema#label o: null

but, my RDF file don't have these triples! Why these triples are loaded?

回答1:

I guess the code for data loading is run in a different program to the query code. If so, then

TDB.sync(dataset)

(using transactions would be better, or use the command line tool to do a bulk load).