on 01-11-2011 12:08 PM
Hi,
I have the below requirement.
For eg: I have the values in the Input Queue(1st column) and output values should be in the (2nd column) as shown below
10 - 10
10 - 20
04 - 10
04 - 20
04 - 30
10 - 30
04 - 40
04 - 50
i.e the values should be incremented by 10... and and it shd increment for the same value
for the 1st two occurances of 10... value was 10, 20, and 10 has occurred again, it was incremented by 10 as 30
when a new value 40 has come... incremented started by 10... same as above ... incremented by 10 wherever 40 occurs
So I need a UDF to achieve the same... I tried using graphical. using Index function but did not work
Thanks,
Varun
Try this:
public static void createCounter(String[] value, ResultList result, Container container){
Vector<String> valueVector = new Vector<String>();
Vector<BigDecimal> counterVector = new Vector<BigDecimal>();
BigDecimal counter10 = new BigDecimal(10);
// create vector from input array
for (int i = 0; i < value.length; i++ ){
int vectorIndex = valueVector.indexOf(value<i>);
// if tax code not in vector so far
if (vectorIndex == -1){
// add tax code and amount to vectors
valueVector.addElement(value<i>);
counterVector.addElement(counter10);
result.addValue("10");
} else {
// add the amount to the vector element corresponding to tax code
BigDecimal currentCounter
= ((BigDecimal) counterVector.get(vectorIndex)).add(counter10);
counterVector.set(vectorIndex,currentCounter);
result.addValue(currentCounter.toString());
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Varun,
You can define a global variable Under Functions Tab ->Attributes and methods as :
Map<String,String> counterMap = new HashMap<String,String>();
Now create a UDF and pass the key value (values in column 1 mentioned by you) to the UDF and map it to whatever output field you want i.e.
key -> createCounter (UDF) -> outputValue
the logic for the UDF would be:
public String createCounter(String key, Container container) throws StreamTransformationException{
if(counterMap.isEmpty() || (! (counterMap.containsKey(key)) ) ){
counterMap.put(key,"10");
return "10";
} else{
String value = counterMap.get(key);
int valueAsInt = Integer.parseInt(value) + 10;
String valueString = String.valueOf(valueAsInt);
counterMap.put(key, valueString);
return valueString;
}
} //end of method create Counter
Hope this solves your problem.
Best Regards,
Pratik
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Map<String,String> counterMap = new HashMap<String,String>();
After searching Java forums for comparison of Vector and HashMap, I found that HashMap is faster.
So I changed my code to this:
public static void createCounter(String[] value, ResultList result, Container container){
HashMap<String,BigDecimal> valueMap = new HashMap<String,BigDecimal>();
BigDecimal counter10 = new BigDecimal(10);
// create vector from input array
for (int i = 0; i < value.length; i++ ){
BigDecimal counter = valueMap.get(value<i>);
// if tax code not in vector so far
if (counter == null){
// add tax code and amount to vectors
valueMap.put(value<i>,counter10);
result.addValue("10");
} else {
// add the amount to the vector element corresponding to tax code
counter = counter.add(counter10);
valueMap.put(value<i>,counter);
result.addValue(counter.toString());
}
}
}
Hi
I dono why you are going to UDF
You can easily achieve this by doing below steps with graphical funcitons...
a) First Sort the input queue based on number...by using SORT function present in Nodefuncitons..
b) then use SPLIT BY VALUE CHANGE node function
c) then Use INDEX Function present in STATIC Graphical function and in this u can give increment of 10 and select the radio button of Reset index to inital value once the context changes...
Note: - Once you are sorting then definetely the remaining target segments contexts also needs to handle based on this context..
Regards
Amar Srinivas Eli
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
101 | |
13 | |
13 | |
11 | |
11 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.