July 30, 2023

tCombinedSQLAggregate – Docs for ESB 7.x

tCombinedSQLAggregate

Provides a set of matrix based on values or calculations.

tCombinedSQLAggregate collects data
values from one or more columns of a table for statistical purposes. This component has
real-time capabilities since it runs the data transformation on the DBMS itself.

tCombinedSQLAggregate Standard properties

These properties are used to configure tCombinedSQLAggregate running in the Standard Job framework.

The Standard
tCombinedSQLAggregate component belongs to the ELT family.

The component in this framework is available in all Talend
products
.

Basic settings

Schema and Edit
schema

A schema is a row description, it defines the number of fields that will be processed
and passed on to the next component. The schema is either built-in
or remote in the Repository.

Click Edit
schema
to make changes to the schema. If the current schema is of the Repository type, three options are available:

  • View schema: choose this
    option to view the schema only.

  • Change to built-in property:
    choose this option to change the schema to Built-in for local changes.

  • Update repository connection:
    choose this option to change the schema stored in the repository and decide whether
    to propagate the changes to all the Jobs upon completion. If you just want to
    propagate the changes to the current Job, you can select No upon completion and choose this schema metadata
    again in the Repository Content
    window.

Click Sync columns to retrieve
the schema from the previous component connected in the Job.

 

Built-in: You create and store
the schema locally for this component only. Related topic: see

Talend Studio User
Guide
.

 

Repository: You have already
created the schema and stored it in the Repository. You can reuse it
in various projects and Jobs. Related topic: see
Talend Studio User
Guide
.

Group by

Define the aggregation sets, the values of which will be used for
calculations.

 

Output Column: Select the column
label in the list offered according to the schema structure you
defined. You can add as many output columns as you wish to make more
precise aggregations.

 

Input Column: Select the input
column label to match the output column’s expected content, in case
the output label of the aggregation set needs to be
different.

Operations

Select the type of operation along with the value to use for the
calculation and the output field.

 

Output Column: Select the
destination field in the list.

 

Function: Select any of the
following operations to perform on data: count, min,
max, avg, sum, first, last, distinct and
count

(distinct)
.

 

Input column: Select the input
column from which you want to collect the values to be
aggregated.

Advanced settings

tStatCatcher Statistics

Select this check box to gather the Job processing metadata at a
Job level as well as at each component level.

Global Variables

Global Variables

NB_LINE: the number of rows read by an input component or
transferred to an output component. This is a Flow variable and it returns an
integer.

QUERY: the query statement being processed. This is a Flow
variable and it returns a string.

ERROR_MESSAGE: the error message generated by the
component when an error occurs. This is an After variable and it returns a string. This
variable functions only if the Die on error check box is
cleared, if the component has this check box.

A Flow variable functions during the execution of a component while an After variable
functions after the execution of the component.

To fill up a field or expression with a variable, press Ctrl +
Space
to access the variable list and choose the variable to use from it.

For further information about variables, see
Talend Studio

User Guide.

Usage

Usage rule

This component is an intermediary component. The use of the
corresponding connection and commit components is recommended when
using this component to allow a unique connection to be open and
then closed during the Job execution.

Filtering and aggregating table columns directly on the DBMS

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

  • populates a database table with the input data,

  • creates the output table for the filtered data,

  • 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.

tCombinedSQLAggregate_1.png

Adding and linking the components

  1. Drop the following components from the Palette onto the design workspace: tMysqlConnection, tFixedFlowInput, tMysqlOutput,
    tCreateTable, tCombinedSQLInput, tCombinedSQLFilter, tCombinedSQLAggregate, tCombinedSQLOutput, tMysqlCommit, tMysqlInput and
    tLogRow.
  2. Connect tMysqlConnection to tFixedFlowInput using a Trigger > On Subjob Ok link
  3. Do the same to connect tFixedFlowInput to
    tCreateTable, tCreateTable to tCombinedSQLInput, tCombinedSQLInput to tMysqlCommit, and tMysqlCommit
    to tMysqlInput.
  4. Connect tFixedFlowInput and tMysqlOutput using a Row > Main link.
  5. Connect tCombinedSQLInput to tCombinedSQLFilter using a Row > Combine link.
  6. Do the same to connect tCombinedSQLFilter to
    tCombinedSQLAggregate, and tCombinedSQLAggregate to tCombinedSQLOutput
  7. Connect tMysqlInput and tLogRow using a Row >
    Main link.

Configuring the components

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.

tCombinedSQLAggregate_2.png

Opening a MySQL connection

  1. Launch MySQL Workbench and start a local
    connection on port 3306.
  2. Create a new schema and name it test.
  3. Back in the design workspace, select tMysqlConnection and click the Component tab to define its basic settings.
  4. 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
    .
    tCombinedSQLAggregate_3.png

