Donnerstag, 17. Januar 2013

Access Triple Store via Java

The easiest way to access triple stores via Java is to use Jena ARQ.
ARQ is a query engine for Jena that supports the SPARQL RDF Query language. SPARQL is the query language developed by the W3C RDF Data Access Working Group.
You can easily access the data in the store using SELECT, ASK, DESCRIBE, and CONSTRUCT queries.
Query query = QueryFactory.create("queryString");
QueryExecution queryExec = QueryExecutionFactory.sparqlService( endpoint, query );
ResultSet result = queryExec.execSelect(); // or execSelectTriples()
It results in a ResultSet or an Interator of triples which can easily be processed further.
while (result.hasNext()) {
    QuerySolution solution = result.next();
    Resource id = solution.getResource("id");
    Resource title = solution.getLiteral("title");
}
One QuerySolution thereby is one result row of the query and each cell can be accessed by the variable name used in the SPARQL query.

But sometimes you do not want to just extract data from your triple store, but INSERT, UPDATE or DELETE triples. This can also be done using Jena.
UpdateRequest update = UpdateFactory.create(queryString);           
UpdateProcessor uExec = UpdateExecutionFactory.createRemote(update, endpoint);
uExec.execute(); 
Attention: This just works with triple stores supporting SPARQL 1.1 UPDATE.

Keine Kommentare:

Kommentar veröffentlichen