bing
Flat 10% & upto 50% off + Free additional Courses. Hurry up!

Creating Advanced Transformations and Jobs

 

Updating a file with news about examinations by setting a variable with the name of the file:

  1. Copy the examination files you used in Chapter 2 to the input files and folder defined in your kettle.properties file. If you don’t have them, download them from the Packt website.
  2. Open Spoon and create a new transformation.
  3. Use a Get System Info step to get the first command-line argument. Name the field as filename.
  4. Add a Filter rows step and create a hop from the Get System Info step to this step.
  5. From the Flow category drag an Abort step to the canvas, and from the Job category of steps drag a Set Variables step.
  6. From the Filter rows step, create two hops—one to the Abort step and the other to the Set Variables step. Double-click the Abort step. As Abort message, put File name is mandatory.
  7. Double-click the Set Variables step and click on Get Fields. The window will be filled as shown here:

 

1

 

 

  1. Click on OK.
  2. Double-click the Filter rows step. Add the following filter: filename IS NOT NULL. In the drop-down list to the right of Send ‘true’ data to step, select the Set Variables step, whereas in the drop-down list to the right of Send ‘false’ data to step, select the Abort step.
  3. The final transformation looks like this:

 

1

 

 

  1. Save the transformation in the transformations folder under the name getting_filename.ktr.
  2. Open the transformation named examinations.ktr that was created in Chapter 2 or download it from the Packt website. Save it in the transformations folder under the name examinations_2.ktr.
  3. Delete the Get System Info step.
  4. Double-click the Text file input step.
  5. In the Accept filenames from previous steps frame, uncheck the Accept filenames from previous step option.
  6. Under File/Directory in the Selected files grid, type ${FILENAME}. Save the transformation.
  7. Create a new job.
  8. From the General category, drag a START entry and a Transformation entry to the canvas and link them.
  9. Save the job as examinations.kjb.
  10. Double-click the Transformation entry. As Transformation filename, put the name of the first transformation that you created: ${Internal.Job.Filename.Directory}/transformations/getting_filename.ktr.
  11. Click on OK.
  12. From the Conditions category, drag a File Exists entry to the canvas and create a hop from the Transformation entry to this new one.
  13. Double-click the File Exists entry.
  14. Write ${FILENAME} in the File name textbox and click on OK.
  15. Add a new Transformation entry and create a hop from the File Exists entry to this one.
  16. Double-click the entry and, as Transformation filename, put the name of the second transformation you created:${Internal.Job.Filename.Directory}/transformations/examinations_2.ktr.
  17. Add a Write To Log entry, and create a hop from the File Exists entry to this. The hop should be red, to indicate when execution fails. If not, right-click the hop and change the evaluation condition to Follow when result is false.
  18. Double-click the entry and fill all the textboxes as shown:

 

1

 

 

29. Add two entries—an abort and a success. Create hops to these new entries as shown next:

 

1

 

 

  1. Save the job.
  2. Press F9 to run the job.
  3. Set the logging level to Minimal logging and click on Launch.
  4. The job fails. The following is what you should see in the Logging tab in the Execution results window:

 

1

 

 

  1. Press F9 again. This time set Basic logging as the logging level.
  2. In the arguments grid, write the name of a fictitious file—for example, c:/pdi_files/input/nofile.txt.
  3. Click on Launch. This is what you see now in the Logging tab window:

 

1

 

 

  1. Press F9 for the third time. Now provide a real examination filename such as c:/pdi_files/input/exam1.txt.
  2. Click on Launch. This time you see no errors. The examination file is appended to the global file:

 

1

 

 

Generating files with top scores:

  1. Create a new transformation and save it in the transformations folder under the name top_scores.ktr.
  2. Use a Text file input step to read the global examination file generated in the previous tutorial.
  3. After the Text file input step, add the following steps and link them in the same order:

A Select values step to remove the unused fields—file_processed and process_date.

A Split Fields to split the name of the students in two—name and last name.

A Formula step to convert name and last name to uppercase.

With the same Formula step, change the scale of the scores. Replace each skill field writing, reading, speaking, and listening with the same value divided by 20—for example, [writing]/20.

4.Do a preview on completion of the final step to check that you are doing well. You should see this:

 

1

 

 

  1. Af‍ter the last Formula step, add and link in this order the following steps:

A Sort rows step to order the rows in descending order by the writing field.

A JavaScript step to filter the first 10 rows. Remember that you learned to do this in the chapter devoted to JavaScript. You do it by typing the following piece of code:

trans_Status = CONTINUE_TRANSFORMATION;

if (getProcessCount('r')>10) trans_Status = SKIP_TRANSFORMATION;

An Add sequence step to add a field named seq_w. Leave the defaults so that the field contains the values 1, 2, 3 …

A Select values step to rename the field seq_w as position and the field writing as score. Specify this change in the Select & Alter tab, and check the option Include unspecified fields, ordered.

A Text file output step to generate a file named writing_top10. txt at the location specified by the ${LABSOUTPUT} variable. In the Fields tab, put the following fields— position, student_code, student_name, student_lastname, and score.

 

  1. Save the transformation, as you’ve added a lot of steps and don’t want to lose your work.
  2. Repeat step number 5, but this time sort by the reading field, rename the sequence seq_r as position and the field reading as score, and send the data to the reading_top10.txt file.
  3. Repeat the same procedure for the speaking field and the listening field.
  4. This is how the transformation looks like:

 

1

 

 

  1. Save the transformation.
  2. Run the transformation. Four files should have been generated. All the files should look similar. Let’s check the writing_top10.txt file (the names and values may vary depending on the examination files that you have appended to the global file):

 

