Apache Jena is an Open Source Framework for building Semantic Web and Linked Data applications. Jena provides an API to help write Java code that handles RDF, RDFS, RDFa, OWL and SPARQL. It supports serialisation of RDF graph for:
- relational databases
- RDF/XML
- Turtle
- Notation 3
A similar framework RDF4J (formerly known as Sesame) queries, processes and analyses RDF constructs (it supports SPARQL and SeEQL), however it does not currently support OWL. This tutorial will be a step-by-step guide on how to attach Jena to an Eclipse project in order to write and print out a sample RDF statement. Step 1 – Create a new Java Project in Eclipse First of all, you will need to create a new Java project in Eclipse. This can be done by selecting File -> New-> Java Project. You will be prompted with the below window where you will specify the name of your project and the chosen execution environment JRE (in our example, we used 1.7): Step 2 – Create your sample Class Create a sample Class called MyFirstRDFStatement from Jena-Tutorial-Project -> New -> Class. Initially, this class will only include the Class declaration and main method, but we will later be enriched with the necessary code to create a RDF statement and output it as Turtle format.
Step 3 Adding the Jena Libraries to your Eclipse Project In order to start writing and reading RDF statements, we need to import the Jena Libraries into our project. Go to Window -> Preferences -> BuildPath -> UserLibraries:
You would have already downloaded the Jena Libraries from here. Create a New User Libraries by clicking on New and specifying your custom name:
From the left click on ADD External JARs and navigate to the location where your libraries are located (in my case, they were placed in my Download folder):
Step 4: Create an empty Model with ModelFactory The first element you will need to include in your MyFirstRDFStatement class is a
Model
. In Jena terms, a Model is a set, or better, a container for RDF statements. The Model
interface is contained in the package com.hp.hpl.jena.rdf.model
and it provides methods for creating resources, properties, literals and statements. In our example, the class ModelFactory provides methods for creating a standard kind of Model such as createDefaultModel, which simply creates a fresh Model with some default specification: It should be noted that our Model is currently ’empty’, therefore we would need to create the resource and add its property. Step 5: Create the Resource and its Property In order to create the Resource and Property, you will need to use the Model methods
createResource
and createProperty
, as follows: Step 6: Write a serialised representation of the Model The last step of our tutorial consists in outputting the Model in a chosen format – in our case Turtle. We will employ the Model method to write with the following signature :
Model write(OutputStream out, String lang)
Although our statement has been correctly printed out (<http://www.johnwebpage.com> <John Smith> “this is a property”^^<http://www.w3.org/2001/XMLSchema#string> .), 3 red lines come up in the console (which have been deliberately left in the screenshot) and they refer to an issue concerning Log4J. Jena uses Log4J as a logging system and this error is caused by the fact that the configuration file log4j.properties cannot be found. It is very likely you will encounter the same issue, so have a look at these resources:
http://stackoverflow.com/questions/20365204/configure-eclipse-for-log4j
http://www.mkyong.com/logging/log4j-log4j-properties-examples/
http://logging.apache.org/log4j/1.2/faq.html#noconfig
Probably, the easiest way to fix an issue with Log4J is to follow these simple steps:
- Find a properties file named jena-log4j.properties and copy it;
- Navigate to your workspace folder where you have your source project;
- Rename the file to just log4j.properties;
- Go to bin folder and paste the file there
- Clean the project and run it again.
The errors should have gone and you should be returned with the following:
Leave a comment
Comments 0