Home | Table of Contents | DBJoin | CloverETL 4.6.1
--- | --- | ---
Prev | Joiners | Next

## DBJoin

# Not available in Community Designer

Short Description
Ports
Metadata
DBJoin Attributes
Details
Examples
Best Practices
Compatibility
See also

### Short Description

DBJoin receives data through a single input port and joins it with data from a database table. These two data sources can potentially have different metadata structures.

Component | Same input metadata | Sorted inputs | Slave inputs | Outputs | Output for drivers without slave | Output for slaves without driver | Joining based on equality | Auto-propagated metadata
--- | --- | --- | --- | --- | --- | --- | --- | ---
DBJoin |  |  | 1 (virtual) | 1-2 |  |  |  |

### Icon

### Ports

DBJoin receives data through a single input port and joins it with data from a database table. These two data sources can potentially have different metadata structure.

The joined data is then sent to the first output port. The second output port can optionally be used to capture unmatched master records.

Port type | Number | Required | Description | Metadata
--- | --- | --- | --- | ---
Input | 0 |  | Master input port | Any
1 (virtual) |  | Slave input port | Any
Output | 0 |  | Output port for the joined data | Any
1 |  | Optional output port for master data records without slave matches. (Only if the Join type attribute is set to `Inner join`.) This applies only to LookupJoin and DBJoin. | Input 0

### Metadata

DBJoin propagates metadata from first input port to second output port and from second output port to first input port.

DBJoin has no metadata template.

If mapping is not defined, DBJoin requires output metadata to match metadata of query result. If mapping is defined, metadata of query result must match metadata define in DBJoin attribute.

### DBJoin Attributes

Attribute | Req | Description | Possible values
--- | --- | --- | ---
Basic
Join key | yes | Key according to which the incoming data flows are joined. See Join Key. |
Left outer join |  | If set to `true`, also driver records without corresponding slave are parsed. Otherwise, `inner join` is performed. | false (default) | true
DB connection | yes | ID of the DB connection to be used as the resource of slave records. |
DB metadata |  | ID of DB metadata to be used. If not set, metadata is extracted from database using SQL query. |
Query URL | [1] | Name of external file, including path, defining SQL query. |
SQL query | [1] | SQL query defined in the graph. |
Transform | [2] [3] | Transformation in CTL or Java defined in the graph. |
Transform URL | [2] [3] | External file defining the transformation in CTL or Java. |
Transform class | [2] [3] | External transformation class. |
Cache size |  | Maximum number of records with different key values that can be stored in memory. | 100 (default)
Advanced
Transform source charset |  | Encoding of external file defining the transformation. The default encoding depends on DEFAULT_SOURCE_CODE_CHARSET in defaultProperties. | E.g. UTF-8
Deprecated
Error actions |  | Definition of the action that should be performed when the specified transformation returns some Error code. See Return Values of Transformations. |
Error log |  | URL of the file to which error messages for specified Error actions should be written. If not set, they are written to Console. |
[1] One of these attributes must be specified. If both are defined, Query URL has the highest priority. [2] One of these transformation attributes should be set. Any of them must use a common CTL template for Joiners or implement a `RecordTransform` interface. See CTL Scripting Specifics or Java Interfaces for more information. See also Defining Transformations for detailed information about transformations. [3] The unique exception is the case when none of these three attributes is specified, but the SQL query attribute defines what records will be read from DB table. Values of Join key contained in the input records serve to select the records from db table. These are unloaded and sent unchanged to the output port without any transformation.

### Details

Join Key
Transformation

DBJoin receives data through a single input port and joins it with data from a database table. These two data sources can potentially have different metadata structure. It is a general purpose joiner usable in most common situations. It does not require the input to be sorted and is very fast as data is processed in memory.

The data attached to the first input port is called the master, the second data source is called slave. Its data is considered as if it were incoming through the second (virtual) input port. Each master record is matched to the slave record on one or more fields known as a join key. The output is produced by applying a transformation that maps joined inputs to the output.

#### Join Key

The Join key is a sequence of field names from master data source separated from each other by a semicolon, colon, or pipe. You can define the key in the Edit key wizard.

Order of these field names must correspond to the order of the key fields from database table (and their data types). The slave part of Join key must be defined in the SQL query attribute.

One of the query attributes must contain the expression of the following form: `... where field_K=? and field_L=?`.

Example 51.1. Join Key for DBJoin

    $first_name;$last_name

This is the master part of fields that should serve to join master records with slave records.

SQL query must contain the expression that can look like this:

    ... where fname=? and lname=?

Corresponding fields will be compared and matching values will serve to join master and slave records.

#### Transformation

The transform in DBJoin lets you define transformation that sends records to the first output port. The unjoined master records sent to second output cannot be modified within the DBJoin transformation.

### CTL Scripting Specifics

All Joiners share the same transformation template which can be found in CTL Templates for Joiners.

For detailed information about Clover Transformation Language see Part IX, CTL2 - CloverETL Transformation Language.

### Java Interfaces

If you define your transformation in Java, it must implement the following interface that is common for all Joiners:

Java Interfaces for Joiners

See Public Clover API.

### Examples

#### Joining on Exact Key

Input records contain one field `customerId` with values:

    1
    35535
    255

Database table `customers` has the following structure and data:

    customer_id | customer_name | customer_surname
     1          | John          | Doe
     3          | Anna          | Smith
    10          | Jane          | Brown

Join incoming records with data from database on `customerId`.

##### Solution

Create input metadata with one field `customerId` (`integer`) and output metadata with `customerId (integer)`, `customerName (string)`, `customerSurname (string)` fields.

Set Join key and SQL query attributes of DBJoin.

Attribute | Value
--- | ---
Join key | customerId
DBConnection | Your DBConnection
SQL query | select * from "customers" where customer_id=?

Only records with `customerId` found in database are send to output.

#### Matching Range

Input records contain product code and date of order.

    productCode | orderDate
    1           | 2015-10-20
    2           | 2015-10-30
    1           | 2015-11-06

Price of particular product on particular date can be found in database.

    productCode | dateFrom   | dateTo     | price
    1           | 2014-01-01 | 2015-10-31 |  10.99
    1           | 2015-11-01 | 2099-12-31 |  14.99
    2           | 2015-07-01 | 2099-12-31 | 109.99

Match price to particular product orders.

##### Solution

Create input metadata (productCode, orderDate), output metadata (productCode, orderDate, price) and metadata for DBJoin ( productCode, price).

Set attributes

Attribute | Value
--- | ---
Join key | product;orderDate;orderDate
Metadata | DBJoinMetadata
DBConnection | Your DBConnection
SQL query | select product_id,price_per_unit from "dbjoin_example_02" where product_id=? and valid_from<=? and ?<=valid_to
Transform | See the code below.

    //#CTL2

    function integer transform() {
    	$out.0.* = $in.0.*;
    	$out.0.* = $in.1.*;

    	return ALL;
    }

### Best Practices

If the transformation is specified in an external file (with Transform URL), we recommend users to explicitly specify Transform source charset.

### Compatibility

#### 4.2.0-M1

Since CloverETL 4.2.0-M1, DBJoin propagates metadata between first input port and second output port.

### See also

Common Properties of Components
Specific Attribute Types
Common Properties of Joiners
Joiners Comparison
DBExecute

Prev | Up | Next
CrossJoin | Home | Table of Contents | ExtHashJoin