RDF FOAF Contact Data

RDB To RDF

A Java program to read data based on an SQL query and output the information as RDF triples

This Java program loads an ontology that is in RDF/XML, N3 or Turtle format, connects to a database, runs a SQL statement as configured by the user, creates RDF triples that are combined with the loaded ontology, infers over the graph and then writes out the resulting triples to a file. You may download the entire ant-based project (contains all required libraries) or just the source code file. If you download just the source, you will need Jena and Pellet to compile and run the program. See the tools section on my main semantic web page for links to those packages.

You will also need access to a relational database and its JDBC driver in order to use this program.

Program Operation

The easiest way to run the program is to download the full package, unzip the archive, bring up a command prompt, switch to the base directory of the exploded archive, configure the properties file to point to your database, add the JDBC JAR file to the db-lib directory and use ant to execute the run target. Here are a few lines that are output when running the program using the ant run target:

=====================================================================
Buildfile: build.xml

init:

compile:

distjar:

run:
     [java] Using default input file: DSRRdbToRdfTestOntology.turtle
     [java] Defaulting output file name to: DSRRdbToRdfTestOntology.turtle.out
     [java] Loaded Configuration
     [java]   Input File: DSRRdbToRdfTestOntology.turtle
     [java]   Output File: DSRRdbToRdfTestOntology.turtle.out
     [java] 
     [java]   Driver Class: org.gjt.mm.mysql.Driver
     [java]   Connection URL: jdbc:mysql://localhost:3306/sakila
     [java]   User Id: sakila
     [java]   PK Column Label: film_id
     [java] 
     [java]   Data Class: http://monead.com/semantic/education#RdbData
     [java]   Instance Name Prefix: PK_
     [java]   Namespace: http://monead.com/semantic/education#
     [java] 
     [java]   Null PK Value Flag: NULL_PK
     [java]   Null Value Flag: NULL
     [java] 
     [java] Load model with reasoner: owl
     [java] Loaded model DSRRdbToRdfTestOntology.turtle using format: Turtle
     [java] Row PK: film_id=1
     [java] Add subject as class instance: 1
     [java] Add statement to model: http://monead.com/semantic/education#PK_1->http://monead.com/semantic/education#title->ACADEMY DINOSAUR
     [java] Add statement to model: http://monead.com/semantic/education#PK_1->http://monead.com/semantic/education#description->A Epic Drama of a Feminist And a Mad Scientist who must Battle a Teacher in The Canadian Rockies
...
     [java] Write loaded data to file, DSRRdbToRdfTestOntology.turtle.out, in format: Turtle
     [java] Classifying 5 elements
     [java] 
     [java] Classifying:  20% complete in 00:00Classifying:  40% complete in 00:00Classifying:  60% complete in 00:00Classifying:  80% complete in 00:00Classifying:  100% complete in 00:00Classifying finished in 00:00
     [java] Realizing 5 elements
     [java] 
     [java] Realizing:  20% complete in 00:00Realizing:  40% complete in 00:00Realizing:  60% complete in 00:00Realizing:  80% complete in 00:00Realizing:  100% complete in 00:00Realizing finished in 01:29

BUILD SUCCESSFUL
Total time: 5 minutes 23 seconds
=====================================================================

The main points we see in the output is that the initial model is setup using the ontology file, triples are asserted based on the rows returned by the SQL statement, inferencing is run (note it took 5 minutes to inference over this dataset with the restrictions I added in the ontology file) and finally the data is written to the output file.

The output will be written to a file with the same name and format as the input file but with the suffix .out appended. Below is some of the generated output (shown using Turtle) using the "film" table from the Sakila sample database from MySQL and a query of "select * from film":

=====================================================================
@prefix rdfs:     .
@prefix owl:      .
@prefix xsd:      .
@prefix rdf:      .
@prefix dsr:      .
@prefix dsr2:     .

dsr:PK_63
      a       owl:Thing , dsr:RdbData , dsr2:ImportedData ;
      rdfs:label "63" ;
      dsr:language_id "1" ;
      dsr:last_update "2006-02-15 05:03:42" ;
      dsr:length "73" .

dsr:PK_628
      a       owl:Thing , dsr:RdbData , dsr2:ImportedData ;
      rdfs:label "628" ;
      dsr:language_id "1" ;
      dsr:last_update "2006-02-15 05:03:42" ;
      dsr:length "172" .
=====================================================================

The sample output was greatly trimmed down from the actual output from the sample query results.

Source Code

Feel free to download, use and modify this code. The intent is to help understand how to load ontologies, convert RDB data to RDF and transform RDF data using semantic web technologies.