Scenario: Filtering and aggregating table columns directly on the DBMS - 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

The following scenario creates a Job that opens a connection to a MySQL database and:

  • instantiates the schema from a database table in part (for column filtering),

  • filters two columns in the same table to get only the data that meets two filtering conditions,

  • collects data from the filtered column(s), grouped by specific value(s) and writes aggregated data in a target database table.

To filter and aggregate database table columns:

  • Drop the following components from the Palette onto the design workspace: tMysqlConnection, tCombinedSQLInput, tCombinedSQLFilter, tCombinedSQLAggregate, tCombinedSQLOutput and tMysqlCommit.

  • Connect tMysqlConnection, tCombinedSQLInput and tMysqlCommit using OnSubjobOk links.

  • Connect tCombinedSQLInput, tCombinedSQLFilter, tCombinedSQLAggregate and tCombinedSQLOutput using a Combine link.

  • In the design workspace, select tMysqlConnection and click the Component tab to define its basic settings.

  • In the Basic settings view, set the database connection details manually or select Repository from the Property Type list and select your DB connection if it has already been defined and stored in the Metadata area of the Repository tree view.

For more information on centralizing DB connection details in the Repository, see Talend Studio User Guide.

  • In the design workspace, select tCombinedSQLInput and click the Component tab to access the configuration panel.

  • Enter the source table name in the Table field, and click the three-dot button next to Edit schema to define the data structure.

Note

The schema defined through tCombinedSQLInput can be different from that of the source table as you can just instantiate the desired columns of the source table. Therefore, tCombinedSQLInput also plays a role of column filtering.

In this scenario, the source database table has seven columns: id, first_name, last_name, city, state, date_of_birth, and salary while tCombinedSQLInput only instantiates four columns that are needed for the aggregation: id, state, date_of_birth, and salary from the source table.

  • In the design workspace, select tCombinedSQLFilter and click the Component tab to access the configuration panel.

  • Click the Sync columns button to retrieve the schema from the previous component, or configure the schema manually by selecting Built-in from the Schema list and clicking the [...] button next to Edit schema.

Note

When you define the data structure for tCombinedSQLFilter, column names automatically appear in the Input column list in the Conditions table.

In this scenario, the tCombinedSQLFilter component instantiates four columns: id, state, date_of_birth, and salary.

  • In the Conditions table, set input parameters, operators and expected values in order to only extract the records that fulfill these criteria.

In this scenario, the tCombinedSQLFilter component filters the state and date_of_birth columns in the source table to extract the employees who were born after Oct. 19, 1960 and who live in the states Utah, Ohio and Iowa.

  • Select And in the Logical operator between conditions list to apply the two conditions at the same time. You can also customize the conditions by selecting the Use custom SQL box and editing the conditions in the code box.

  • In the design workspace, select tCombinedSQLAggregate and click the Component tab to access the configuration panel.

  • Click the Sync columns button to retrieve the schema from the previous component, or configure the schema manually by selecting Built-in from the Schema list and clicking on the [...] button.

The tCombinedSQLAggregate component instantiates four columns: id, state, date_of_birth, and salary, coming from the previous component.

The Group by table helps you define the data sets to be processed based on a defined column. In this example: State.

  • In the Group by table, click the [+] button to add one line.

  • In the Output column drop-down list, select State. This column will be used to hold the data filtered on State.

The Operations table helps you define the type of aggregation operations to be performed. The Output column list available depends on the schema you want to output (through the tCombinedSQLOutput component). In this scenario, we want to group employees based on the state they live. We want then count the number of employees per state, calculate the average/lowest/highest salaries as well as the oldest/youngest employees for each state.

  • In the Operations table, click the [+] button to add one line and then click in the Output column list to select the output column that will hold the computed data.

  • In the Function field, select the relevant operation to be carried out.

  • In the design workspace, select tCombinedSQLOutput and click the Component tab to access the configuration panel.

  • On the Database type list, select the relevant database.

  • On the Component list, select the relevant database connection component if more than one connection is used.

  • In the Table field, enter the name of the target table which will store the results of the aggregation operations.

Note

In this example, the Schema field doesn't need to be filled out as the database is not Oracle.

  • Click the three-dot button next to Edit schema to define the data structure of the target table.

In this scenario, tCombinedSQLOutput instantiates seven columns coming from the previous component in the Job design (tCombinedSQLAggregate): state, empl_count, avg_salary, min_salary, max_salary, oldest_empl and youngest_empl.

  • In the design workspace, select tCombinedSQLCommit and click the Component tab to access the configuration panel.

  • On the Component list, select the relevant database connection component if more than one connection is used.

  • Save your Job and press F6 to execute it.

Rows are inserted into a seven-column table empl_by_state in the database. The table shows, per defined state, the number of employees, the average salary, the lowest and highest salaries as well as the oldest and youngest employees.