Using JavaScript for ETL transformations
Our example transformation consists of 3 steps:
1) Select rows from source table.
2) Transform a column value from number to text
3) Insert a transformed value into a destination table.
<!DOCTYPE etl SYSTEM "http://scriptella.javaforge.com/dtd/etl.dtd"> <etl> <connection id="db" driver="auto" url="jdbc:hsqldb:mem:tst" user="sa" password="" classpath="../lib/hsqldb.jar"/> <connection id="js" driver="script"/> <connection id="log" driver="text"/> <!-- For printing debug information on the console --> <script connection-id="db"> CREATE TABLE Table_In ( Error_Code INT ); CREATE TABLE Table_Out ( Error VARCHAR(10) ); INSERT INTO Table_IN VALUES (1); INSERT INTO Table_IN VALUES (7); </script> <query connection-id="db"> SELECT * FROM Table_In <script connection-id="log"> Transforming $Error_Code </script> <!-- Transformation is described as an enclosing query which is executed before nested elements --> <query connection-id="js"> <![CDATA[ if (Error_Code < 5) { Error_Code='WARNING'; //Set a transformed value } else { Error_Code='ERROR'; //Set a transformed value } query.next(); //Don't forget to trigger nested scripts execution ]]> <script connection-id="db"> <!-- Insert transformed value --> INSERT INTO Table_Out VALUES (?Error_Code); </script> <script connection-id="log"> Transformed to $Error_Code </script> </query> </query> </etl>