cancel
Showing results for 
Search instead for 
Did you mean: 

UDF to add values occuring in as many times

Former Member
0 Kudos

Hi Experts,

I have a source message structure as below

<Node>

<element1>1</element1>

<element2>33</element2>

<element3>43</element3>

</Node>

<Node>

<element1>2</element1>

<element2>35</element2>

</Node>

<Node>

<element1>3</element1>

<element2>34</element2>

</Node>

Target Is as below

<Node>

<element1>value</element1>

</Node>

Where value has to be 123 (value in each occurence)

Can this be done by standard functions?? do I have to use a UDF? queue or context??

Pls share ur thoughts

Edited by: Ravindra Teja on Jan 3, 2011 4:17 PM

Edited by: Ravindra Teja on Jan 3, 2011 4:20 PM

Accepted Solutions (1)

Accepted Solutions (1)

srikanth_srinivasan3
Active Participant
0 Kudos

This is very simple & could be done using standard functions itself.

-

Srikanth Srinivasan

Answers (2)

Answers (2)

sunil_singh13
Active Contributor
0 Kudos

Hi Ravindra,

You can achive this by using standard functions also. Use arithmatic function "add" and also change the context of Source node to the topmost node, so that it will appeare as a single queue. The function will add all the values in the queue.

Thanks,

Sunil Singh

stefan_grube
Active Contributor
0 Kudos

> You can achive this by using standard functions also. Use arithmatic function "add" and also change the context of Source node to the topmost node, so that it will appeare as a single queue.

You mean: statistic function "sum"

baskar_gopalakrishnan2
Active Contributor
0 Kudos

>>>> Where value has to be 123 (value in each occurence)

>>>> Can this be done by standard functions?? do I have to use a UDF? queue or context??

Please go through this link ... I already answered here....

If you dont want decimal in your value then use just below code...

public void sumValue(ResultList result, String[] value, Container container) throws StreamTransformationException{

double sum = 0.0;

for(int count =0; count<value.length; count++){

if(value[count]!=null && value[count].length() > 0){

sum = sum + new Double(value[count]).doubleValue();

}

}

result.addValue(new String().valueOf(sum));}

This UDF takes care even if the input file misses value on the input side ....

Note: use square bracket before and after count in if block. This editor deleting it.

Baskar