cancel
Showing results for 
Search instead for 
Did you mean: 

add counter only when duplicate segments are present in mapping

former_member440449
Participant
0 Kudos

Hi Gurus,

I have a FILE to FILE scenario, and the source file is as this:

aaa aaa aaa
bbb bbb bbb
ccc ccc ccc
ccc ccc ccc
ddd ddd ddd

and the structure will be as:

<Row>
field1> aaa
field2> aaa
field3> aaa
</Row>
<Row>
field1> bbb
field2> bbb
field3> bbb
</Row>
etc

In the target structure, I need to have the same elements, but there is one more field which is a counter field that need to be increased for each repetitive line and then reset back to 1 for a non-unique one.

So regarding the source structure, the target one should be as this:

<Row>
field1> aaa
field2> aaa
field3> aaa
counter> 1
</Row>
<Row>
field1> bbb
field2> bbb
field3> bbb
counter> 1
</Row>
<Row>
field1> ccc
field2> ccc
field3> ccc
counter> 1
</Row>
<Row>
field1> ccc
field2> ccc
field3> ccc
counter> 2
</Row>
<Row>
field1> ddd
field2> ddd
field3> ddd
counter> 1
</Row>
etc

I am asumming that I need to do this using UDF and local variables, but what I don't know is how can I compare the actual Row data with the previous one so I can increase the counter (or reset it).

Any idea on what can I do here?

Accepted Solutions (1)

Accepted Solutions (1)

PriyankaAnagani
Active Contributor
0 Kudos

Hi,

You can try the below logic to get the counter incremented according to the row. For all fields its simple one to one mapping except counter.

Row----Row

field1---field1

field2---field2 etc

for counter concat field1,field2,field3 with empty delimiter ...

concatinationof3fields--removecontext-sort-splitbyvaluechange-UDF-splitbyeachValue--counter

**execution type of UDF is Allvalues of a context.

public void UDF(String[] var1, ResultList result, Container container) throws StreamTransformationException{

ArrayList aList = new ArrayList();

int counter = 1;

int cnt = 1;

for(int i=0; i<var1.length; i++){

if(aList.contains(var1<i>)){

cnt++;

result.addValue(cnt);

}

else{

aList.add(var1<i>);

result.addValue(counter);

}

}

}

Regards

Priyanka

Answers (1)

Answers (1)

former_member440449
Participant
0 Kudos

Priyanka,

your solution worked successfully.

Thank you very much for your help!!!