Akshay Hazari
Akshay Hazari

Reputation: 3267

Multiple Inputs and multiple Mapper classes in EMR (Is there anything similar in EMR to MultipleInputs on Hadoop)

I have used MultipleInputs while using hadoop . In that I had multiple Mappers assigned to different inputs. I want to know whether it is supported in EMR as well.

In hadoop I have done it like this. These are my mappers for different files. Here I need these because I have to perform some operations on different inputs which are supposed to identify the inputs separately and perform separate operations in the reducer.

public static class Map1 extends Mapper<Object, Text, Text, Text> {
Text out=new Text();

Text value1= new Text();
public void map(Object key,Text value,Context context) throws IOException,InterruptedException
    {
    try
        {
        String line= value.toString();
        Configuration conf=context.getConfiguration();
        Float CVsTime=conf.getFloat("CVstartTime",0);
        String dimension=conf.get("CVdimension");
        String CVfilter=conf.get("CVfilters");
        Float CVeTime=conf.getFloat("CVendTime",0);
        Float CVstartTime=CVsTime;
        Float CVendTime=CVeTime;
        JSONParser parser = new JSONParser();
        Object obj=parser.parse(line);
        JSONObject jsonObject=(JSONObject)obj;
        Object datasttime=jsonObject.get("client_received_start_timestamp");
        String ddimension="";
        Object odimension=jsonObject.get(dimension);
        if(odimension!=null)
            ddimension=odimension.toString();
        String dst=datasttime.toString();
        dst=dst.substring(0,6)+"."+dst.substring(6,dst.length());                 
        String metric=conf.get("CVmetric");
        Float tim=0.0f,/* sttime=0,endtime=0,*/CVval=0.0f;
        tim=Float.parseFloat(dst.toString());
        Object met=jsonObject.get(metric);
        CVval=Float.parseFloat(met.toString());
        int CVfiltercount = CVfilter.length() - CVfilter.replace(" ", "").length();

        String CVfilters[][]=new String[CVfiltercount][];
        StringTokenizer tokenizer=new StringTokenizer(CVfilter);
        int k=0;
        while(tokenizer.hasMoreTokens())
            {
            String temptoken=tokenizer.nextToken();
            if(temptoken.indexOf("=")!=-1)
                {
                CVfilters[k]=temptoken.split("=");
                CVfilters[k][1]=CVfilters[k][1].replace("\"","");
                k++;
                }
            }
        int count=k;
        int flag=0;
        for(int i=0;i<k;i++)
            {
            Object filter=jsonObject.get(CVfilters[i][0]);
            if(filter==null)
                {
                flag=1;
                break;
                }
            if(!filter.toString().equals(CVfilters[i][1]))
                {
                flag=1;
                break;
                }
            }
        if((odimension!=null)&&(CVstartTime<=tim)&&(CVendTime>=tim)&&(flag==0))
            {
            value1.set("key1"+" "+tim.toString()+" "+CVval.toString());
            out.set(ddimension);
            context.write(out,value1);
            }
        flag=0;
        }
    catch(Exception e)
        {
        e.printStackTrace();
        }
    }
}
public static class Map2 extends Mapper<Object, Text, Text, Text> 
{
    Text out = new Text();
    Text value2= new Text();
public void map(Object key,Text value,Context context) throws IOException,InterruptedException
    {
    try
        {
        Configuration conf=context.getConfiguration();
        Float CTVstartTime=conf.getFloat("CTVstartTime",0);
        Float CTVendTime=conf.getFloat("CTVendTime",0);
        String CTVfilter=conf.get("CTVfilters"); 
        String dimension=conf.get("CTVdimension");
        String line= value.toString();
        JSONParser parser = new JSONParser();
        Object obj=parser.parse(line);
        JSONObject jsonObject=(JSONObject)obj;
        Object datasttime=jsonObject.get("client_received_start_timestamp");
        Object odimension=jsonObject.get(dimension);
        String ddimension="";
        if(odimension!=null)
            ddimension=odimension.toString();
        String dst=datasttime.toString();
        dst=dst.substring(0,6)+"."+dst.substring(6,dst.length());                 
        String metric=conf.get("CTVmetric");
        Float tim=0.0f,/*sttime=0,endtime=0,*/ctvvalue=0.0f;        
        StringTokenizer st=new StringTokenizer(line);
        tim=Float.parseFloat(dst.toString());
        Object met=jsonObject.get(metric);
        ctvvalue=Float.parseFloat(met.toString());
        int CTVfiltercount = CTVfilter.length() - CTVfilter.replace(" ", "").length();
        StringTokenizer tokenizer=new StringTokenizer(CTVfilter);
        String CTVfilters[][]=new String[CTVfiltercount][];
        int k=0;
        while(tokenizer.hasMoreTokens())
            {
            String temptoken=tokenizer.nextToken();
            if(temptoken.indexOf("=")!=-1)
                {
                CTVfilters[k]=temptoken.split("=");
                CTVfilters[k][1]=CTVfilters[k][1].replace("\"","");                 
                k++;
                }
            }
        int count=k;
        int flag=0;
        for(int i=0;i<k;i++)
            {
            Object filter=jsonObject.get(CTVfilters[i][0]);
            if(filter==null)
                {
                flag=1;
                break;
                }
            if(!filter.toString().equals(CTVfilters[i][1]))
                flag=1;

            }
        if((odimension!=null)&&(CTVstartTime<=tim)&&(CTVendTime>=tim)&&(flag==0))
            {
            value2.set("key2"+" "+tim.toString()+" "+ctvvalue.toString());
            out.set(ddimension);
            context.write(out,value2);
            }
        }
    catch(Exception e)
        {
        e.printStackTrace();
        }
    }
}

