Version 58, last updated by dbastin at July 14, 2009 UTC

In this demonstration, we are going to take a database table, EMPLOYEE, in a HSQLDB database and transform each record in the table into a line in an output CSV file called employee.csv.

The demonstration test consists of the following parts:

  • Input data: EMPLOYEE table
  • Expected output: employee.csv
  • The test itself: JdbcToCsvDemoTest.java
  • A specification class: JdbcToCsvSpecification.java
  • A wiring class: JdbcToCsvWirer.java
  • A properties file: jdbc2csv.properties

Let's go through each part in turn. Basically, we want to turn this database table...

EMPLOYEE

ID NAME SALARY START
1 Fred 1000000 2001-01-01
2 Wilma 2000000 2002-01-01
3 Barney 3000000 2003-01-01
4 Betty 4000000 null
5 Dino 0 null

...into this...

employee.csv

The JdbcToCsvDemoTest contains code that illustrates how this is achieved. The test launches the Donkey, and checks that the CSV file output matches the expected results. Pretty simple really.

JdbcToCsvDemoTest.java

You will perhaps notice the TestCase implements some additional behaviour through the HasFixtures interface. This is a feature of the testing framework that the Donkey uses, called Sniper. The fixtures() method is called by Sniper for each test in a Sniper LifecycleTestCase. In this implementation, we are creating the test EMPLOYEE table we need to slurp from.

The Donkey is launched via a Trebuchet. A Trebuchet takes a Specification. Further details regarding a Specification are here.

Here is the Specification class for this demonstration test...

JdbcToCsvSpecification.java

The Wirer contains dependency injection configuration. This wiring is quite similar to the wiring in the CsvToSqlDemoTest. You can find out a bit about it here. Burper is the name we give to transformation error handlers.

JdbcToCsvWirer.java

The configuration required by each of these implementations is contained in the jdbc2csv.properties

jdbc2csv.properties

The db and sql are required by the DatabaseSlurper.

The properties:// prefix is interpreted by the donkey and loads the key value pairs contained in the prefixed properties filename which then become available in the Donkey session through the Config instance.

The contents:// prefix is interpreted by the donkey and assigns the contents of the prefixed filename as the value to the key; so in this case the SQL contained in config/employee-select.sql is assigned to the sql key and made available to the Donkey session through the Config instance.

Transform

A Slurper reads data from the inbound end-point into a Cake. The slurped Cake is then marshaled into a Gargler where the Cake is decomposed into individual Slices. The Gargler delegates to a Transform which performs the transformations and/or mappings. The Transform effects changes on each Slice in the Cake and returns a modified copy of the slice back to the Gargler. The transformed slices are collected in a new Cake which is then marshaled on to a Spitter.

Note that we have also set up the Wirer to use a Burper that logs and then swallows any transformation errors, in order to continue processing other slices of the Cake. The default behaviour is to halt on transformation error.

This concludes the walk-through of the JDBC to CSV Demonstration Test. Check out the code and run the JdbcToCsvDemoTest.java for yourself. Rock.