Text File Output
Output text files ..
Workshop - Text File Output
Reading files is only half the job. You also need to generate files for users and systems.
In this workshop, you build a transformation that writes a customer survey for Steel Wheels. You will build the survey from multiple streams. You will parameterize it with a runtime customer name.
What you'll do
Read a customer name from a transformation argument
Build header and body sections with static rows and file-driven rows
Format text using User Defined Java Expression
Merge streams in a predictable order with Append streams
Write the final output with Text file output
Prerequisites: Understanding of basic transformation concepts (steps, hops, preview). Complete Text File Input first.
Estimated time: 35 minutes
Workshop files
Download the following files.
Keep the filenames unchanged.
Save them in your workshop folder.

Create a new transformation
Use any of these options to open a new transformation tab:
Select File > New > Transformation
Use
Ctrl+N(Windows/Linux) orCmd+N(macOS)
User Defined Java Expression
Use User Defined Java Expression to format the header line. It will combine a label with the customer name.
Drag User Defined Java Expression onto the canvas.
Create a hop from Get System Info.
Double-click the step. Configure it like this:

Select OK.
This replaces the original argument value with formatted text. The output field is still named text.
Append streams
Use Append streams when order matters. It outputs all rows from the first hop. It then outputs all rows from the second hop.
Both input streams must have the same field names and types.
Drag Append streams onto the canvas.
Create hops from User Defined Java Expression and Data Grid.
Double-click the step. Configure it like this:

Select OK.
To append streams, keep the layout consistent. In this workshop, every stream uses a single text field.
If order does not matter, use a step that performs a union of streams instead.
Make sure the header stream is the first input hop. Append streams will output that stream first.
Text file input
Use Text file input to read the question list from a file. Each question becomes one row.
Drag Text file input onto the canvas.
Double-click the step. Configure it like this:

File: ${Internal.Transformation.Filename.Directory}/questions.txt

Use a Tab delimiter. Enable row numbers to generate question numbers.
On Fields, rename the output field to
text:

Select OK.
Each row now contains a question in text. The row number field (for example question_num) identifies the question number.
User Defined Java Expression
Use a second User Defined Java Expression to prefix each question line with its question number.
Drag User Defined Java Expression onto the canvas.
Create a hop from Text file input (questions).
Double-click the step. Configure it like this:

Select OK.
This overwrites text with a numbered question like 1. How did we do?.
Select values
The Select Values step is useful for selecting, removing, renaming, changing data types and configuring the length and precision of the fields on the stream.
These operations are organized into different categories:
Select and Alter — Specify the exact order and name in which the fields should be placed in the output rows
Remove — Specify the fields that should be removed from the output rows
Meta-data — Change the name, type, length, and precision (the metadata) of one or more fields
Drag Select values onto the canvas.
Create a hop from User Defined Java Expression (number questions).
Double-click the step. Configure it like this:

Select OK.
Remove the question number field so both streams have the same layout. You need a single text field before you append.
Do not run multiple copies of this step against the same output file. Use Include stepnr in filename if you need parallel output.
Drag Text file output onto the canvas.
Create a hop from Append streams (body).
Double-click the step. Set the file name:
Filename: ${Internal.Transformation.Filename.Directory}/survey
Set Extension to txt if your output should be survey.txt.

On Fields, select Get Fields.

Select OK.
Select Run in the canvas toolbar.
Open Arguments (legacy). Enter a customer name.

Select Run.
Open the Preview data tab.

Open the generated survey file in your transformation folder.
This workshop reinforces the rule for merging streams:
Keep the same field layout (names and order).
Keep matching data types.
Last updated
Was this helpful?