Populating the database table with input data

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

    tCombinedSQLAggregate_4.png

  2. In the Basic settings view, in the Number of rows field, enter 500.
  3. In this scenario, the source database table has seven columns: id, first_name, last_name, city, state, date_of_birth,
    and salary

    Click the […] button next to Edit schema to define the following data
    structure.
    tCombinedSQLAggregate_5.png

  4. Click the floppy disk icon to save the schema as a generic schema for later
    reuse.
  5. In the Select folder window, select default and click OK.
  6. Choose a name for your generic schema and click Finish.
  7. Click OK.
  8. The first column of the Values table
    automatically reflects the data structure you entered previously.
  9. In the Values table, enter a value for each
    column.
  10. In the design workspace, select tMysqlOutput
    and click the Component tab to define its basic
    settings.

    tCombinedSQLAggregate_6.png

    The output schema will automatically be the same as the previous component, in
    this case tFixedFlowInput.

Creating the target database table

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

    tCombinedSQLAggregate_7.png

  2. Click the […] button next to Edit schema to define the following data
    structure.

    tCombinedSQLAggregate_8.png

    The schema you enter at this step must reflect the the differents aggregation
    operations you want to perform on the input data.

Extracting and filtering data

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

    tCombinedSQLAggregate_9.png

  2. Enter the source table name, in this case employees in
    the Table field.
  3. In the Schema field, select Repository from the list and click the […] button right to the empty field to load the
    schema you saved.while configuring the settings for tFixedFlowInput.
  4. In the Repository Content window, expand
    Generic schemas and select your
    schema.
  5. Click the […] button right to Edit schema.
  6. Select View schema, and in the first column
    of the table, clear the check boxes for first_name,
    last_name and city.

Filtering and aggregating the input data

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

    tCombinedSQLAggregate_10.png

  2. 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
    .

    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.
  3. In the Conditions table, set input
    parameters, operators and expected values in order to only extract the records
    that fulfill these criteria.

    Click two times on the [+] button under the
    Conditions table, and in Input column, select state and
    date_of_birth from the drop-down list.
    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.
  4. For the column state, select IN as operator from the drop-down list, and enter
    (‘Utah’,’Ohia’,’Iowa’) as value.
  5. For the column date_of_birth, select > as operator from the drop-down list, and enter
    (‘1960-10-19’) as value.
  6. 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.
  7. In the design workspace, select tCombinedSQLAggregate and click the Component tab to access the configuration panel.
  8. Click on the […] button.next to Edit schema to enter the following configuration:

    tCombinedSQLAggregate_11.png

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

  9. 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.
  10. In the Output column drop-down list, select
    State. This column will be used to hold the data
    filtered on State.
  11. 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 in. Then
    we want to count the number of employees per state, calculate the
    average/lowest/highest salaries as well as the oldest/youngest employees for
    each state.
  12. In the Operations table, click the [+] button to add a line and then click in the
    Output column list to select the output
    column that will hold the computed data.
  13. In the Function field, select the relevant
    operation to be carried out.

Writing the output data into MySQL

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

    tCombinedSQLAggregate_13.png

  2. On the Database type list, select the
    relevant database.
  3. On the Component list, select the relevant
    database connection component if more than one connection is used.
  4. In the Table field, enter the name of the
    target table which will store the results of the aggregation operations,
    empl_by_state in this case

    The tCombinedSQLOutput component requires
    that an output table already exists in the database to work. That is why the
    empl_by_state table was created earlier in the
    scenario.
    In this example, the Schema field doesn’t
    need to be filled out as the database is not Oracle.
  5. Click the Sync columns button to retrieve the
    schema from the previous component.

    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.

Committing the data into the database

  1. In the design workspace, select tCombinedSQLCommit and click the Component tab to access the configuration panel.
  2. On the Component list, select the relevant
    database connection component if more than one connection is used.
  3. Clear the check box Close Connection.

Retrieving the filtered and aggregated data

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

    tCombinedSQLAggregate_14.png

  2. Select the check box Use an existing
    connection
    ans choose tMysqlConnection_1 from the list.
  3. Click on the […] button.next to Edit schema to enter the following schema:

    tCombinedSQLAggregate_15.png

  4. In the field Table Name, enter
    empl_by_state and in the Query field, enter select * from
    empl_by_state
    .
  5. In the design workspace, select tLogRow and
    click the Component tab to define its basic
    settings.

    tCombinedSQLAggregate_16.png

  6. Click the Sync columns button to retrieve the
    schema from the previous component and select the Table
    (print values in cells of a table)
    mode.

Saving and executing the Job

  1. Save your Job and press F6 to execute
    it.
  2. The Run tab opens, where you can observe the
    result of the Job execution.
  3. The output data retrieved by the tLogRow is
    visible in a table.

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.

tCombinedSQLAggregate_17.png


Document get from Talend https://help.talend.com
Thank you for watching.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x