on 09-14-2015 4:25 PM
Hi Experts,
I have one source, two target fields.
logic:
if source value length < 40, map source to targetone , send empty string to targettwo
else if (source value length > 40), map subString(0,40) to targetone, remaining value to targettwo
else pass empty string to both target fields.
I want to try UDF for this requirement, Please help me with the code.
Mohan,
Please use UDF execution type 'All values of a context'.
public void udf_split(String[] in, ResultList out1, ResultList out2, Container container) throws StreamTransformationException {
in[0] = in[0] + " "; //To avoid ArrayIndexOutOfBoundsException
out1.addValue(in[0].substring(0, 40).trim());
out2.addValue(in[0].substring(40).trim());
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Mohan,
Pls try the below udf and check the outcome:
Code snippet:
if(input[0].length()<=40)
{
output1.addValue(input[0]);
output2.addValue("");
}
else if(input[0].length()>40)
{
output1.addValue(input[0].substring(0,40));
output2.addValue(input[0].substring(40));
}
else
{
output1.addValue("");
output2.addValue("");
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Mohan,
I hope the above examples helped you. But in addition to this, What if the source string length is 200 char or more? you just need take remaining 120 char (after 40 char) and populate target2 field? or you need take 40 char a time and then keep creating the Target2 filed until string is completed. If this is the case , use the below UDF. Use Execution Type as All values of Queue.
public void Nodesplit(String[] input, ResultList result, Container container) throws StreamTransformationException{
String str=input[0];
while(str.length()>40)
{
result.addValue(str.substring(0,40));
str=str.substring(5,str.length());
}
result.addValue(str);
}
With this UDF you can split the source value to multiple Target field, In Case remaining source values need to be copied to target fields (you want repeat the target structure based source string length which is more than 40 char), You can use the nodefunction UseOneAsMany and SplitByValue (each Value) combination.
Hope this helps too..
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
93 | |
10 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.