简体   繁体   中英

Why my map reduce program is giving me output in Text when i have specified IntWritable

My test set is :

Onida|Lucid|18|Uttar Pradesh|232401|16200
Akai|Decent|16|Kerala|922401|12200
Lava|Attention|20|Assam|454601|24200
Zen|Super|14|Maharashtra|619082|9200
Samsung|Optima|14|Madhya Pradesh|132401|14200

My mapper class:

public class UnitsSoldPerCompanyMapper extends Mapper<LongWritable,Text,Text,Text>{

    public void map(LongWritable inputKey, Text inputValue,Context context) throws IOException, InterruptedException{
        String[] lineArray= inputValue.toString().split("\\|");
        Text companyName = new Text(lineArray[0]);
        Text productName = new Text(lineArray[1]);
        context.write(companyName,productName);
    }
}

Reducer class:

public class UnitsSoldPerCompanyReducer extends Reducer<Text,Iterable<Text>,Text,IntWritable>{

    public void reduce(Text companyKey,Iterable<Text> productName,Context context) throws IOException, InterruptedException{

        IntWritable counter1= new IntWritable();
        int counter =0;

        for(Text values : productName ){
            System.out.println(values);
            counter++;
        }
        counter1.set(counter);
        //IntWritable sum= new IntWritable(counter);
        context.write(companyKey, new IntWritable(1));
    }
}

Driver class:

public class UnitsSoldPerCompanyDriver {

public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

    Configuration conf = new Configuration();// To set job related
                                                // configuration

    // @SuppressWarnings("deprecation")
    @SuppressWarnings("deprecation")
    Job job = new Job(conf, "TaskofJob");
    job.setJarByClass(UnitsSoldPerCompanyDriver.class);

    // Job job = new Job(conf,"TvSalesAcrossLocations");

    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));

    job.setMapperClass(UnitsSoldPerCompanyMapper.class);
    job.setReducerClass(UnitsSoldPerCompanyReducer.class);

    BasicConfigurator.configure();
    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(Text.class);

    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);

    // job.setInputFormatClass(TextInputFormat.class);
    // job.setOutputFormatClass(TextOutputFormat.class);

    job.waitForCompletion(true);

}

I am getting out put as:

Akai    Decent
Lava    Attention
Lava    Attention
Lava    Attention
NA  Lucid
Onida   NA
Onida   Decent
Onida   Lucid
Onida   Lucid
Samsung Super
Samsung Super
Samsung Super
Samsung Decent
Samsung Optima
Samsung Optima
Samsung Optima

Whereas, I am trying to find the units sold per company.

I believe that output is generated by the identity (default) reducer (which just outputs the mapper keys and values with tabs), instead of yours. Not sure why is that but I suspect BasicConfigurator.configure();

In the ResourceManager UI, you can verify mapred.reducer.class, go to the job and on the left menu you can see the actual job properties that where used.

I had by mistakely extended Reducer,Text,IntWritable>, which was supposed to be Reducer

This was not pointed at compile time since i had not used @Override annotation for reduce method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM