To define the Java transformation functionality, you enter Java code snippets on the Java tab. Enter code snippets to import Java packages, write helper code, and write Java code that defines the behavior for specific transformation events. You can develop code snippets in the Java editor in any order.
Enter Java code snippets in the following sections of the Java editor:
Define variables and methods available to all sections except Import Packages.
On Input Row
Define the transformation behavior when it receives an input row.
At End of Data
Define the transformation behavior when it has processed all input data.
On Receiving Transaction
Define the transformation behavior when it receives a transaction notification. Use with active Java transformations.
You cannot use the On Receiving Transaction section in advanced mode.
Access input data and set output data in the On Input Row section. For active transformations, you can also set output data in the At End of Data and On Receiving Transaction sections.
Creating Java code snippets
To create Java code snippets that define transformation behavior, use the Java editor on the Java tab.
The following image shows the Java tab with the Java editor expanded:
1Inputs, Outputs, and APIs tabs. Use these tabs to add input and output fields as variables and to call API methods in the Java code snippets. The fields and methods displayed on these tabs vary based on which section of the code entry area is selected.
2Go to list. Use to switch among the sections in the code entry area.
3Minimize, Open Both, and Maximize icons. Use the Minimize and Maximize buttons to minimize and maximize the transformation properties. Use the Open Both icon to open the Mapping Designer canvas and the transformation properties at the same time.
4Code entry area. Enter Java code snippets in the Import Packages, Helper Code, On Input Row, At End of Data, and On Receiving Transaction sections.
5Compilation results. Expand the compilation results to see detailed compilation results, compilation errors, and view the full code.
Tip: To expand the transformation properties so that you can see the code entry area more fully, click Maximize.
1In the Go to list, select the section in which you want to enter a code snippet.
2To access an input or output field in the snippet, select the field on the Inputs or Outputs tab, and click Add.
You can also create output fields on the Outputs tab by clicking Create New Field.
3To call a Java transformation API method in the snippet, select the method on the APIs tab, and click Add.
The methods displayed on the APIs tab change based on which section is selected. For example, you can use the getInRowType method only in the On Input Row section but not in other sections. Therefore, this method is listed only when the On Input Row section is selected.
4If necessary, configure the method input values.
5Write appropriate Java code based on the section.
After you finish creating the Java code snippets, compile the code to validate the transformation.
Importing Java packages
You can import Java packages for active or passive Java transformations. Import packages in the Import Packages section of the Java editor.
For example, to import the Java I/O package, enter the following code in the Import Packages section:
import java.io.*;
You can import built-in, third-party, or custom Java packages. If you import third-party or custom Java packages, you must add the packages to the classpath. For more information about configuring the classpath, see Classpath configuration.
After you import Java packages, you can use the imported packages in the other sections.
You cannot declare or use static variables, instance variables, or user methods in the Import Packages section.
Note: When you export a mapping or mapping task that contains a Java transformation, the jar or class files that contain the third-party or custom packages required by the Java transformation are not included in the export XML file. Therefore, when you import the mapping or task, you must copy the jar or class files that contain the required third-party or custom packages to the Secure Agent machine.
Defining helper code
You can declare user-defined variables and methods for the Java transformation class in active or passive Java transformations. Define helper code in the Helper Code section of the Java editor.
After you declare variables and methods in the Helper Code area, you can use them in any code entry area except the Import Packages area.
You can declare the following types of code, variables, and methods:
Static code and static variables
Within a static block, you can declare static variables and static code. Static code runs before any other code in a Java transformation.
For example, the following code declares a static variable to store the error threshold for a Java transformation:
static int errorThreshold;
User-defined static or instance methods
These methods extend the functionality of the Java transformation. Java methods that you declare in the Helper Code section can use or modify output variables. You cannot access input variables from Java methods in the Helper Code section.
For example, use the following code in the Helper Code section to declare a function that adds two integers:
private int myTXAdd (int num1,int num2) { return num1+num2; }
Defining input row behavior
You can define the behavior of the Java transformation when it receives an input row. Define input row behavior in the On Input Row section of the Java editor. The Java code in this section executes one time for each input row.
Access and use the following input and output field data, variables, and methods in the On Input Row section:
Input field and output field variables
Access input and output field data as a variable by using the name of the field as the name of the variable. For example, if “in_int” is an integer input field, you can access the data for this field by referring as a variable “in_int” with the Java primitive data type int. You do not need to declare input and output fields as variables.
Do not assign a value to an input field variable. If you assign a value to an input variable in the On Input Row section, you cannot get the input data for the corresponding field in the current row.
Static variables and user-defined methods
Use any static variable or user-defined method that you declared in the Helper Code section.
For example, an active Java transformation has two input fields, BASE_SALARY and BONUSES, with an integer data type, and a single output field, TOTAL_COMP, with an integer data type. You create a user-defined method in the Helper Code section, myTXAdd, that adds two integers and returns the result.
Use the following Java code in the On Input Row section to assign the total values for the input fields to the output field and generate an output row:
When the Java transformation receives an input row, it adds the values of the BASE_SALARY and BONUSES input fields, assigns the value to the TOTAL_COMP output field, and generates an output row.
Java transformation API methods
You can call API methods provided by the Java transformation.
Defining end of data behavior
You can define the behavior of an active or passive Java transformation when it has processed all input data. Define end of data behavior in the At End of Data section of the Java editor.
To generate output rows in the At End of Data section, set the transformation scope for the transformation to Transaction or to All Input on the Advanced tab. You cannot access or set the value of input field variables in this section.
Access and use the following variables and methods in the At End of Data section:
Output field variables
Use the names of output fields as variables to access or set output data for active Java transformations.
User-defined methods
Use any user-defined method that you declared in the Helper Code section.
Java transformation API methods
Call API methods provided by the Java transformation. For example, use the following Java code to write information to the session log when the end of data is reached:
logInfo("Number of null rows for partition is: " + partCountNullRows);
If you use the Java transformation in advanced mode, consider the following guidelines:
•You cannot write output to standard output, but you can write output to standard error which appears in the log files.
•You cannot pass binary null characters to an output field.
To avoid a mapping failure, you can add code to the Java transformation that replaces the binary null characters with an alternative character before writing the data to the output field.
Defining transaction notification behavior
You can define the behavior of an active Java transformation when it receives a transaction notification. Define the transaction notification behavior in the On Receiving Transaction section of the Java editor.
The code snippet in the On Receiving Transaction section is only executed if the Transaction Scope for the transformation is set to Transaction. You cannot access or set the value of input field variables in this section.
Access and use the following output data, variables, and methods from the On Receiving Transaction section:
Output field variables
Use the names of output fields as variables to access or set output data.
User-defined methods
Use any iuser-defined method that you declare in the Helper Code section.
Java transformation API methods
Call API methods provided by the Java transformation.
Using Java code to parse a flat file
You can develop Java code to parse a flat file. Use Java code to extract specific columns of data from a flat file of varying schema or from a JMS message.
For example, you want to read the first two columns of data from a delimited flat file. Create a mapping that reads data from a delimited flat file and passes data to one or more output fields.
The mapping contains the following components:
Source transformation
The source is a delimited flat file. Configure the source to pass each row as a single string to the Java transformation. The source file contains the following data:
Define the Java transformation functionality in the Java editor.
Use the On Input Row section of the Java editor to read each input row and pass the first two fields to the output field, outputRow. Enter the following code in the On Input Row section:
// Collect the first two fields of the row and output them into outputRow. String[] rowsSplit = row.split(",", 3); if (rowsSplit.length >= 2) { outputRow = rowsSplit[0] + "," + rowsSplit[1]; } generateRow();
Target transformation
Configure the target to receive the output field, outputRow, from the Java transformation. After you run the mapping, the target file has the following data: