cancel
Showing results for 
Search instead for 
Did you mean: 

Generic UDF ?

Former Member
0 Kudos

Hi Experts ,

I am not sure if this can be dealt with in message mapping or do i have to write a UDF for it

if there is any1 who can help me with the UDF it would be great

the requirement is that i am receiving a string which is a combination of n strings seperated by @ as a delimiter

e.g

(abc@frgthj@234)

or (abc@frgthj)

or (abc@234)

my requirement is to pass the first part abc to one target element and the second part frgth to the other target element

basically strings could be 2 or 3 but maximum 4 in number but the seperator will be @ and the requirement is to just pick up the first string(coming of any length) and map it to target 1

the 2nd string of any length and map it to target2 i.e reading the string before @ and mapping to target structures

Can anyone help me build this UDF , m sure it can be a generic one

Thanks

Rohan

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

Create generic UDF with following input "Value" "separator", "fieldplace"

So in your case separator = "@" and fieldplace will be either "1", "2" or n.

Write following code:

//==================

int index = Integer.parseInt(fieldplace);

for(int i = 1; i<=index ; i++)

{

Value = Value.subString(Value.indexOf("@"),Value.lenght());

}

return Value;

//====================

One UDF will be sufficient to decode any value with any delimiter for any number of field.

Disclaimer: check for syntax error and I have not tested it.

Regards,

Gourav

Former Member
0 Kudos

Hi Gaurav

Thanks , however I am getting an exception

" the length 0 of the array 'sortedFunctionKeys' is not equal to the number 1 of functions. "

also if I test it with the code u provided with an example

value =rohan@das

seperator=,

fieldplace=1

I am not getting the desired result as rohan

same if i change fieldplacefor 2 im not getting das

my requirement is simple

I will always get a string of maximum 2 or 3 fields(different lengths) seperated by a @

and i need to extract these individually , can you please help me

thanks

Rohan

santhosh_kumarv
Active Contributor
0 Kudos

Hi use this..

public String UDF(String input, Container container) 
{
String temp[] = input.split("@");
return temp[0];
}

use temp[0] & temp[1] in for two different target...

~SaNv...

Former Member
0 Kudos

Rohan,

Did you try the udfs I have given above. I tested on my system and they are working. You can give a try and if it doesnot work then somebody can help you. You can also try to combine the two udfs into one udf also.

For your question you may need to check @ with the function exists. If exists then you may need to do one logic else you may do other logic.

Regards,

---Satish

Former Member
0 Kudos

HI Santosh

it gives and ERROR WHILE EXECUTING SAVE

" the length 1 of the array 'sortedFunctionKeys' is not equal to the number 2 of functions "

this happens when im trying to save the UDF for testing purpose

i am unaware why this is happening

Former Member
0 Kudos

Yes Satish

thanks I am trying the same but unableto save the UDF's while mapping and it gives the errro as i mentioend above

Error while Executing Save

" the length 1 of the array 'sortedFunctionKeys' is not equal to the number 2 of functions "

which is why now i am unable to test unless this exception is resolved

Former Member
0 Kudos

Can any one please guide me how to deal with this mapping exception while trying to save the mapping ?

I am unable to save the UDFand it gives an error while trying to execute save

" the length 1 of the array 'sortedFunctionKeys' is not equal to the number 2 of functions "

if anyone has encountered the same before or knws what could be the reason please let me know

thanks

Rohan

Former Member
0 Kudos

Hi Rohan,

In my suggestion separator is also generic field, don't give "," as separator but "@" as separator for input.

It will work fine (if you have atleast one "@") in input.

Regards,

Gourav.

Former Member
0 Kudos

Hi Rohan,

It seems there is some error in one of your udf. Please check where you used sortedFunctionKeys and delete that function and give it a try.

Regards,

---Satish

Former Member
0 Kudos

No No Gaurav

It was a typo earlier i am using @ as the seperator as an input to the udf

my issue is the exception that i am receiving as mentioned

and i dont have any sortedFunctionKeys used anywhere in my functions and am unableto catch the cause of this exception

due to which i cant test the udf's in the mapping now

as mentioned by Satish i checked my udf's none use sortedFunctionKeys

markangelo_dihiansan
Active Contributor
0 Kudos

Hi,

I've modified the code given by Santosh:


For the first node, do this mapping:

sourceNode --> UDF --> copyValue[0] --> targetNode (1)

For the second node, do this mapping:

sourceNode --> UDF --> copyValue[1] --> targetNode (2)

Here is the code, UDF type is CONTEXT Type
Argument is input

String b[] = input[0].split("@");
for (int a=0;a<b.length;a++){result.addValue(b[a]);}

Hope this helps,

Answers (4)

Answers (4)

GabrielSagaya
Active Contributor
0 Kudos
public String SPLITUDF(String a, String b, Container container){
String flag="";
String c="";
String d="";
String first="";
String second="";
first=a.substring(0,a.indexOf("@"));
c=a.substring(a.indexOf("@")+1);
if (c.indexOf("@")==-1)
flag="Y";
else
flag="Z";

if (flag=="Y")
second=c.substring(a.indexOf("@")+1);
else
second=c.substring(0,c.indexOf("@"));

 if (b == "1")
return first;
else if (b == "2")
return second;
}

Use the Same UDF for both different fields that is splitted based on constants.

input-------------> SplitUDF -------------> First
Constant(1)---->

input-------------> SplitUDF -------------> Second
Constant(2)---->

Former Member
0 Kudos

As you are saying it can vary then better to go for stringTokenizer class of java which will separate the strings based on the token provided ...

Google it if need more help and code for the same

Rajesh

prateek
Active Contributor
0 Kudos

You may use indexOf function to find the location of @. Then you may use substring to take the value before @ and after @. Then you may apply the same thing for the remaining string. You may check in second case that if @ is present, only then perform the same indexOf and substring.

Regards,

Prateek

Former Member
0 Kudos

Rohan,

In your first example (abc@frgthj@234) abc will assigned to field1 and frgthj will be assigned to field2. Am I correct? If yes proceed like this:

For field1:

Create a value udf and take one input parameter 'a' and name as field1udf. Then add this code:

Imports: java.;*

int i = a.indexOf("@");

a = a.substring(0,i);

return(a);

Then map like this: source --> field1udf --> target (this will give the output as abc)

For field2:

Create a value udf and take three input parameter 'a' 'b' and 'c' and name as field2udf. Then add this code:

Imports: java.;*

int i = Integer.parseInt(a);

int j = Integer.parseInt(b);

String d = c.substring(j+1,i);

return(d);

Then map like this:

source --> length --> is the first input to field2udf

source-->indexof2 (text function) with constant @ is the second input to field2udf

source --> is the third input to field2udf

Then map the above to field2udf --> field1udf --> target. this will give the output as frgthj.

Regards,

---Satish