3
© 2008 Informatica Corporation Java Transformation: Using Java Code to Parse a Flat File

0015-Java ParseFlatFile Complete

Embed Size (px)

Citation preview

© 2008 Informatica Corporation

Java Transformation: Using Java Code to Parse a Flat File

2

Overview The Java transformation provides a simple native programming interface to define transformation functionality with the Java programming language. You can use the Java transformation to quickly define simple or moderately complex transformation functionality without advanced knowledge of the Java programming language or an external Java development environment.

Create Java transformations by writing Java code snippets that define transformation logic. You can use Java code in a Java Transformation to parse and extract specific columns of data from a flat file. When the rows in each version of source flat file are of varying length, use the Java Transformation to read the flat file as a string. If you use the Java transformation to read the flat file as a string, you do not need to update the mapping for each flat file source of varying row length.

Example In the following example, you use Java code to extract specific columns of data from a flat file of varying schema or a JMS message.

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 ports.

The following figure shows how the Java transformation reads rows of data and passes data to a target:

The mapping contains the following components:

Source Definition and Source Qualifier

Java Transformation

Target Definition

Source Definition and Source Qualifier The source is a delimited flat file. Configure the source to pass rows of a flat file as a string to the Java transformation. The source file has the following data:

1a,2a,3a,4a,5a,6a,7a,8a,9a,10a

1b,2b,3b,4b,5b,6b,7b,8b,9b

1c,2c,3c,4c,5c,6c,7c

1d,2d,3d,4d,5d,6d,7d,8d,9d,10d

3

Java Transformation Use the Java transformation to read each row of data from a string and pass the data to output ports. Then, define the Java transformation functionality on the Java Code tab.

The Java transformation has the following ports: Name Datatype Type

row String Input

f1 String Output

f11 String Output

Configuring the Java Code Tab The Java Code tab contains multiple code entry tabs. Use the code entry tabs to enter Java code snippets that define Java transformation functionality. For this example, use the Import Packages tab and the On Input Row tab.

Import Packages Tab You can use the Import Packages tab to import third-party Java packages, built-in Java packages, or custom Java packages.

Use the Import Packages tab of the Java Code to import a standard java package named StringTokenizer.

Enter the following code on the Import Packages tab: import java.util.StringTokenizer;

On Input Row tab You can use the On Input Row tab to define transformation behavior when it receives an input row.

Use the On Input Row tab of the Java Code tab to read each row of data from a string and pass the data to output ports.

Enter the following code on the On Input Row tab to retrieve the first two columns of data: StringTokenizer st1 = new StringTokenizer(row,","); // Reads each row of string data using the comma as the delimiter. f1 = st1.nextToken(); // Read the first column f11= st1.nextToken(); // Read the second column

The StringTokenizer reads each row of string data using the comma as the delimiter.

Target Definition Configure the target to receive rows of data from the Java Transformation. After you run a workflow that contains the mapping, the target contains two columns of data.

The target file has the following data:

1a,2a

1b,2b

1c,2c

1d,2d

Author Padma Heid, Technical Writer