Scenario: Writing a file to MySQL if the number of its records matches a reference value - 6.3

Talend Components Reference Guide

EnrichVersion
6.3
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

In this scenario, tFileRowCount counts the number of records in a .txt file, which is compared against a reference value through tJava. Once the two values match, the .txt file will be written to a MySQL table.

The .txt file has two records:

1;andy
2;mike

Linking the components

  1. Drop tFileRowCount, tJava, tFlieInputDelimited, and tMysqlOutput from the Palette onto the design workspace.

  2. Link tFileRowCount to tJava using an OnSubjobOk trigger.

  3. Link tJava to tFlieInputDelimited using a Run if trigger.

  4. Link tFlieInputDelimited to tMysqlOutput using a Row > Main connection.

Configuring the components

  1. Double-click tFileRowCount to open its Basic settings view.

  2. In the File Name field, type in the full path of the .txt file. You can also click the [...] button to browse for this file.

    Select the Ignore empty rows check box.

  3. Double-click tJava to open its Basic settings view.

    In the Code box, enter the function to print out the number of rows in the file:

    System.out.println(globalMap.get("tFileRowCount_1_COUNT"));
  4. Click the if trigger connection to open its Basic settings view.

    In the Condition box, enter the statement to judge if the number of rows is 2:

    ((Integer)globalMap.get("tFileRowCount_1_COUNT"))==2

    This if trigger means that if the row count equals 2, the rows of the .txt file will be written to MySQL.

  5. Double-click tFlieInputDelimited to open its Basic settings view.

    In the File name/Stream field, type in the full path of the .txt file. You can also click the [...] button to browse for this file.

  6. Click the Edit schema button open the schema editor.

  7. Click the [+] button to add two columns, namely id and name, respectively of the integer and string type.

  8. Click the Yes button in the pop-up box to propagate the schema setup to the following component.

  9. Double-click tMysqlOutput open its Basic settings view.

  10. In the Host and Port fields, enter the connection details.

    In the Database field, enter the database name.

    In the Username and Password fields, enter the authentication details.

    In the Table field, enter the table name, for instance "staff".

  11. In the Action on table list, select Create table if not exists.

    In the Action on data list, select Insert.

Executing the Job

  1. Press Ctrl+S to save the Job.

  2. Press F6 to run the Job.

    As shown above, the Job has been executed successfully and the number of rows in the .txt file has been printed out.

  3. Go to the MySQL GUI and open the table staff.

    As shown above, the table has been created with the two records inserted.