Xaj: Mondial Example with Berkeley DB XML

This guide shows an example of using Xaj with the Berkeley DB XML database by loading the Mondial XML dataset into a Berkeley DB XML enviornment using Xaj and then querying that database with XQuery.

At a minimum you'll need the following to be able to follow this guide:

  1. The Xaj source code, checked out from Subversion. See the Building from source guide for information on this.
  2. Berkeley DB XML 2.3
  3. A Java 5 or later runtime.
  4. Apache Ant 1.6 or later.

Compile the Xaj JAR

First you need to build the Xaj JAR. Follow the steps in the build guide, following the steps outlined for the Berkeley DB XML support (i.e. be sure to pass the -Dwith.bdb=true flag to Ant). It should look similar to this:

~/xaj matt$ ant -Dwith.bdb=true jar Buildfile: build.xml prepare.compile: [mkdir] Created dir: xaj/build/ant/bin [mkdir] Created dir: xaj/build/ant/src compile.core: [javac] Compiling 35 source files to xaj/build/ant/bin [copy] Copying 35 files to xaj/build/ant/src compile.bdb: [javac] Compiling 11 source files to xaj/build/ant/bin [copy] Copying 11 files to xaj/build/ant/src jar: [jar] Building jar: xaj/dist/xaj.jar BUILD SUCCESSFUL Total time: 5 seconds

Load the Mondial data

This data is available in the defs/examples/data directory in the Xaj source distribution. To load it, we'll use a simple tool included with Xaj.

  1. Change into the defs/examples/bdb directory.
  2. Copy the mondial-loader.properties file to env.properties.
  3. Run the mondial-loader Ant task.

The whole thing should look similar to this:

~/xaj/defs/examples/bdb matt$ cp mondial-loader.properties env.properties ~/xaj/defs/examples/bdb matt$ ant mondial-loader Buildfile: build.xml mondial-loader: [mkdir] Created dir: xaj/defs/examples/bdb/mondial [java] INFO Initializing BDB Environment [xaj/defs/examples/bdb/mondial] [java] INFO DbEnvironment version = Berkeley DB 4.5.20: (September 20, 2006) [java] INFO DB XML version = Oracle: Berkeley DB XML 2.3.10: (January 30, 2007) ... [java] INFO Stored batch XML 700: dbxml_701 [java] INFO Stored batch XML 800: dbxml_801 [java] INFO Stored batch XML 900: dbxml_901 [java] INFO Loaded 974 XML documents from [xaj/defs/examples/data/mondial.xml.gz] BUILD SUCCESSFUL Total time: 1 minute 8 seconds

Query the Mondial data

Now that you've loaded the Mondial data, you can query it with another simple tool that is included with Xaj. The net.sf.xaj.support.XQueryTool class is a simple tool to query a Xaj database.

  1. Run the mondial-query Ant task to generate the Java command line arguments needed to run the tool.
  2. Copy the generated command line, appending -query and the desired XQuery to run.

The whole thing should look similar to this (note that the results have been wrapped for display here, but should be treated as non-wrapped lines when you execute them):

~/xaj/defs/examples/bdb matt$ ant mondial-query Buildfile: build.xml mondial-query: [echo] java -classpath xaj/lib/args4j-2.0.8.jar:xaj/lib/commons-logging.jar: xaj/lib/log4j-1.2.13.jar:xaj/lib/spring-1.2.8.jar:xaj/lib/stax-api-1.0.1.jar: xaj/lib/stax-utils.jar:xaj/lib/wstx-asl-3.1.1.jar:xaj/dist/xaj.jar: xaj/environment/local/db.jar:xaj/environment/local/dbxml.jar:xaj/defs/examples/data: xaj/defs/examples/bdb net.sf.xaj.support.XQueryTool -config mondial-XmlDbContext.xml -db mondialXmlDb -txnTmpl bdbTransactionTemplate BUILD SUCCESSFUL Total time: 1 second ~/xaj/defs/examples/bdb matt$ java -classpath xaj/lib/... net.sf.xaj.support.XQueryTool -config mondial-XmlDbContext.xml -db mondialXmlDb -txnTmpl bdbTransactionTemplate -query 'for $name in collection("mondial.dbxml")/country/name order by $name return $name' INFO Initializing BDB Environment [xaj/defs/examples/bdb/mondial] INFO DbEnvironment version = Berkeley DB 4.5.20: (September 20, 2006) INFO DB XML version = Oracle: Berkeley DB XML 2.3.10: (January 30, 2007) ... 1) <name>Afghanistan</name> 2) <name>Albania</name> 3) <name>Algeria</name> 4) <name>American Samoa</name> 5) <name>Andorra</name> ... 256) <name>Western Samoa</name> 257) <name>Yemen</name> 258) <name>Zaire</name> 259) <name>Zambia</name> 260) <name>Zimbabwe</name> Found 260 results in 269ms

