Hello again!
I need to do next staff: read all "name" fields from MY_TDB, do some
calculations with this "name" and save results to MY_TDB.
But I need to read and save on the fly, eg read on "name" -> do calculaions
-> save results -> read next "name" -> calculations -> save result ...
Here is the example code:
public class TestTdbTransactionsReadWrite {
public static void main(String[] args) {
Dataset dataset = TDBFactory.createDataset();
dataset.begin(ReadWrite.WRITE);
DatasetGraph datasetGraph = dataset.asDatasetGraph();
Graph graph = datasetGraph.getDefaultGraph();
// Fill graph.
graph.add(
new Triple(
NodeFactory.createURI("http://example/unit13"),
NodeFactory.createURI("http://example/name"),
NodeFactory.createLiteral("Unit 13", "en")
)
);
graph.add(
new Triple(
NodeFactory.createURI("http://example/unit13"),
NodeFactory.createURI("http://example/type"),
NodeFactory.createURI("http://example/robot")
)
);
graph.add(
new Triple(
NodeFactory.createURI("http://example/unit13"),
NodeFactory.createURI("http://example/creationYear"),
NodeFactory.createURI("http://example/2015")
)
);
// Test.
String qs1 = "SELECT * { ?s <http://example/name> ?o }";
try (QueryExecution qExec = QueryExecutionFactory.create(qs1,
dataset)) {
ResultSet rs = qExec.execSelect();
while (rs.hasNext()) {
QuerySolution qs = rs.next();
RDFNode nodeSubject = qs.get("s");
RDFNode nodeObject = qs.get("o");
if (nodeSubject.isURIResource() && nodeObject.isLiteral()) {
String str = nodeObject.asLiteral().getString();
graph.add(
new Triple(
NodeFactory.createURI(nodeSubject.asResource().getURI()),
NodeFactory.createURI("http://example/value"),
NodeFactory.createLiteral(String.valueOf(str.length()))
)
);
}
}
dataset.commit();
} finally {
dataset.end();
}
RDFDataMgr.write(System.out, graph, RDFFormat.NTRIPLES);
}
}
https://github.com/Hronom/test-jena