package com.amazonaws.vivanih.hadoop.cascading;

import java.util.Properties;
import java.util.Calendar;
import java.text.SimpleDateFormat;

import cascading.flow.Flow;
import cascading.flow.FlowConnector;
import cascading.flow.FlowDef;
import cascading.flow.hadoop.HadoopFlowConnector;
import cascading.operation.aggregator.Count;
import cascading.operation.aggregator.Average;
import cascading.operation.regex.RegexSplitGenerator;
import cascading.operation.regex.RegexGenerator;
import cascading.operation.text.DateParser;
import cascading.operation.text.DateFormatter;
import cascading.tap.SinkMode;
import cascading.pipe.Each;
import cascading.pipe.Every;
import cascading.pipe.GroupBy;
import cascading.pipe.Pipe;
import cascading.pipe.assembly.Unique;
import cascading.property.AppProps;
import cascading.scheme.Scheme;
import cascading.scheme.hadoop.TextDelimited;
import cascading.tap.Tap;
import cascading.tap.hadoop.Hfs;
import cascading.tuple.Fields;


public class
  Main
  {
  public static void
  main( String[] args )
    {
    String inPath = args[ 0 ];
    String outPath = args[ 1 ];

    Properties properties = new Properties();
    AppProps.setApplicationJarClass( properties, Main.class );
    FlowConnector flowConnector = new HadoopFlowConnector( properties );

    //scheme definition:
    Scheme inScheme = new TextDelimited( new Fields("LoadId", "MRP", "ServicerName", "CIR", "UPB", "LoanAge", "RMLM" , "ARMM", "MadurityDate", "MSA", "CLDS", "ModificationFlag", "ZBC", "ZBED", "RepurchaseIndicator"), "|");
    Scheme outScheme = new TextDelimited( new Fields( /*"year",*/ "month", "UPBaverage"),"\t" ) ;

    // create source and sink taps
    Tap inTap = new Hfs( inScheme, inPath );
    Tap outTap = new Hfs( outScheme, outPath );
    //Tap outTap = new Hfs( new TextDelimited(true, "\t"), outPath );

    //convert date to "ts" from MRP field
    DateParser dateParser = new DateParser( new Fields( "ts" ), "MM/dd/yyyy" );

    //Parsing the date. (0 is January)
    //DateParser dateParser= new DateParser(new Fields("month", "day", "year"), new int[] { Calendar.MONTH, Calendar.DAY_OF_MONTH, Calendar.YEAR }, "MM/dd/yyyy");
    //parse pipe will parse the MRP field into month, day, year. /with Fields.ALL I get original fields + new fields. With Fields.RESULTS, I get only the date parsed.
    Pipe parsePipe = new Each("parsePipe", new Fields("MRP"), dateParser , Fields.ALL);

    //change the format from "ts" to date required
    DateFormatter formatter = new DateFormatter( new Fields( "date" ), "dd/MMMM/yyyy" );
    parsePipe = new Each( parsePipe, new Fields( "ts" ), formatter, Fields.ALL );

    //regex to extract the month in MMMM format:
    RegexGenerator splitter=new RegexGenerator(new Fields("month"),"(?<!\\pL)(?=\\pL)[^ ]*(?<=\\pL)(?!\\pL])");
    parsePipe = new Each( parsePipe, new Fields( "date" ), splitter, Fields.ALL );

    //Pipe to filter duplicates. It is like select distinct. It is like using combiners.
    //http://docs.cascading.org/cascading/2.2/javadoc/cascading/pipe/assembly/Unique.html
    Pipe uniquePipe = new Unique("unique", parsePipe, new Fields("LoadId","MRP"));

    // aggregate by year, month. input is previous parsePipe
    Pipe averagePipe = new GroupBy( "averagePipe", uniquePipe, new Fields(/*"year",*/ "month" ));
    // average each aggregation
    averagePipe = new Every(averagePipe, new Fields("UPB"), new Average(new Fields("UPBaverage")), Fields.ALL );


    // connect the taps, pipes, etc., into a flow
    FlowDef flowDef = FlowDef.flowDef()
    .addSource( averagePipe, inTap )
    //.addSource( parsePipe, inTap )
    .addTailSink(averagePipe, outTap );
    //.addTailSink(parsePipe, outTap );
    // run the flow
    flowConnector.connect( flowDef ).complete();
    }
  }

@@ -467,32 +467,78 @@
 RegexGenerator;%0A
+import cascading.operation.regex.RegexFilter;%0A
 import cascading
@@ -1004,16 +1004,17 @@
 elds;%0A%0A%0A
+%0A
 public c
@@ -2918,24 +2918,203 @@
 lds.ALL );%0A%0A
+    //filterning tuples with null at UPB field%0A    RegexFilter nullfilter = new RegexFilter( %22%5E$%22 ,true);%0A    parsePipe = new Each( parsePipe, new Fields( %22UPB%22 ), nullfilter );%0A%0A
     //Pipe t
@@ -3288,18 +3288,12 @@
     
-Pipe uniqu
+pars
 ePip
@@ -3311,18 +3311,8 @@
 que(
-%22unique%22, 
 pars
@@ -3338,21 +3338,169 @@
 LoadId%22,
+ 
 %22MRP%22
+, %22ServicerName%22, %22CIR%22, %22UPB%22, %22LoanAge%22, %22RMLM%22 , %22ARMM%22, %22MadurityDate%22, %22MSA%22, %22CLDS%22, %22ModificationFlag%22, %22ZBC%22, %22ZBED%22, %22RepurchaseIndicator%22
 ));%0A%0A   

Update Main.java