Transformations > Java transformation > Java transformation example
  

Java transformation example

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:
  1. 1. Create the source file.
  2. Create a comma-delimited flat file in a directory that the Secure Agent can access.
  3. 2. Configure the mapping.
  4. Add a source, target, and Java transformation to the mapping, and configure the fields.
  5. 3. Configure the Java code snippets in the Java transformation.
  6. Enter code snippets in the Import Packages, Helper Code, and On Input Row sections of the Java editor.
  7. 4. Compile 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.
Use the following data:
EMP_ID,EMP_NAME,EMP_AGE,EMP_DESC,EMP_PARENT_EMPID
1,James Davis,50,CEO,
4,Elaine Masters,40,Vice President - Sales,1
5,Naresh Thiagarajan,40,Vice President - HR,1
6,Jeanne Williams,40,Vice President - Software,1
9,Geetha Manjunath,34,Senior HR Manager,5
10,Dan Thomas,32,Senior Software Manager,6
14,Shankar Rahul,34,Senior Software Manager,6
20,Juan Cardenas,32,Technical Lead,10
21,Pramodh Rahman,36,Lead Engineer,14
22,Sandra Patterson,24,Software Engineer,10
23,Tom Kelly,32,Lead Engineer,10
35,Betty Johnson,27,Lead Engineer,14
50,Dave Chu,26,Software Engineer,23
70,Srihari Giran,23,Software Engineer,35
71,Frank Smalls,24,Software Engineer,35

Configure the mapping

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:
The example mapping shows Source transformation "src_Emp" connected to downstream Java transformation "java_FindEmpMgr." The Java transformation is connected to Target transformation "tgt_EmpWithMgr."
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 (isNull ("EMP_DESC"))
{
setNull("EMP_DESC_OUT");
} else {
EMP_DESC_OUT = EMP_DESC;
}

boolean isParentEmpIdNull = isNull("EMP_PARENT_EMPID");

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.
The target file contains the following results:
"EMP_ID","EMP_NAME","EMP_DESC","EMP_PARENT_EMPNAME"
1,"James Davis","CEO",
4,"Elaine Masters","Vice President - Sales","James Davis"
5,"Naresh Thiagarajan","Vice President - HR","James Davis"
6,"Jeanne Williams","Vice President - Software","James Davis"
9,"Geetha Manjunath","Senior HR Manager","Naresh Thiagarajan"
10,"Dan Thomas","Senior Software Manager","Jeanne Williams"
14,"Shankar Rahul","Senior Software Manager","Jeanne Williams"
20,"Juan Cardenas","Technical Lead","Dan Thomas"
21,"Pramodh Rahman","Lead Engineer","Shankar Rahul"
22,"Sandra Patterson","Software Engineer","Dan Thomas"
23,"Tom Kelly","Lead Engineer","Dan Thomas"
35,"Betty Johnson","Lead Engineer","Shankar Rahul"
50,"Dave Chu","Software Engineer","Tom Kelly"
70,"Srihari Giran","Software Engineer","Betty Johnson"
71,"Frank Smalls","Software Engineer","Betty Johnson"