You can use the Java code in this example to create and compile an active Java transformation.
Use a Java transformation to process employee data for a fictional company. The Java transformation reads input rows from a flat file source and writes output rows to a flat file target. The source file contains employee data, including the employee identification number, name, job title, and the manager identification number.
The transformation finds the manager name for a given employee based on the manager identification number and generates output rows that contain employee data. The output data includes the employee identification number, name, job title, and the name of the employee’s manager. If the employee has no manager in the source data, the transformation assumes the employee is at the top of the hierarchy in the company organizational chart.
Note: The transformation logic assumes that the employee job titles are arranged in descending order in the source file.
To create and run the mapping in this example, perform the following steps:
1Create the source file.
Create a comma-delimited flat file in a directory that the Secure Agent can access.
2Configure the mapping.
Add a source, target, and Java transformation to the mapping, and configure the fields.
3Configure the Java code snippets in the Java transformation.
Enter code snippets in the Import Packages, Helper Code, and On Input Row sections of the Java editor.
4Compile the code and run the mapping.
Create the source file
Create the source file as a comma-delimited flat file. Save the file in a directory that the Secure Agent can access.
To configure the mapping in this example, create a new mapping, and add a Java transformation. Then configure the source fields, target fields, and Java transformation fields. In the Java transformation, verify that the transformation behavior is active.
The following image shows the mapping:
Configure the transformations as follows:
Source transformation
In the Source transformation, update the source field metadata as shown in the following table:
Name
Type
Precision
Scale
Origin
EMP_ID
integer
10
0
<source file name>
EMP_NAME
string
100
0
<source file name>
EMP_DESC
string
100
0
<source file name>
EMP_AGE
integer
10
0
<source file name>
EMP_PARENT_EMPID
integer
10
0
<source file name>
Java transformation
The Java transformation includes all incoming fields from the Source transformation.
Create the following output fields:
Name
Type
Precision
Scale
EMP_ID_OUT
integer
10
0
EMP_NAME_OUT
string
100
0
EMP_DESC_OUT
string
100
0
EMP_PARENT_EMPNAME
string
100
0
On the Advanced tab, verify that the behavior is set to active.
Target transformation
In the target transformation, create the following target fields:
Name
Type
Precision
Scale
Origin
EMP_ID
integer
10
0
<target file name>
EMP_NAME
string
100
0
<target file name>
EMP_DESC
string
100
0
<target file name>
EMP_PARENT_EMPNAME
string
100
0
<target file name>
Configure the field mapping as shown in the following table:
Target Field Name
Mapped Field
EMP_ID
EMP_ID_OUT
EMP_NAME
EMP_NAME_OUT
EMP_DESC
EMP_DESC_OUT
EMP_PARENT_EMPNAME
EMP_PARENT_EMPNAME
Configure the Java code snippets
Enter the Java code snippets that define the transformation functionality on the Java tab.
Enter the Java code snippets in the following sections of the Java editor:
Import Packages
Import the java.util.Map and java.util.HashMap packages.
Helper Code
Create a Map object, lock object, and boolean variables to track the state of data in the Java transformation.
On Input Row
Enter code that defines the behavior of the Java transformation when it receives an input row.
Import packages
Import third-party Java packages, built-in Java packages, or custom Java packages in the Import Packages section. This example uses the Map and HashMap packages.
Enter the following code in the Import Packages section:
import java.util.Map; import java.util.HashMap;
Data Integration adds the import statements to the Java code for the transformation.
Helper code
Declare user-defined variables and methods for the Java transformation in the Helper Code section.
The Helper Code section defines the following variables that are used by the Java code in the On Input Row section:
Variable
Description
empMap
Map object that stores the identification number and employee name from the source.
lock
Lock object used to synchronize the access to empMap across partitions.
generateRow
Boolean variable used to determine whether an output row should be generated for the current input row.
isRoot
Boolean variable used to determine whether an employee is at the top of the company organizational chart (root).
Enter the following code in the Helper Code section:
// Static Map object to store the ID and name relationship of an employee. // If a session uses multiple partitions, empMap is shared across all partitions. private static Map <Integer, String> empMap = new HashMap <Integer, String> ();
// Static lock object to synchronize the access to empMap across partitions. private static Object lock = new Object();
// Boolean to track whether to generate an output row based on validity of the // input data. private boolean generateRow;
// Boolean to track whether the employee is root. private boolean isRoot;
Data Integration adds the import statements to the Java code for the transformation.
On Input Row
The Java transformation executes the Java code in the On Input Row section when the transformation receives an input row. In this example, the transformation might or might not generate an output row, based on the values of the input row.
Enter the following code in the On Input Row section:
// Initially set generateRow to true for each input row. generateRow = true;
// Initially set isRoot to false for each input row. isRoot = false;
// Check if input employee id and name is null. if (isNull ("EMP_ID") || isNull ("EMP_NAME")) { incrementErrorCount(1);
// If input employee id and/or name is null, don't generate a output row for this // input row. generateRow = false;
} else {
// Set the output field values. EMP_ID_OUT = EMP_ID; EMP_NAME_OUT = EMP_NAME; }
if(isParentEmpIdNull) { // This employee is the root for the hierarchy. isRoot = true; logInfo("This is the root for this hierarchy."); setNull("EMP_PARENT_EMPNAME"); }
synchronized(lock)
{ // If the employee is not the root for this hierarchy, get the corresponding // parent ID. if(!isParentEmpIdNull) EMP_PARENT_EMPNAME = (String) (empMap.get(new Integer (EMP_PARENT_EMPID)));
// Add employee to the map for future reference. empMap.put (new Integer(EMP_ID), EMP_NAME); }
// Generate row if generateRow is true. if(generateRow) generateRow();
Compile the code and run the mapping
To compile the Java code for the transformation, click Compile in the Java editor. After you compile the code successfully, you can run the mapping.
The compilation results displays the status of the compilation. If the Java code does not compile successfully, correct the errors in the Java editor and recompile the Java code. After you successfully compile the transformation, save and run the mapping.
To run the mapping, click Run in the Mapping Designer.