In a Job, the tRuleSurvivorship component generates a survivorship rule package based on the conditions you define in the Rule table in the component Basic settings view.
If you want the rule to survive records based on some more advanced criteria, you must manually code the conditions in the rule using the Drools language.
The Job in this scenario gives an example about how to modify the code in the rule generated by the component to use specific conditions to create a survivor. Later, you can use this survivor, for example, to create a master copy of data for MDM.
The components used in this Job are:
tFixedFlowInput: it provides the input data to be processed by this Job.
tRuleSurvivorship: it creates the survivor validation flow based on the conditions you code in the rule. This component selects the best-of-breed data that composes the single representation of each duplicate group.
tLogRow: it shows the result of the Job execution.
Drop tFixedFlowInput, tRuleSurvivorship and tLogRow from the palette of the studio onto the Design workspace.
Right-click tFixedFlowInput and select the Row > Main link to connect this component to tRuleSurvivorship.
Do the same to connect tRuleSurvivorship to tLogRow using the Row > Main link.
Double-click tFixedFlowInput to open its Component view.
Click the three-dot button next to Edit schema to open the schema editor.
Click the plus button and add five rows.
Rename these rows respectively as the following: Record_ID, File, Acctname, GRP_ID and GRP_SIZE.
The input data has information about group ID and group size. In real life scenario, such information can be gathered by the tMatchGroup component as shown in scenario 1. tMatchGroup groups duplicates in the input data and gives each group a group ID and a group size. These two columns are required by tRuleSurvivorship.
In the Type column, select the data types for your columns. In this example, set the type to Integer for Record_ID and GRP_SIZE, and set it to String for the other columns.
Make sure to set the proper data type so that you can define the validation rules without error messages.
Click OK to validate these changes and accept the propagation when prompted by the pop-up dialog box.
In the Mode area of the Basic settings view, select Use Inline Content (delimited file).
In the Content field, enter the input data to be processed.
This data should correspond to the schema you have defined. In this example, the input data is as the following:
1;2;AcmeFromFile2;1;2 2;1;AcmeFromFile1;1;0 3;1;AAA;2;1 4;2;BBB;3;1 5;1; ;4;2 6;2;NotNull;4;0
Set the row and field separators in the corresponding fields.
Double-click tRuleSurvivorship to open its Component view.
Select GRP_ID from the Group Identifier list and GRP_SIZE from the Group size list.
In the Rule package name field, replace the by-default name org.talend.survivorship.sample with a name of your choice, if needed.
The survivor validation flow will be generated and saved under this name in the Repository tree view of the Integration perspective.
In the Rule table, click the plus button to add a row per rule.
In this example, define one rule and complete it as the following:
One rule, "Rule1", will be generated and executed by tRuleSurvivorship. This rule validates the records in the File column that comply with the expression you enter in the Value column of the Rule table. The component will then select the corresponding value as the best breed from the Acctname target column.
Next to Generate rules and survivorship flow, click the icon to generate the rule package according to the conditions you have defined.
The rule package is generated and saved under Metadata > Rules Management > Survivorship Rules in the Repository tree view of the Integration perspective.
In the Repository tree view, browse to the rule file under the Survivorship Rules folder and double-click "Rule1" to open it.
But this rule will select the values that come from file 1. However, you may also want to survive records based on specific criteria; for example, if Acctname has a value in file1, you may want to use that value, or else use the value from file2 instead. To do this, you must modify the code manually in the rule file.
Modify the rule with the following Drools code:
package org.talend.survivorship.sample rule "ExistInFile1" no-loop true dialect "mvel" ruleflow-group "Rule1Group" when $input : RecordIn( file.equals("1"), acctname!= null, !acctname.trim().equals("") ) then System.out.println("ExistInFile1 fired\t" + $input.record_id); dataset.survive( $input.TALEND_INTERNAL_ID, "Acctname" ); dataset.survive( $input.TALEND_INTERNAL_ID, "File" ); end rule "NotExistFile1" no-loop true dialect "mvel" ruleflow-group "Rule1Group" when $input : RecordIn( file.equals("2"), acctname!= null && !acctname.trim().equals("") ) (not (exists (RecordIn( file.equals("1") ))) or exists( RecordIn( file.equals("1"), acctname== null || acctname.trim().equals("") ) )) then System.out.println("NotExistFile1 fired\t" + $input.record_id); dataset.survive( $input.TALEND_INTERNAL_ID, "Acctname" ); dataset.survive( $input.TALEND_INTERNAL_ID, "File" ); end
After you modify the rule file, you must not click the icon. Otherwise, your modifications will be replaced by the new generation of the rule package.
Double-click tLogRow to open the Component view and in the Mode area, select the Table (print values in cells of a table) option.
The execution result of the Job will be printed in a table.
Press F6 to execute the Job.
The Run view is opened automatically showing the execution results.
You can read that four rows are the survivor records because their SURVIVOR column indicates true. In the survivor records, the Acctname value is selected from file 1, if the value exists. If not, the value is selected from file 2, as you defined in the rule. Other rows are the duplicates of same groups.
The CONFLICT column shows that no column has more than one value compliant with the given validation rules.