06 June 2008

Building a Web Service with Java, Jena and mySQL [Part 1]

Without getting into a huge overview of the Semantic Web world, why we need it, if we need it, etc; I am going to discuss developing for it. There can be infinite objectives with semantic data. Data mining, web crawling, tag clouds, organizational tracking, data inference, wiki integration and so forth, are a few of the uses. What a developer needs to look at is how to:
  1. collect this data
  2. make it available for whatever client may be using the data.
Pushing step one to the back burner, one way to make semantic stores available is through a web service. A web service can provide us the interface so multiple and varying clients can use its functionality.

Methods to collect the data will be in following parts as we discuss a semantic web crawler, add, update, delete methods, load from RDF and database manipulation.

For our semantic purposes we need to download Jena. The Jena Semantic Web Framework is a framework API for building semantic web applications. This framework allows us to use Jena's methods to manipulate semantic stores. If you're developing for the semantic web, Jena is the foundation.

There are other components to web service development which are far beyond the scope of this article. For our purposes we're going to assume this will be running on Apache Tomcat 5.5 with AXIS2.

The other major, but optional, component is mySQL. You will need to have this running and an empty database created to start this article, if you wish to use a persistent store. Persistent stores are database backed Jena models. Additionally, the code within these posts assume you will be using a persistent model so adjust as necessary. While you're at it, pick up a copy of the mySQL JDBC driver also for the database connection.

You also need to have the Jena libs and the jdbc in your classpath.

Whew ... prerequisites done? Lets code.

First off, open a connection to the database so Jena can use it. Here is my privatized method that can be called from the various other places we will be building in the coming parts.
private static IDBConnection initDBStore() {
String dbUrl = "jdbc:mysql://YOUR_DATABASE_SERVER_IP/YOUR_DATABASE_NAME";
String dbUser = "MYSQL_USER"; // please, for all things good, don't use 'root'
String dbPass = "MYSQL_PASSWORD";
String dbType = "MySQL";
String dbDriver = "com.mysql.jdbc.Driver";

// this should almost never be set to true. Set it true if you want
// to purge out all the models
boolean cleanDB = false;

// load JDBC
try {
Class.forName(dbDriver);
IDBConnection dbcon = new DBConnection(dbUrl, dbUser,dbPass,dbType);

if (cleanDB)
dbcon.cleanDB();
return dbcon;
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;

}
Now that we know how to connect to a mysql datastore, we can do something with Jena. This method will create a Jena model in our database.
public static boolean createModel(String modelName) {
try {
IDBConnection dbcon = initDBStore();
ModelMaker maker = initModel(dbcon); // see attached code for this method.

maker.createModel(modelName, true);

maker.close();
dbcon.close();
return true;
} catch (Exception e) {
System.out.println("Model Create Failed : " + e.getMessage());
}
return false;
}
Here is part one organized into a Java file. Please note if you want to run this as a Java class you will need to add a main method in. This article intends the files to be run as a web service.

JawsPart1.java


In the continuing parts, we will be building on this file and adding in methods to load an RDF file, run SPARQL queries, insert and delete triples, and manage the server models.

0 comments: