Jena API is one of the most commonly used API for handling RDF. It allows for creating different types of RDF models which includes: memory models, file-based models, inferencing models and database-backed models. The older versions of Jena used to be shipped with database subsystem that supported database-backed models . Jena authors later shifted two SDB and TDB. SDB uses an SQL database fir the storage and query of RDF data. As per Jena SDB documentation the use of SDB in new applications is not recommended as it may be pulled from the main build at very short notice. Developers are encouraged to use TDB which is a non-transactional, faster database soultion. This post explains the setup of SDB and how it is used for storing a simple RDF file in a MySQL database.

For this I will assume that you have the latest release of Jena (Jena 2.11.0). I will also assume that you have loaded your IDE and imported Jena API into you newly created project.

The first thing you will need to do is to setup a mysql database. In my case I created a mysql database named “rdfplay”. Now we have the database ready, you should download the maintenance release of SDB. Save and extract the files to you favorite location. Form here on I will assume that you extracted SDB to “/path/to/SDB”. Under this folder you will find a “lib” directory which contains the necessary jar files for running SDB tools. Get your hand into a copy of mysql JDBC driver jar file and place it under “lib” . We will need this we creating the format of our RDF model in MySQL. Next you need to create the store description file for MySQL which will look like this:

@prefix sdb:     <http://jena.hpl.hp.com/2007/sdb#> .
@prefix rdfs:	 <http://www.w3.org/2000/01/rdf-schema#> .
@prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix ja:      <http://jena.hpl.hp.com/2005/11/Assembler#> .

# MySQL - InnoDB

rdf:type sdb:Store ;
sdb:layout "layout2" ;
sdb:connection ;
sdb:engine "InnoDB" ; # MySQL specific
.

rdf:type sdb:SDBConnection ;
sdb:sdbType "MySQL" ; # Needed for JDBC URL
sdb:sdbHost "localhost" ;
sdb:sdbName "rdfplay" ; # database name
sdb:driver "com.mysql.jdbc.Driver" ;
sdb:sdbUser "rdfplay" ;
sdb:sdbPassword "password";
.

Save this file with the name sdb-mysql-innodb.ttl in the SDB folder. Next you need to configure a couple environment variables for creating before configuring the RDF store.

$ export SDBROOT=/path/to/sdb
$ export PATH=$SDBROOT/bin:$PATH
$ export SDB_JDBC=com.mysql.jdbc.Driver

Then you need to run the following to initialize the database. Be aware that this will wipe any existing data in the database. So you may need to check first.


$ sdbconfig --sdb sdb-mysql-innodb.ttl --format

This will create a basic layout in you database. Four tables should be created given that we have used layout2 in our store description file. The tables are: Nodes, Prefixes, Quads and Triples. Now we are ready to load some RDF to our database. Copy the following codes to eclipse and run it. The code simply, connects to the database store, checks of the model in the database (using the URI of the RDF file), if not it reads the RDF in an in-memory model and then adds it to the store. Otherwise it fetches the model from the store. The last statement prints the statements in the model. The code is pretty much self explanatory. Happy coding.

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.sdb.SDBFactory;
import com.hp.hpl.jena.sdb.Store;
import com.hp.hpl.jena.util.PrintUtil;

public class DBModelTester {

	private static String MODEL_URI = "http://example.me/foaf.rdf";

	public static void main(String[] args) throws Exception {

		// connect to a store
		Store store = SDBFactory.connectStore("sdb-mysql-innodb.ttl");

		// connect to an RDF dataset in the store
		Dataset ds = SDBFactory.connectDataset(store);

		// here is our mode
		Model m = null;

		// check the dataset if it contains our model
		if (!ds.containsNamedModel(MODEL_URI)) { // if not
			
			System.out.println("Loading Instance --- done once");
			
			// create an in memory model
			m = ModelFactory.createDefaultModel();
			
			// read the RDF in my model
			m.read(MODEL_URI);
			
			// add it to the data sore
			ds.addNamedModel(MODEL_URI, m);
		} else { // already in the story - just fetch it
			m = ds.getNamedModel(MODEL_URI);
		}

		// print the model
		printStatements(m, null, null, null);
		
		// close the data sore
		ds.close();

	}

	private static void printStatements(Model m, Resource s, Property p,
			Resource o) {

		for (StmtIterator iter = m.listStatements(s, p, o); iter.hasNext();) {
			Statement stmt = iter.next();

			System.out.println(" - " + PrintUtil.print(stmt));
		}

	}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s