Below User Define Functions (UDF) logic can be used to group, sort and handle duplicate XML records.
Note: -
Grouping XML records
Here is input XML and expected output XML: -

Mapping: -

UDF: -
UDF will search ‘Name_People’ in ‘Name_Record’, if it finds a match, corresponding ‘Where’ and ‘Phone’ will be written to target fields with Context Change. Context Change will be added to output ‘Record_out’ for each ‘Name_People’.

Sorting XML records
Here is input XML and expected output XML: -

Mapping: -
Mapping is same as above, add standard function 'sort' to key field. UDF logic is same as above. Output XML will be sorted with Name_People.

Handling Duplicate XML records
Here is input XML and expected output XML: - Duplicate records are ignored.

Mapping: -

UDF: -
HashSet will return true, if unique value is added. It will return false, if duplicate is added.

UDF code for reference
// Grouping XML records.
public void udf_getCorrespondingRecord(String[] Name_People, String[] Name_Record, String[] Where, String[] Phone, ResultList Record_out, ResultList Where_out, ResultList Phone_out, Container container) throws StreamTransformationException{
for(int i = 0; i < Name_People.length; i ++) {
for(int j = 0; j < Name_Record.length; j ++) {
if(Name_People[ i ].equals(Name_Record[ j ])) {
Record_out.addValue(" ");
Where_out.addValue(Where[ j ]); Phone_out.addValue(Phone[ j ]);
Where_out.addValue(ResultList.CC); Phone_out.addValue(ResultList.CC);
}
}
Record_out.addValue(ResultList.CC);
}
}
//If match has to be found using a combination of two fields. Please use logic something like this in above UDF:
//if((Name_People[ i ] + Where[ i ]).equals(Name_People[ j ] + Where[ j ]))
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Handle duplicate XML records.
public void udf_getUnique(String[] Id, String[] Name, String[] field1, ResultList Record_out, ResultList Id_out, ResultList Name_out, ResultList field1_out, Container container) throws StreamTransformationException{
Set<String> unique = new HashSet<String>();
for(int i = 0; i < Id.length; i ++) {
if(unique.add( Id[ i ] )) { // True if unique.
Record_out.addValue(" ");
Id_out.addValue(Id[ i ]); Name_out.addValue(Name[ i ]); field1_out.addValue(field1[ i ]);
Id_out.addValue(ResultList.CC); Name_out.addValue(ResultList.CC); field1_out.addValue(ResultList.CC);
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 27 | |
| 24 | |
| 13 | |
| 12 | |
| 11 | |
| 11 | |
| 10 | |
| 10 | |
| 9 | |
| 9 |