Rollup
Short Description
Rollup creates one or more output records from one or more input records.
Rollup receives potentially unsorted data through the single input port, transforms it and creates one or more output records from one or more input records.
The component can send different records to different output ports as specified by the user.
Same input metadata | Sorted inputs | Inputs | Outputs | Java | CTL | Auto-propagated metadata |
---|---|---|---|---|---|---|
- |
⨯ |
1 |
1-n |
✓ |
✓ |
⨯ |
Ports
Port type | Number | Required | Description | Metadata |
---|---|---|---|---|
Input |
0 |
✓ |
For input data records |
Any(In0) |
Output |
0 |
✓ |
For output data records |
Any(Out0) |
1-N |
⨯ |
For output data records |
Any(Out1-N) |
Metadata
Rollup does not propagate metadata.
Rollup has no metadata template.
Input and output metadata fields can have any data types.
Metadata on output ports can differ.
You may need a metadata for the accumulator record in rollup transformation.
Rollup Attributes
Attribute | Req | Description | Possible values |
---|---|---|---|
Basic |
|||
Group key |
Key according to which the records are considered to be included into one group. Expressed as a sequence of individual input field names separated from each other by a semicolon. For more information, see Group Key. If not specified, all records are considered to be members of a single group. |
e.g. |
|
Group accumulator |
The ID of metadata that serves to create group accumulators. Metadata serves to store values used for transformation of individual groups of data records. |
no metadata (default) | any metadata |
|
Transform |
Definition of the transformation written in the graph in CTL or Java. |
||
Transform URL |
The name of an external file, including the path, containing the definition of the transformation written in CTL or Java. |
||
Transform class |
The name of an external class defining the transformation. |
||
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 |
|
Sorted input |
By default, records are considered to be sorted.
Either in ascending or descending order.
Different fields may even have different sort order.
If your records are not sorted, switch this attribute to |
true (default) | false |
|
Equal NULL |
By default, records with null values of key fields are considered to be equal.
If set to |
true (default) | false |
Details
Rollup requires transformation. You can define the transformation using CTL (see CTL interface) or Java (see Java Interface).
The flow of function calls in a rollup transformation is depicted below. If any optional function (except functions for error handling) is not used, the position of unimplemented function from diagram is skipped.
If you do not define Group accumulator metadata, VoidMetadata is used in transformation functions.
CTL interface
The transformation uses a CTL template for Rollup, implement a RecordRollup
interface or inherit from a DataRecordRollup
superclass.
Below is a list of RecordRollup
interface methods.
For detailed information about this interface, see Java Interface.
Once you have written your transformation, you can also convert it to Java language code by clicking a corresponding button at the upper right corner of the tab.
You can open the transformation definition as another tab of the graph (in addition to the Graph and Source tabs of Graph Editor) by clicking a corresponding button at the upper right corner of the tab.
CTL Templates for Rollup
CTL Template Functions | |
---|---|
void init() |
|
Required |
No |
Description |
Initialize the component, setup the environment, global variables. |
Invocation |
Called before processing the first record. |
Returns |
|
void initGroup(<metadata name> groupAccumulator) |
|
Required |
yes |
Input Parameters |
If |
Returns |
void |
Invocation |
Called repeatedly, once for the first input record of each group. Called before |
Description |
Initializes information for specific group. |
Example |
|
boolean updateGroup(<metadata name> groupAccumulator) |
|
Required |
yes |
Input Parameters |
If |
Returns |
|
Invocation |
Called repeatedly (once for each input record of the group, including the first and the last record). Called after the |
Description |
Updates information for specific group. If If any of the input records causes a failure of the |
Example |
|
boolean finishGroup(<metadata name> groupAccumulator) |
|
Required |
yes |
Input Parameters |
If |
Returns |
|
Invocation |
Called repeatedly, once for the last input record of each group. Called after |
Description |
Finalizes the group information. If If any of the input records causes fail of the The |
Example |
|
integer updateTransform(integer counter, <metadata name> groupAccumulator) |
|
Required |
yes |
Input Parameters |
|
If |
|
Returns |
Integer numbers. For more information, see Return Values of Transformations. |
Invocation |
Called repeatedly as specified by user. Called after The function is called until |
Description |
It creates output records based on individual record information. If If any part of the The |
Example |
|
integer transform(integer counter, <metadata name> groupAccumulator) |
|
Required |
yes |
Input Parameters |
|
If |
|
Returns |
Integer numbers. For more information, see Return Values of Transformations. |
Invocation |
Called repeatedly as specified by the user. Called after The function is called until |
Description |
It creates output records based on all of the records of the whole group. If If any part of the The |
Example |
|
void initGroupOnError(string errorMessage, string stackTrace, <metadata name> groupAccumulator) |
|
Required |
no |
Input Parameters |
|
|
|
If |
|
Returns |
void |
Invocation |
Called if |
Description |
Initializes information for specific group. |
Example |
|
boolean updateGroupOnError(string errorMessage, string stackTrace, <metadata name> groupAccumulator) |
|
Required |
no |
Input Parameters |
|
|
|
If |
|
Returns |
|
Invocation |
Called if |
Description |
Updates information for specific group. |
Example |
|
boolean finishGroupOnError(string errorMessage, string stackTrace, <metadata name> groupAccumulator) |
|
Required |
no |
Input Parameters |
|
|
|
If |
|
Returns |
|
Invocation |
Called if |
Description |
Finalizes the group information. |
Example |
|
integer updateTransformOnError(string errorMessage, string stackTrace, integer counter, <metadata name> groupAccumulator) |
|
Required |
yes |
Input Parameters |
|
|
|
|
|
If |
|
Returns |
Integer numbers. For detailed information, see Return Values of Transformations. |
Invocation |
Called if |
Description |
It creates output records based on individual record information. |
Example |
|
integer transformOnError(string errorMessage, string stackTrace, integer counter, <metadata name> groupAccumulator) |
|
Required |
no |
Input Parameters |
|
|
|
|
|
If |
|
Returns |
Integer numbers. For detailed information, see Return Values of Transformations. |
Invocation |
Called if |
Description |
It creates output records based on all of the records of the whole group. |
Example |
|
string getMessage() |
|
Required |
No |
Description |
Prints an error message specified and invoked by the user. |
Invocation |
Called in any time specified by the user (called only when either |
Returns |
|
void preExecute() |
|
Required |
No |
Input parameters |
None |
Returns |
|
Description |
May be used to allocate and initialize resources required by the transform. All resources allocated within this function should be released by the |
Invocation |
Called during each graph run before the transform is executed. |
void postExecute() |
|
Required |
No |
Input parameters |
None |
Returns |
|
Description |
Should be used to free any resources allocated within the |
Invocation |
Called during each graph run after the entire transform was executed. |
Access to input and output fields
All of the other CTL template functions allow to access neither inputs nor outputs or groupAccumulator
.
Input records or fields
Input records or fields are accessible within the initGroup()
, updateGroup()
, finishGroup()
, initGroupOnError()
, updateGroupOnError()
and finishGroupOnError()
functions.
They are also accessible within the updateTransform()
, transform()
, updateTansformOnError()
and transformOnError()
functions.
Output records or fields
Output records or fields are accessible within the updateTransform()
, transform()
, updateTansformOnError()
and transformOnError()
functions.
Group accumulator
Group accumulator is accessible within the initGroup()
, updateGroup()
, finishGroup()
, initGroupOnError()
, updateGroupOnError()
and finishGroupOnError()
functions.
It is also accessible within the updateTransform()
, transform()
, updateTansformOnError()
and transformOnError()
functions.
Remember that if you do not hold these rules, NPE will be thrown. |
Java Interface
The transformation implements methods of the RecordRollup
interface and inherits other common methods from the Transform
interface.
See Common Java Interfaces and Public CloverDX API.
Following is the list of the RecordRollup
interface methods:
-
void init(Properties parameters, DataRecordMetadata inputMetadata, DataRecordMetadata accumulatorMetadata, DataRecordMetadata[] outputMetadata)
Initializes the rollup transform. This method is called only once at the beginning of the life-cycle of the rollup transform. Any internal allocation/initialization code should be placed here.
-
void initGroup(DataRecord inputRecord, DataRecord groupAccumulator)
This method is called for the first data record in a group. Any initialization of the group accumulator should be placed here.
-
void initGroupOnError(Exception exception, DataRecord inputRecord, DataRecord groupAccumulator)
This method is called for the first data record in a group. Any initialization of the group "accumulator" should be placed here. Called only if
initGroup(DataRecord, DataRecord)
throws an exception. -
boolean updateGroup(DataRecord inputRecord, DataRecord groupAccumulator)
This method is called for each data record (including the first one as well as the last one) in a group in order to update the group accumulator.
-
boolean updateGroupOnError(Exception exception, DataRecord inputRecord, DataRecord groupAccumulator)
This method is called for each data record (including the first one as well as the last one) in a group in order to update the group accumulator. Called only if
updateGroup(DataRecord, DataRecord)
throws an exception. -
boolean finishGroup(DataRecord inputRecord, DataRecord groupAccumulator)
This method is called for the last data record in a group in order to finish the group processing.
-
boolean finishGroupOnError(Exception exception, DataRecord inputRecord, DataRecord groupAccumulator)
This method is called for the last data record in a group in order to finish the group processing. Called only if
finishGroup(DataRecord, DataRecord)
throws an exception. -
int updateTransform(int counter, DataRecord inputRecord, DataRecord groupAccumulator, DataRecord[] outputRecords)
This method is used to generate output data records based on the input data record and the contents of the group accumulator (if it was requested). The output data record will be sent to the output when this method finishes. This method is called whenever the
boolean updateGroup(DataRecord, DataRecord)
method returnstrue
. Thecounter
argument is the number of previous calls to this method for the current group update. See Return Values of Transformations for detailed information about return values and their meaning. -
int updateTransformOnError(Exception exception, int counter, DataRecord inputRecord, DataRecord groupAccumulator, DataRecord[] outputRecords)
This method is used to generate output data records based on the input data record and the contents of the group accumulator (if it was requested). Called only if
updateTransform(int, DataRecord, DataRecord)
throws an exception. -
int transform(int counter, DataRecord inputRecord, DataRecord groupAccumulator, DataRecord[] outputRecords)
This method is used to generate output data records based on the input data record and the contents of the group accumulator (if it was requested). The output data record will be sent to the output when this method finishes. This method is called whenever the
boolean finishGroup(DataRecord, DataRecord)
method returnstrue
. Thecounter
argument is the number of previous calls to this method for the current group. See Return Values of Transformations for detailed information about return values and their meaning. -
int transformOnError(Exception exception, int counter, DataRecord inputRecord, DataRecord groupAccumulator, DataRecord[] outputRecords)
This method is used to generate output data records based on the input data record and the contents of the group accumulator (if it was requested). Called only if
transform(int, DataRecord, DataRecord)
throws an exception.
Examples
Merging and updating incomplete records
You have a list of records containing name, email address and phone number. Records do not have all fields filled in. Records are sorted according to the field name.
Merge together data of records with the same name. If more records with the same name have the same field filled in, use the last one.
Alice|alice@example.com| Alice| |+420123456789 Alice|alice@example.org| Bob | |+421212345678 Bob |bob@example.info | Eve |eve@example.com |+420720123456 Eve | |+420720123457
Solution
Input and output metadata (updateRecord) have fields name, email and phoneNumber.
Use the attributes Group key, Group accumulator and Transform of Rollup.
Attribute | Value |
---|---|
Group key |
name |
Group accumulator |
updateRecord |
Transform |
See the code below |
//#CTL2
function void initGroup(updateRecord groupAccumulator) {
groupAccumulator.* = $in.0.*;
return;
}
function boolean updateGroup(updateRecord groupAccumulator) {
if (!isnull($in.0.email))
{
groupAccumulator.email = $in.0.email;
}
if (!isnull($in.0.phoneNumber))
{
groupAccumulator.phoneNumber = $in.0.phoneNumber;
}
return false;
}
function boolean finishGroup(updateRecord groupAccumulator) {
return true;
}
function integer updateTransform(integer counter, updateRecord groupAccumulator) {
raiseError("Function not implemented!");
}
function integer transform(integer counter, updateRecord groupAccumulator) {
if ( counter > 0 )
{
return SKIP;
}
$out.0.* = groupAccumulator.*;
return ALL;
}
Result records contains merged field values:
Alice|alice@example.org|+420123456789 Bob |bob@example.info |+421212345678 Eve |eve@example.com |+420720123457
Transforming multivalue fields to multiple records
Input records containing name, group and email have a multivalue field email. Split input stream to two data streams: one with name and group, the other one with name and email. The output records will be loaded into a database without support of multivalue fields.
Jane Green |users |[jane.green@example.org, greenj@example.org, jane.brown@example.org] John Smith |users |[john.smith@example.com, jsmith@example.com] Peter Green|users |[peter.green@example.com]
The field name of an input record is unique
Solution
Input metadata users has fields name, group and email. Output metadata users2 has fields name and group, output metadata emails has fields name and email.
Use the Rollup attributes Group key, Group accumulator and Transform.
Attribute | Value |
---|---|
Group key |
name |
Group accumulator |
users |
Transform |
See the code below |
//#CTL2
function void initGroup(users groupAccumulator) {
return;
}
function boolean updateGroup(users groupAccumulator) {
groupAccumulator.* = $in.0.*;
return true;
}
function boolean finishGroup(users groupAccumulator) {
return true;
}
function integer updateTransform(integer counter, users groupAccumulator) {
if(counter >= length(groupAccumulator.email )) {
return SKIP;
}
$out.1.name = $in.0.name;
$out.1.email = groupAccumulator.email[counter];
return 1;
}
function integer transform(integer counter, users groupAccumulator) {
if(counter > 0 )
{
return SKIP;
}
$out.0.name = $in.0.name;
$out.0.group = $in.0.group;
return 0;
}
The transformation above requires the field user to be unique.
You receive 3 records on the first output port:
Jane Green |users John Smith |users Peter Green|users
Six records will be send to second output port:
Jane Green |jane.green@example.org Jane Green |greenj@example.org Jane Green |jane.brown@example.org John Smith |john.smith@example.com John Smith |jsmith@example.com Peter Green|peter.green@example.com
Best practices
To process a large number of records, sort records first and than use Rollup instead of using Rollup with the Sorted input attribute set to false
.
If the transformation is specified in an external file (with Transform URL), we recommend users to explicitly specify Transform source charset.