1

 

 

Creating a job as a process flow

With the implementation of a subtransformation, you simplify much of the transformation. But you still have some reworking to do. In the main transformation, you basically do two things. First you read the source data from a file and prepare it for further processing. And then, after the preparation of the data, you generate the files with the top scores. To have a clearer vision of these two tasks, you can split the transformation in two, creating a job as a process flow.

 

Splitting the generation of top scores by copying and getting rows:

  1. Open the transformation in the previous tutorial. Select all steps related to the preparation of data, that is, all steps from the Text file input step upto the Formula step.
  2. Copy the steps and paste them in a new transformation.
  3. Expand the Job category of steps.
  4. Select a Copy rows to result step, drag it to the canvas, and create a hop from the last step to this new one. Your transformation looks like this:

 

1

 

 

  1. Save the transformation in the transformations folder with the name top_scores_flow_preparing.ktr.
  2. Go back to the original transformation and select the rest of the steps, that is, the Mapping and the Text file output steps.
  3. Copy the steps and paste them in a new transformation.

8. From the Job category of steps select a Get rows from result step, drag it to the canvas, and create a hop     from this step to each of the Mapping steps

 

1

 

 

  1. Save the transformation in the transformations folder with the name top_ scores_flow_processing.ktr.
  2. In the top_scores_flow_preparing transformation , right-click the step Copy rows to result and select Show output fields.
  3. The grid with the output dataset shows up.

 

1

 

 

  1. Select all rows. Press Ctrl+C to copy the rows.
  2. In the top_scores_flow_processing transformation, double-click the step Get rows from result.

14. Press Ctrl+V to paste the values.

 

1

 

 

  1. Save the transformation.
  2. Create a new Job.
  3. Add a START and two transformation entries to the canvas and link them one after the other.
  4. Double-click the first transformation. Put ${Internal.Job.Filename. Directory}/transformations/top_scores_flow_preparing.ktr as the name of the transformation.
  5. Double-click the second transformation. Put ${Internal.Job.Filename. Directory}/transformations/top_scores_flow_processing.ktr as the name of the transformation.
  6. Save the job. Press F9 to open the Job properties window and click on Launch. Again, the four files should have been generated, with the very same information.

 

1

 

 

Nesting jobs:

Suppose that every time you append a file with examination results, you want to generate updated files with the top 10 scores. You can do it manually, running one job after the other, or you can nest jobs.

 

Generating the files with top scores bynesting jobs:

  1. Open the examinations job you created in the first tutorial of this chapter.
  2. After the last transformation job entry, add a job entry as Job. You will find it under the General category of entries.
  3. Double-click the Job job entry.
  4. Type ${Internal.Job.Filename.Directory}/top_scores_flow.kjb as Job filename.
  5. Click on OK.
  6. Save the job.
  7. Pick an examination that you have not yet appended to the global file—for example, exam5.txt.
  8. Press F9.
  9. In the Arguments grid, type the full path of the chosen file: c:/pdi_files/input/exam5.txt.
  10. Click on Launch.
  1. In the Job metrics tab of the Execution results window, you will see the following:

 

1

 

 

  1. Also the chosen file should have been added to the global file, and updated files with top scores should have been generated.

 

Iterating jobs and transformations:

It may happen that you develop a job or a transformation to be executed several times, once for each different row of your data. The same applies to transformations. If you have to execute the same transformation several times, once for each row of a set of data, you can do it by iterating the execution.

 

Generating custom files by executing a transformation for every input row:

  1. Create a new transformation.
  2. Drag a Text file input, a Filter rows, and a Select values step to the canvas and link them in that order.
  3. Use the Text file input step to read the global examination file.
  4. Use the Filter rows step to keep only those students with a writing score below 60.
  5. With the Select values step, keep just the student_code and name values.
  6. After this last step, add a Copy rows to result step.

7. Do a preview on this last step.

 

1

 

 

  1. Save the transformation in the transformations folder with the name students_list.ktr.

 

Let’s create a job that puts everything together:

  1. Create a job.

      2. Drag a START, a Delete files, and two transformation entries to the canvas, and link them one after the    other

 

1

 

 

  1. Save the job.
  2. Double-click the Delete files step. Fill the Files/Folders: grid with a single row—under File/Folder type ${LABSOUTPUT} and under Wilcard (RegExp) type hello.*\.txt. This regular expression includes all .txt files whose name start with the string “hello” in the ${LABSOUTPUT} folder.
  3. Double-click the first transformation entry. As Transformation filename, put ${Internal.Job.Filename.Directory}/transformations/student_ list.ktr and click on OK.
  4. Double-click the second transformation entry. As Transformation filename, put ${Internal.Job.Filename.Directory}/transformations/ hello_each.ktr.
  5. Check the option Execute for every input row? and click on OK.
  6. Save the job and press F9 to run it.
  7. When the execution finishes, explore the folder pointed by your ${LABSOUTPUT} variable. You should see one file for each student in the list. The files are named hello_<hhmmddss>.txt where <hhmmddss> is the time in your system at the moment that the file was generated. The generated files look like the following:

 

1

"0 Responses on Creating Advanced Transformations and Jobs"

Training in Cities

Bangalore, Hyderabad, Chennai, Delhi, Kolkata, UK, London, Chicago, San Francisco, Dallas, Washington, New York, Orlando, Boston

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.

top

Sales Offer

  • To avail this offer, enroll before 07th December 2016.
  • This offer cannot be combined with any other offer.
  • This offer is valid on selected courses only.
  • Please use coupon codes mentioned below to avail the offer
offer-june

Sign Up or Login to view the Free Creating Advanced Transformations and Jobs.