There are two dedicated steps to use or execute a sub-transformation: Mapping (sub-transformation) step and Simple Mapping (sub-transformation) step. To execute a sub-transformation like the one created earlier, we will use the second one.
In this example, we will read a file that looks like the following one:
student_code;name;writing;reading;speaking;listening
80711-85;William Miller;81;83;80;90
20362-34;Jennifer Martin;87;76;70;80
75283-17;Margaret Wilson;99;94;90;80
83714-28;Helen Thomas;89;97;80;80
61666-55;Maria Thomas;88;77;70;80
...
After reading the file, we will call the sub-transformation to split the student's name.
Here are the instructions:
- Create a Transformation.
- With a Text file input step, read the exam1.txt file that comes with the code bundle for the chapter.
- From the Mapping category of steps, drag to the work area a Simple Mapping (sub-transformation) step (we will call it Mapping for short). Create a link from the Text file input step toward this new step.
- Double-click on the Mapping step to configure it.
- In the Transformation: textbox, type the full path for your sub-transformation, for example, ${Internal.Entry.Current.Directory}/subtransformations/split_word.ktr.
- Select the Input tab, and fill the grid as shown:
- Select the Output tab, and fill the grid as shown:
- Close the window.
- After the Mapping step, add a Dummy step.
- With the Dummy step selected, run a preview. You will see this:
As you can deduce, the key to call a sub-transformation is to establish a relationship between the fields in your main Transformation and the fields in the sub-transformation. You do it by filling the Input and Output tabs in the Mapping step dialog window.
Let's start by explaining the input. In the input specification of the sub-transformation, you can see that it expects a field named word. In your Transformation, the field that you want to transform is called name. Therefore, in the Input tab of the Mapping step dialog window, you defined the mapping between name and word.
On the other end, the sub-transformation creates two new fields: first_word and other_words. These fields will be added as fields to the main Transformation. If you want to give them a different name, you do it in the Output tab of the Mapping step dialog window, just as you did in the tutorial.
Before going on with advanced features, let's remark two things about sub-transformations:
- In the sample sub-transformation, you used a single input field, but you can use any number of fields. Even more, the Mapping Input Specification step has a checkbox named Include unspecified fields, ordered by name. If you check this option, all of the fields in the dataset of the main Transformation will be passed to the sub-transformation, and will be available to be used.
- As said, sub-transformations are a special kind of transformation. This means that all the things you do in a transformation can be done in a sub-transformation. One of those things is to define named parameters. When you call a sub-transformation, you provide the values for the named parameters in the Parameters tab of the Mapping step.