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.
1 2 <!DOCTYPE etl SYSTEM "http://scriptella.javaforge.com/dtd/etl.dtd"> 3 <etl> 4 <connection id="db" driver="auto" url="jdbc:hsqldb:mem:tst" user="sa" password="" classpath="../lib/hsqldb.jar"/> 5 <connection id="js" driver="script"/> 6 <connection id="log" driver="text"/> <!-- For printing debug information on the console --> 7 8 <script connection-id="db"> 9 CREATE TABLE Table_In ( 10 Error_Code INT 11 ); 12 CREATE TABLE Table_Out ( 13 Error VARCHAR(10) 14 ); 15 16 INSERT INTO Table_IN VALUES (1); 17 INSERT INTO Table_IN VALUES (7); 18 </script> 19 20 <query connection-id="db"> 21 SELECT * FROM Table_In 22 <script connection-id="log"> 23 Transforming $Error_Code 24 </script> 25 <!-- Transformation is described as an enclosing query 26 which is executed before nested elements --> 27 <query connection-id="js"> 28 <![CDATA[ 29 if (Error_Code < 5) { 30 Error_Code='WARNING'; //Set a transformed value 31 } else { 32 Error_Code='ERROR'; //Set a transformed value 33 } 34 query.next(); //Don't forget to trigger nested scripts execution 35 ]]> 36 <script connection-id="db"> 37 <!-- Insert transformed value --> 38 INSERT INTO Table_Out VALUES (?Error_Code); 39 </script> 40 <script connection-id="log"> 41 Transformed to $Error_Code 42 </script> 43 </query> 44 </query> 45 </etl>