Blog

logical Testing the most used pattern in Talend

  • (4.0)

Testing Logic is probably the most-used job design in TALEND PROGRAMMING, and is used to ensure that a snippet of new code is not influenced by external factors within a large and complex job. This simple recipe shows how this can easily be achieved.

MindMajix Talend Training course is designed to make you an expert in developing ETL solutions using Talend platform. Start Learning!

Getting ready

Open the jo_cook_ch10_0140_logicTest job.

How to do it…

The steps for testing logic are as follows:

  • In tFixedFlowInput, tick the box labeled Use Inline Table.
  • Add the values, as shown in the following screenshot:

  • In the tMap, add a new field to the output named ageCheckValid, and populate it with the following code:
customer.age >= 21 && customer.country.equals("UK") ? true : customer.age >= 18  
          && !customer.country.equals("UK") ? true : false
  • Run the job to see the results of the test.

How it works…

In this example, we are testing an age limit; 21 or over is valid for the UK, 18 or over is valid for the rest of the world.
In tFixedFlowInput, we defined a set of test values that would prove that the logic test in tMap is working correctly.
The tLogRow component, then allows us to see the inputs and the corresponding results.

There’s more…

This example is great for testing new rules, and especially for testing new code routines prior to adding to a complex job. It is quick to create, and the tFixedFlowInput component allows us to build a controlled set of test data that should test all return values, thus giving us confidence in the correctness of the new code or code routine.

Frequently Asked TALEND Interview Questions & Answers

Killing a job from within tJavaRow

User Case

There are multiple ways to stop a job in Talend. You could use “if” trigger statements combined with “tDie”, some components have a die on error tick boxes in their settings. Here, let us see one of the ways to kill/terminate a Talend Studio job based on an input file row value.
Let’s say you are black boxing a Talend job for a client and you know based on experience that the client does not always provide the most accurate input files. Some of the rows that must be NOT NULL for the job to run successfully aren’t always provided. Unfortunately you cannot substitute those empty rows with dummy data (according to your job logic), so you have to kill the job and inform the client that something went wrong, so that the person running the job will be able to fix the issue.

Validation Process

1. Create a new Job;
2. Add 3 components

  • “tFileInputDelimited”;
  • “tJavaRow”;
  • “tLogRow”;
output_row.columnName1= input_row.columnName1; output_row.columnName2 = input_row.columnName2; 
output_row.columnName3 = input_row.columnName3; if (output_row.columnName4 == null)
 { System.out.println("Error: " + output_row.columnName4 + " is null"); 
System.exit(99); } 
 else { output_row.columnName4 = input_row.columnName4; } 
 output_row.columnName5 = input_row.columnName5;

3. Create a Delimited file (.csv) and make sure that for one of the columns the value is null;
4. In “tJavaRow” add code similar to:

output_row.columnName1= input_row.columnName1; 
 output_row.columnName2 = input_row.columnName2; output_row.columnName3 = input_row.columnName3;
 if (output_row.columnName4 == null) 
 { System.out.println("Error: " + output_row.columnName4 + " is null"); System.exit(99); }
 else { output_row.columnName4 = input_row.columnName4; }
 output_row.columnName5 = input_row.columnName5;

Note: columnName[1,2,3,4,5] – the names of the columns you’ve specified in your Delimited file. If row value equals to null for a particular column, then print a message to the console log and terminate the job.

5. Run the job (if in your delimited file, the column that you are evaluating has a null, the job will be terminated);
This simple approach can reduce the amount of Talend components used in the Job while providing a way to evaluate the input file.

Explore TALEND Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

Popular Courses in 2018

Get Updates on Tech posts, Interview & Certification questions and training schedules