Please make sure you have the appropriate libraries in your classpath (including the JDBC driver used to connect to your database) before starting the tutorials.
In this tutorial, you'll learn the basics of creating a new Column Translator and running the export from the command line. The table that will contain the rows exported is called employee and it has the following columns :
employee | |
---|---|
Name | Type |
id | number(6) |
firstname | varchar(10) |
lastname | varchar(10) |
jobdescription | varchar(10) |
managerid | number(6) |
startdate | date |
salary | number(9,2) |
department | number(6) |
Make sure that these table(s) are created in the database that you'll be exporting data and that you have several rows of data. By using the tutorial5/import.xml with the JDBCImporter, several rows can be created. You can find the oracle creation script in the samples directory under the filename : 'tutorial5/createtable_ora.sql'.
Now that the database is setup, you can examine the architecture to see how the Column Translator is used during the export.
The Column Translator is used during the export for converting the column value read from the database into a different value.
The JDBCExporter requires that each Column Translator be a Java Bean like object. All <property> tags defined inside the '<translator>' will be passed to the appropriate set method. The ColumnTranslator must also implement one method : getValue().
The Column Translator that you will be creating will reverse the characters of the string read from the database if the column is of type varchar.
The first thing to do is create the class ReverseColumnTranslator that implements the ColumnTranslator interface.
import net.sourceforge.jdbcimporter.ColumnDef; import net.sourceforge.jdbcimporter.ColumnTranslator; import net.sourceforge.jdbcimporter.ColumnValue; public class ReverseColumnTranslator implements ColumnTranslator { public ColumnValue getValue(ColumnDef column, ColumnValue columnValue) { /* @todo implement this method */ return columnValue; } }Initial Code
The Column Translator now needs to check the column type . If the column type is varchar then the Column Translator will reverse the characters in the string:
import net.sourceforge.jdbcimporter.ColumnDef; import net.sourceforge.jdbcimporter.ColumnTranslator; import net.sourceforge.jdbcimporter.ColumnValue; import net.sourceforge.jdbcimporter.engine.BasicEngine; public class ReverseColumnTranslator implements ColumnTranslator { public ColumnValue getValue(ColumnDef column, ColumnValue columnValue) { if ( Types.VARCHAR == column.getType() && columnValue.getString() != null ) { String oldValue = columnValue.getString(); StringBuffer newValue = new StringBuffer(""); for ( int i = oldValue.length() - 1; i >= 0; i-- ) { newValue.append( oldValue.charAt(i) ); } ColumnValue newColumnValue = new ColumnValue(); newColumnValue.setString( newValue.toString() ); return newColumnValue; } return columnValue; } }getValue Implementation
This ends the tutorial for creating the custom Column Translator. The full source code of the ReverseColumnTranslator is found under the package 'samples.columntranslator'. What follows now is the instructions on how to use the custom Column Translator during the export. If you have read through the first tutorial then you may wish to skip to the list of columns in the entity definition section. The other sections are the same as the first tutorial.
Now that the database is setup, you can examine the export XML config file that will be used (in the samples directory under the filename : 'tutorial5/export.xml'). The file begins with the standard XML document declaration followed by the '<export>' tag. This tag indicates that there is an export to be processed. There is one attribute specified on the '<export>' tag: the 'log' attribute. The 'log' attribute specifies a filename into which JDBCExporter writes all audit, error, and warnings that occur during the export.
There are two parts inside the '<export>' tag that define how and where the data is exported: the connection definition and the entity definitions.
The connection definition begins with '<connection>' tag and contains the information needed to connect to the database. In this tutorial, you will be using the JDBC DriverManager to initialize a connection to the database. To indicate this, the 'type' attribute's value, inside the '<connection>' tag, is 'jdbc'. The specific connection information is found inside the '<connection>' tag as '<property>' tags. A '<property>' tag has two attributes: 'name' specifies the name of the property and 'value' specifies the string value of the property. For the JDBC DriverManager, you will need to specify the following information: the driver class name (with the property name 'driver'), the connection url (with the property name 'url'), the username (with the property name 'username'), the password (with the property name 'password'). The following is an example of the connection definition :
<connection type="jdbc"> <property name="driver" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/> <property name="username" value="scott"/> <property name="password" value="tiger"/> </connection>Sample XML for JDBC Connection Def
Since you will be exporting data from one table, there will be only one entity definition. In general, you will need an entity definition for each table that you will be exporting data. Every entity definition begins with '<entity>' tag.
The 'table' attribute must contain the name of the table. Optionally, you can further specify the table by providing values for the 'schema' and the 'catalog' attributes.
To specify a custom export engine to process the entity, you may add the 'engine' attribute, whose value is the classname of the export engine.In this tutorial, you will be using the default export engine.
The 'target' attribute must contain the data file location.You will be exporting data into a csv file (called 'employee_export.csv'). The csv file will have 8 columns separated by the ',' character (similar to the file found under 'samples/tutorial1/employee.csv').
There are two parts inside the '<entity>' tag : the delimiter formatter definition and the list of columns that will be exported to the data file.
The delimiter formatter definition begins with the '<delimiter>' tag and contains the information needed to format a set of rows that will be exported from the table into the output file. In this tutorial, you will be using the CSV Delimiter Formatter. To indicate this, the 'type' attribute's value, inside the '<delimiter>' tag, is 'csv'. The specific delimiter formatter information is found inside the '<delimiter>' tag.
For the CSV Delimiter Formatter, you will need to specify the following information (as '<property>' tags): the string that delimits a column (in the property named 'columnDelimiter'), the string that encloses a column (optional, in the property named 'enclosedDelimiter'), whether the string that encloses a column is optional (in the property named 'enclosedOptional', it must have a value of 'true' or 'false'). Since, the data file will only have a column delimiter (',' is the string separating the columns), the delimiter formatter definition will look like this :
<delimiter type="csv"> <property name="columnDelimiter" value=","/> </delimiter>Sample XML for CSV Delimiter Formatter
The final portion of the entity definition is the list of columns that are to be exported from the database into the output file. The list of columns should be the same order as they will appear in the output file. Each column is defined inside the '<column>' tag. The name of the column must appear in the 'name' attribute of the '<column>' tag. Optionally, the java.sql.Type may be specified in the 'SQLType' attribute of the '<column>'.You will be letting the JDBC Exporter figure out most of the column types (except for dates) in the database, so the 'SQLType' attribute is omitted except for the 'startdate' column. Since you will be using the customer Column Translator for the 'firstname' column, you will have to specify the '<translator>' tag inside the '<column>' tag. You must choose an identifier for the custom Column Translator (ex. 'tutorial_reversechars') and set the 'type' attribute's value, inside the '<translator>' tag, to that identifier. The specific Column Translator information is found inside the '<translator>' tag. For the Reverse Column Translator, there are no properties to set. Here is an example of how the list of columns are defined in the export definition:
<column name="id"></column> <column name="firstname"> <translator type="tutorial_reversechars"> </translator> </column> <column name="lastname"></column>Sample XML for List of Columns
By now, the export definition should look like this (with your appropriate connection information):
<export log="export.log > <connection type="jdbc"> <property name="driver" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/> <property name="username" value="scott"/> <property name="password" value="tiger"/> </connection> <entity table="employee" target="employee_export.csv"> <delimiter type="csv"> <property name="columnDelimiter" value=","/> </delimiter> <column name="id"></column> <column name="firstname"> <translator type="tutorial_reversechars"> </translator> </column> <column name="lastname"></column> <column name="jobdescription"></column> <column name="managerid"></column> <column name="startdate" SQLType="DATE"></column> <column name="salary"></column> <column name="department"></column> </entity> </export>Sample XML for Tutorial 5
Since you are using the custom Column Translator (Reverse Column Translator) you will have to create a property file with one entry that maps the identifier to the full name of the class that implements the ColumnTranslator interface. The entry's key should start with 'columntranslator.' (this indicates that the custom component is a Column Translator). It should look like this:
columntranslator.tutorial_reversechars=samples.columntranslator.ReverseColumnTranslatorProperty File Entry for XML Delimiter Parser
You will also have to include it in the classpath before you run the export (the jar file 'jdbcimporter-samples.jar' under the directory 'lib' contains the custom column translator). You can run the export by issuing the following command (assuming that the export definition and the property file are in the current directory and are called 'export.xml' and 'custom_export.properties'):
java net.sourceforge.jdbcexporter.Exporter export.xml custom_export.properties
If all goes well then the log file and the 'employee_export.csv' file should be created. In the log file there should be an informational message indicating that all rows were exported.