cancel
Showing results for 
Search instead for 
Did you mean: 

Need help in UDF

former_member229310
Active Participant
0 Kudos

Hello Gurus,

Could you please help me if v need any UDF for splitting or can i do it with graphical mapping.

Below is the source structure

<header>id123</header>

<item>item123</item>

<data>abd1232011

^^efg4562011

^^hij7892012</data>

Required target structure

<header>id123</header> 1:1

<item>item123</item> 1:1

<content> 1:1

<data> 1:unbounded

<char>abd</char>

<num>123</num>

<date>2011</date>

</data>

my requirement is to break the 1 source field "data" into 3 target fields "char","num","date". Field data is of type string and received as a raw string. The new line is determined by "^^" in the string. fields are separated by "~"

Kindly help

Thanks in advance.

Accepted Solutions (0)

Answers (5)

Answers (5)

Former Member
0 Kudos

Hi,

I think it can easily be done by graphical mapping. PFB the mapping

Source1 -> substring(1,3) -> CharField

Source1 -> substring(4,7) -> IntegerField

Source1 -> substring(7,11) ->FormatDate -> DateField

Hope this helps.

Former Member
0 Kudos

Hi,

For this you don't need UDF. Use copyValue constant type function and map like below.

<data> - CopyValue [0] - <char>

<data> - CopyValue [1] - <num>

<data> - CopyValue [2] - <date>

In CopyValue function double click and give the indexes (0 for fitst one, 1 for second one and 2 for third one).

Regards,

Venkata Ramesh

former_member229310
Active Participant
0 Kudos

Thanks for your reply...

Does this also take care of new line character "^^" present in the string.... I am expecting multiple rows from the string

Former Member
0 Kudos

>>Does this also take care of new line character "^^" present in the string.... I am expecting multiple rows from the string

splitting wont be possible without UDF...

BTW did u chk the code which i have pasted above??

Former Member
0 Kudos

Hi,

The copyValue function will copy the first value in the field data to the first field, second value in data to second & so on depending on the index in copyValue. For eg

Field1->Val1

Field2->val2

Field2->val3

Field3->val4

here for different values for repeating field Field2 you will use copyvalue & depending on index ie0,1,2 the three values will be populated, but I think in your case all the values are coming under one field right? And the values are separated by ^^..Copyvalue can take values from multiple repetition of a single field into different target fields, but I could not figure out how will it take value from a single field. CopyValue would have been applicable if you had multiple data items and if the values were to be populated from each single instance of field data. Better you go with a UDF.

Edited by: addhingr on Mar 7, 2012 9:55 AM

Edited by: addhingr on Mar 7, 2012 10:09 AM

Former Member
0 Kudos

As the split should happen at data node (^^) and field (~), Its better to go with the code by amit. CopyValue may not work in this case as the input is in single field and no indexes are maintained in the data node.

Former Member
0 Kudos

which version of PI u r working on??

In case ur version is PI7.1 (or greater) then chk this code:

UDF1:

Input will be: var1 of type argument

and add 3 result type with name result1, result2, result3

Execution type: all values of a context


String [] temp = var1[0].split("^^");
for(int i=0;i<temp.length;i++)
{
String [] split1 = temp<i>.split("~");
result1.addValue(split1[0]);
result2.addValue(split1[1]);
result3.addValue(split1[2]);
result1.addContextChange();
result2.addContextChange();
result3.addContextChange();
}

UDF2:


String [] temp = var1[0].split("^^");
for(int i=0;i<temp.length;i++)
result.addValue("");

mapping:


Input----UDF2---data


                       -----char
Input----UDF1 -------num
                    -------date

former_member229310
Active Participant
0 Kudos

Hello all,

after compiling amit code i get below errors, please suggest

1) array required, but java.lang.String found

String [ ] temp = var1[0].split("^^");

2) cannot find symbol

symbol : method addValue(java.lang.String)

location: class java.lang.String

result1.addValue(split1[0]);

former_member229310
Active Participant
0 Kudos

I have resolved the syntax errors...But i am not returning any value from the below function.

String[] temp = var1[0].split("^^");

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

result.addValue("");

Input : ABC1232011^^DEF4562012

Former Member
0 Kudos

>>But i am not returning any value from the below function

this code (UDF2) is used to repeat target "data" node multiple times depending upon the presence of "^^" in the input string....

ur mapping output will be something like this ( correct me if i misunderstood ur req):

<data>

<char>abd</char>

<num>123</num>

<date>2011</date>

</data>

<data>

<char>efg</char>

<num>456</num>

<date>2011</date>

</data>

<data>

<char>hij</char>

<num>789</num>

<date>2012</date>

</data>

Former Member
0 Kudos

And if you need UDF for splitting, here they are:

Former Member
0 Kudos

Are you sure that the fields abd-123-2011 will be of 3,3 & 4 digits only.If yes you may not need a UDF and can use substring mapping function & can map as data-> substring(0,2)->char substring(4,6)->num substring(8,11)->date...the field data will be mapped to three substrings with values as above & the substrings will be passed to the respective fields.The position corresponding to ~ has not been included.But you may need a UDF for splitting values by ^^

Edited by: addhingr on Mar 7, 2012 7:57 AM

Edited by: addhingr on Mar 7, 2012 10:10 AM