Escolar Documentos
Profissional Documentos
Cultura Documentos
Quick links
Home Downloads Help and support Report a bug Roadmap Getting involved Documentation
Documentation
Overview Javadoc RDF SPARQL (ARQ) o Application API o Command line utilities TDB o API for Transactions o Dataset Assembler SDB LARQ Fuseki: Serving Data Ontology Inference Assembler o Assembler how-to o Inside assemblers I/O o I/O how-to
RIOT Concurrency how-to Event handler how-to File manager how-to Model factory how-to RDF frames Reification how-to Typed literals how-to Support for IRI's schemagen eyeball
Notes
o o o o o o o o
Tools
o o
ASF links
SDB JavaAPI
This page describes how to use SDB from Java. Code examples are in src-examples/ in the SDB distribution.
Contents
Concepts Obtaining the Store o From a configuration file o In Java code o Database User and Password Connection Management Formatting or Emptying the Store Loading data Executing Queries Using the Jena Model API with SDB
Concepts
SDB loads and queries data based on the unit of a Store. The Store object has all the information for formatting, loading and accessing an SDB database. One database or table space is one Store. Store objects are made via the static method of the StoreFactory class.
SDBConnection
operations.
StoreDesc
A store description is the low level mechanism for describing stores to be created.
DatasetStore GraphSDB
Two further class are not immediately visible because they are managed by the SDBFactory which creates the necessary classes, such as Jena models and graphs. An object of class DatasetStore represents an RDF dataset backed by an SDB store. Objects of this class trigger SPARQL queries being sent to SDB. The class GraphSDB provides the adapter between the standard Jena Java API and an SDB store, either to the default graph or one of the named graphs. The SDBFactory can also create Jena Models backed by such a graph.
The assembler file has two parts, the connection details and the store type.
_:c rdf:type sdb:SDBConnection ; sdb:sdbType "derby" ; sdb:sdbName "DB/SDB2" ; sdb:driver "org.apache.derby.jdbc.EmbeddedDriver" ; . [] rdf:type sdb:Store ; sdb:layout "layout2" ; sdb:connection _:c ; .
See the full details of store description files for the options.
In Java code
The less flexible way to create a store description is to build it in Java. For example:
StoreDesc storeDesc = new StoreDesc(LayoutType.LayoutTripleNodesHash, DatabaseType.Derby) ; JDBC.loadDriverDerby() ; String jdbcURL = "jdbc:derby:DB/SDB2"; SDBConnection conn = new SDBConnection(jdbcURL, null, null) ; Store store = SDBFactory.connectStore(conn, storeDesc) ;
Connection Management
Each store has a JDBC connection associated with it. In situations where such connections are managed externally, the store object can be created and used within a single operation. A Store is lightweight and does not perform any database actions when created, so creating and releasing them will not impact performance. Closing a store does not close the JDBC connection.
Similarly, a SDBConnection is lightweight and creation does not result in any database or JDBC connection actions. The store description can be read from the same file because any SDB connection information is ignored when reading to get just the store description. The store description can be kept across store creations:
storeDesc = StoreDesc.read("sdb.ttl") ;
then used with an JDBC connection object passed from the connection container:
public static void query(String queryString, StoreDesc storeDesc, Connection jdbcConnection) { Query query = QueryFactory.create(queryString) ; SDBConnection conn = SDBFactory.createConnection(jdbcConnection) ; Store store = SDBFactory.connectStore(conn, storeDesc) ; Dataset ds = SDBFactory.connectDataset(store) ; QueryExecution qe = QueryExecutionFactory.create(query, ds) ; try { ResultSet rs = qe.execSelect() ; ResultSetFormatter.out(rs) ; } finally { qe.close() ; } store.close() ; }
This is an expensive operation, and should be used sparingly. Once you obtain a store for the first time you will need to:
store.getTableFormatter().create();
This will create the necessary tables and indexes required for a full SDB store. You may empty the store completely using:
store.getTableFormatter().truncate();
Loading data
Data loading uses the standard Jena Model.read operations. GraphSDB, and models made from a GraphSDB, implement the standard Jena bulk data interface with backed by an SBD implementation of that interface.
Executing Queries
The interface to making queries with SDB is same as that for querying with ARQ. SDB is an ARQ query engine that can handle queries made on an RDF dataset which is of the SDB class DatasetStore:
Dataset ds = DatasetStore.create(store) ;
When finished, the store should be closed to release any resources associated with the particular implementation. Closing a store does not close it's JDBC connection.
store.close() ;
If models or graphs backed by SDB are placed in a general Dataset then the query is not efficiently executed by SDB.
SDB is optimized for SPARQL queries but queries and other Jena API operations can be mixed. The results from a SPARQL query are Jena RDFNodes, with the associated model having a graph implemented by SDB. Copyright 201112 The Apache Software Foundation, Licensed under the Apache License, Version 2.0. Apache Jena, Jena, the Apache Jena project logo, Apache and the Apache feather logos are trademarks of The Apache Software Foundation.