Tuesday 17 November 2015

Spoon User Defined Java Class Transformation with example.


Welcome

Pentaho Data Integration Suit: Spoon User Defined Java Class Transformation.

This section provide you a basic idea about how to implement a java transformation in Spoon.

Requirement: Splitting effective start date and end date on  yearly basis

Source table:







Expected output:










Transformation:

We can use a java transformation to implement this logic in Spoon.














Java code used inside the User Defined Java Class


import java.util.Date;
import java.util.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
static RowMetaInterface inputRowMeta;

public boolean processRow(StepMetaInterface smi, StepDataInterface sdi)
{

try
{


    // First, get a row from the default input hop

    Object[] r = getRow();

    // If the row object is null, we are done processing.
    //
    if (r == null)
{
      setOutputDone();
      return false;
    }

    // It is always safest to call createOutputRow() to ensure that your output row's Object[] is large
    // enough to handle any new fields you are creating in this step.
    //
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal1 = Calendar.getInstance();//create a empty calendar variable
Calendar cal2 = Calendar.getInstance();
    Calendar cal3 = Calendar.getInstance();
    int st_yr, ed_yr, Cu_yr, st_mon, ed_mon, st_date, ed_date, st_ldm, ed_ldm,stcount, count;
    String str,end,OUT_SVC_ST_DT,OUT_SVC_END_DT;

    Date st_dt = get(Fields.In, "Eligibilty_Begin_Date").getDate(r);//get the start and end date from the source
    Date ed_dt = get(Fields.In, "Eligibility_End_Date").getDate(r);
    cal1.clear();
    cal1.setTime(st_dt);//initialise the calendar variable acc to the source table
    cal2.clear();
    cal2.setTime(ed_dt);

st_yr = cal1.get(Calendar.YEAR);//get the year in int format
    ed_yr = cal2.get(Calendar.YEAR);
    Cu_yr = cal3.get(Calendar.YEAR);
count=(ed_yr-st_yr);

st_mon = cal1.get(Calendar.MONTH)+1;//get the month int format
    ed_mon = cal2.get(Calendar.MONTH)+1;

do
    {
 
        Object[] outputRow = RowDataUtil.createResizedCopy(r, data.outputRowMeta.size());
        OUT_SVC_ST_DT = formatter.format(st_dt);
get(Fields.Out, "Start_Date").setValue(outputRow, OUT_SVC_ST_DT);
        if(ed_yr != st_yr)
        {
            str = "31/12/" + st_yr;
            st_dt = (Date)formatter.parse(str);
            cal1.setTime(st_dt);
            OUT_SVC_END_DT = formatter.format(st_dt);

        }
        else
OUT_SVC_END_DT = formatter.format(ed_dt);
get(Fields.Out, "End_Date").setValue(outputRow, OUT_SVC_END_DT);
        putRow(data.outputRowMeta, outputRow);
     
        st_yr = st_yr + 1;
        str = "01/01/" + st_yr;
        st_dt = (Date)formatter.parse(str);
        cal1.setTime(st_dt);
        st_yr = cal1.get(Calendar.YEAR);

    }while(ed_yr >= st_yr);
}

catch (Exception e)
   {
throw new KettleException("An unexpected error occurred creating the new transformation", e);
    }
 return true;
}









1 comment: