Skip to Content
avatar image
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 comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Best Answer
    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 comment
    10|10000 characters needed characters exceeded

  • 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 comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    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 comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    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 comment
    10|10000 characters needed characters exceeded