Setting up a Xaj server with Berkeley DB XML

You can also configure Xaj as a server with Berkeley DB XML as the backend database. In this example, we'll deploy Xaj into the Apache Tomcat servlet container.

  1. Download and install Apache Tomcat (version 5.5 or later recommended).
  2. Copy your db.jar and dbxml.jar files from your Berkeley DB XML installation into TOMCAT_HOME/common/lib.
  3. Copy the mondial-server-XmlDbContext.xml configuration file from the defs/examples/bdb directory into environment/local.
  4. Copy the env.properties file from the defs/example/bdb directory into environment/local. This assumes you created this file as previously described in this guide. You might need to modify the cache.dir property to a file path suitable for your machine.
  5. Build the Xaj WAR by running the dist-war Ant task.
  6. Copy the generated dist/xaj.war into your Tomcat webapps directory.
  7. Start Tomcat.
  8. Open a web browser and go to the /xaj/admin/xquery.do URL, using your local Tomcat server port, e.g. http://localhost:8080/xaj/admin/xquery.do.

The steps should look something like this:

~/xaj/defs/examples/bdb matt$ cp mondial-server-XmlDbContext.xml ../../../environment/local ~/xaj/defs/examples/bdb matt$ cp env.properties ../../../environment/local ~/xaj/defs/examples/bdb matt$ cd ../../.. ~/xaj matt$ ant -Dwith-bdb=true dist-war Buildfile: build.xml clean: [delete] Deleting directory xaj/build/ant ... prepare-war: [mkdir] Created dir: xaj/build/ant/web [copy] Copying 23 files to xaj/build/ant/web/WEB-INF/lib [jar] Building jar: xaj/build/ant/web/WEB-INF/lib/xaj.jar [copy] Copying 12 files to xaj/build/ant/web/WEB-INF/classes [copy] Copying 7 files to xaj/build/ant/web/WEB-INF [copy] Copying 18 files to xaj/build/ant/web dist-war: [zip] Building zip: xaj/dist/xaj.war BUILD SUCCESSFUL Total time: 9 seconds ~/xaj matt$ cd ../apache-tomcat-5.5.23 ~/apache-tomcat-5.5.23 matt$ bin/catalina.sh start Using CATALINA_BASE: apache-tomcat-5.5.23 Using CATALINA_HOME: apache-tomcat-5.5.23 Using CATALINA_TMPDIR: apache-tomcat-5.5.23/temp Using JRE_HOME: /System/Library/Frameworks/JavaVM.framework/Home

After a brief moment, you should be able to visit http://localhost:8080/xaj/admin/xquery.do in your web browser, which should look like this:

Xaj XQuery Admin Tool
The Xaj web server XQuery Admin Tool

In order to query the Mondial database, change the Service key to mondial. Now you can execute the same XQuery you tried before, using the command-line tool. It should look like this:

Xaj XQuery Admin Tool example results
Results of executing an XQuery against the Mondial database

Where to go from here

Check out the source for the net.sf.xaj.support.XmlLoaderTool and the net.sf.xaj.support.XQueryTool to see how these simple command-line tools use the Xaj API for loading and querying the XML data. The defs/examples/bdb/mondial-XmlDbContext.xml Spring configuration shows how the Xaj Berkeley DB XML environment is configured.

SourceForge.net Logo