This part of my main where I have used MultipleInputs in hadoop. Here I have set a separate mapper class for different inputs i.e Map1.class and Map2.class

job.setJobName("alert");
String MapPath1[]=args[1].split(",");
String MapPath2[];
MapPath2 = type.equals("comparative") ? args[2].split(",") : null;

Path outputPath;
if (MapPath2!=null)
    outputPath = new Path(args[3]);
else
    outputPath = new Path(args[2]);
job.setMapperClass(Map1.class);
if(type.equals("comparative"))
    job.setMapperClass(Map2.class);
job.setReducerClass(Reduce.class);
job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(NullWritable.class);
job.setOutputValueClass(Text.class);
for(int i=0;i<MapPath1.length;i++)
    MultipleInputs.addInputPath(job,new Path(MapPath1[i]),TextInputFormat.class,Map1.class);
if(type.equals("comparative"))
    for(int i=0;i<MapPath2.length;i++)
    MultipleInputs.addInputPath(job,new Path(MapPath2[i]),TextInputFormat.class,Map2.class);
FileOutputFormat.setOutputPath(job, outputPath);

Here I am taking two different input paths and assigning them different Mappers as defined above and It works perfect. I am asked to find out whether the same is possible in EMR and I haven't done anything on EMR before.I tried googling it but it couldn't find anything useful. I wish to know if there is anything equivalent to that on EMR or any workaround would do. Except that I don't wish to use (Path filePath = ((FileSplit) context.getInputSplit()).getPath();) anything where I am trying to find the path of the current input to determine which chunk of data or file it belongs to.

Any Help is appreciated.

Upvotes: 0

Views: 330

Answers (1)

samthebest
samthebest

Reputation: 31543

Of course it's supported, EMR is just where your running Hadoop. Your question is equivalent to saying "can I use a web browser on both a laptop and on a desktop". Well that's what I understand from your question.

http://docs.aws.amazon.com/ElasticMapReduce/latest/DeveloperGuide/emr-plan-hadoop-differences.html

Upvotes: 1

Related Questions