Skip to Content
author's profile photo Former Member
Former Member

Splitting filed

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.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • Best Answer
    Posted on Sep 14, 2015 at 04:15 PM

    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());
        }
    
    
    

    udf.PNG (190.3 kB)
    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Sep 14, 2015 at 04:03 PM

    Hello Mohan,

    Try the below Logic,

    For Target Field 1:

    For Targe Field 2:

    UDF1:

    int L=var1.length();

    var1=var1.substring(40,L);

    return var1;

    Let me know if you wish to add more logic in this.


    Untitled.png (15.0 kB)
    Untitled.png (19.1 kB)
    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Sep 14, 2015 at 06:46 PM

    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..

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Sep 15, 2015 at 09:40 AM

    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("");

    }


    11.PNG (9.5 kB)
    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.