Scenario 4: Handling errors - 6.1

Talend Components Reference Guide

EnrichVersion
6.1
EnrichProdName
Talend Big Data
Talend Big Data Platform
Talend Data Fabric
Talend Data Integration
Talend Data Management Platform
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Open Studio for Big Data
Talend Open Studio for Data Integration
Talend Open Studio for Data Quality
Talend Open Studio for ESB
Talend Open Studio for MDM
Talend Real-Time Big Data Platform
task
Data Governance
Data Quality and Preparation
Design and Development
EnrichPlatform
Talend Studio

This following scenario creates a six-component Job that shows how to handle error conditions using the tHMap component.

When tHMap executes a Talend Data Mapper map, an ExecutionStatus object is always returned. A pointer to this object is stored in the globalMap as EXECUTION_STATUS. In addition, the Overall Severity numeric value (the constants are defined in ExecutionStatus) is also stored in the globalMap as EXECUTION_SEVERITY. Finally, a parameter called Execution Threshold is defined that specifies the severity on which to throw an exception, thus triggering Job-related or component-related error processing. The default value for this parameter is Fatal.

To consult the Javadoc for the ExecutionStatus object, see the Camel Runtime documentation included in the Talend Data Mapper runtime kits and the Talend Data Mapper User Guide.

Adding and linking the components

  1. In the Integration perspective, create a new Standard Job and call it error_handling.

  2. Click the point in the design workspace where you want to add the first component, start typing tFixedFlowInput, and then click the name of the component when it appears in the list proposed in order to select it.

  3. Do the same to add a tHMap component, a tJavaRow component, and three tJava components as well.

  4. Connect the tFixedFlowInput component to the tHMap component using a Row > Main link, then connect the tHMap component to the tJavaRow component using a Row > Main link and rename the link out. If you are asked whether you want to get the schema of the target component, click Yes.

  5. Connect the tFixedFlowInput component to the first tJava component using an OnSubjobError trigger and to the second tJava component using an OnSubjobOk trigger, then connect the tHMap component to the third tJava component using a RunIf trigger.

Defining the properties of the RunIf trigger

  1. Select the RunIf trigger to define its properties.

  2. Add the following code in the Condition section.

    !((org.talend.transform.runtime.api.ExecutionStatus) globalMap.get("tHMap_1_EXECUTION_STATUS")).isOK()

Defining the properties of tFixedFlowInput

  1. Select the tFixedFlowInput component to define its properties.

  2. In the Basic settings tab, click the [...] button next to Edit schema to define the schema.

  3. Add one column, of type String, rename it inputKey, and then click OK.

  4. In the Mode area. select the Use Inline Table radio button, then add three values to the inputKey column by clicking the [+]  button and then entering the value between double quotes in each case. In this example, the values used are value1, value2 and value3.

Defining the properties of tJavaRow

  1. Select the tJavaRow component to define its properties.

  2. In the Basic settings tab, click the [...] button next to Edit schema to define the schema.

  3. Click the [+]  button to add a column to the input schema on the left, of type String, rename it outputKey, copy it to the output schema on the right, and then click OK.

  4. Add the following code in the Code section.

    String actualOutput = out.outputKey;
    
    System.out.println("======" + actualOutput + "======");

Defining the properties of tHMap

  1. Select the tHMap component to define its properties.

  2. Click the [...] button next to the Open Map Editor field to create a new map based on the input and output of tHMap.

  3. In the tHMap Structure Generate/Select dialog box that opens, select Generate hierarchical mapper structure based on the schema and then click Next to generate the input structure.

  4. Do the same for the output structure.

  5. In the Map editor that opens, drag the inputKey element of Input (map) to the outputKey element of Output (map), and then save your changes.

  6. Back in the Job, in the tHMap component, leave the value of the Exception Threshold drop-down list (in the Advanced settings tab) as Fatal.

  7. In the Basic settings tab, check that Read Input As is set to Data Integration columns.

  8. Check that Write Output As is set to Data Integration columns.

Defining the properties of the first tJava component (OnSubjobError)

  1. Select the first tJava component to define its properties. This component displays information in the console when the Job contains an error.

  2. Add the following code in the Code section.

    System.out.println("tJava_1: Subjob ERROR");
    
    org.talend.transform.runtime.api.ExecutionStatus es = (org.talend.transform.runtime.api.ExecutionStatus)globalMap.get("tHMap_1_EXECUTION_STATUS");
    
    System.out.println("Execution result:" + es.getOverallSeverity());
    
    // ExecutionStatus object
    System.out.println(es.toString());
    
    // XML version of ExecutionStatus object
    java.io.StringWriter sw = new java.io.StringWriter();
    es.exportToXml(sw);
    System.out.println("ExecutionStatus as XML");
    System.out.println(sw.toString());

Defining the properties of the second tJava component (OnSubjobOk)

  1. Select the second tJava component to define its properties. This component displays information in the console when the Job runs successfully.

  2. Add the following code in the Code section.

    System.out.println("tJava_2: Subjob OK");
    
    org.talend.transform.runtime.api.ExecutionStatus es = (org.talend.transform.runtime.api.ExecutionStatus)globalMap.get("tHMap_1_EXECUTION_STATUS");
    
    System.out.println("Execution result:" + es.getOverallSeverity());
    
    // ExecutionStatus object
    System.out.println(es.toString());
    
    // XML version of ExecutionStatus object
    java.io.StringWriter sw = new java.io.StringWriter();
    es.exportToXml(sw);
    System.out.println("ExecutionStatus as XML");
    System.out.println(sw.toString());

Defining the properties of the third tJava component (RunIf)

  1. Select the third tJava component to define its properties. This component runs if any errors occur within the map itself (higher than informational status).

  2. Add the following code in the Code section.

    System.out.println("tJava_3: Run If");
    
    org.talend.transform.runtime.api.ExecutionStatus es = (org.talend.transform.runtime.api.ExecutionStatus)globalMap.get("tHMap_1_EXECUTION_STATUS");
    
    System.out.println("Execution result:" + es.getOverallSeverity());
    
    // ExecutionStatus object
    System.out.println(es.toString());
    
    // XML version of ExecutionStatus object
    java.io.StringWriter sw = new java.io.StringWriter();
    es.exportToXml(sw);
    System.out.println("ExecutionStatus as XML");
    System.out.println(sw.toString());

Running the Job under different conditions

  1. Press Ctrl+S to save your Job.

  2. In the Run tab, click Run to execute the Job.

    In this case, no errors occur, so the Job triggers the second tJava component only.

  3. Double-click the tHMap component to open the Map editor.

  4. Right-click the outputKey element and click Go to Structure Element.

  5. Change the tHMap_1_output structure that opens from Read Only to Editable and then change the Data Type for outputkey to Integer (32). This means that this element can only be an Integer, and since this does not match the input, an error will occur.

  6. In the Run tab, click Run to execute the Job again.

    In this case, the Job still triggers the second tJava component even though there is an error, because the threshold above which an exception should be thrown (thus enabling the downstream components to detect the error) is set as Fatal. However, the third tJava component is also triggered since there is an error in the execution of the map.

  7. In the tHMap component, change the value of the Exception Threshold drop-down list (on the Advanced Settings tab) to Error. This causes an exception to be thrown when the map has a severity of Error or higher.

  8. In the Run tab, click Run to execute the Job for a third time.

    In this case, the Job detects the error and triggers the first tJava component (OnSubjobError).