Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]
Created on 09-12-2015 11:29 PM - edited 09-16-2022 02:40 AM
i,
I have been trying to do a word count programme, which emmits only 1 key value , ie the last key value pair in the input file using wordcount mapreduce programme.
Here is the content of the input file in a directory :
a.txt :
====
f f g h
i i j k
l l m r
f f h h
Content of b.txt
========
r r g h
h h m m
c c b b
d d r f
O/p should be :
r 4
Here is my sample mapper code & reducer code for simple word count. Can anyone tell me what changes should I make to get th o/p like above :
Mapper code:
--------------------
public class WcMapper extends Mapper<LongWritable,Text,Text,IntWritable>{
private static final IntWritable one= new IntWritable(1);
private final Text word=new Text();
public void map(LongWritable key,Text value, Context context
) throws IOException, InterruptedException
{
StringTokenizer st =new StringTokenizer(value.toString());
while(st.hasMoreTokens()){
word.set(st.nextToken());
context.write(word, one);
}
}
}
Reducer code :
---------------------
public void reduce(Text key,Iterable<IntWritable> values, Context context ) throws IOException, InterruptedException{
int sum=0;
for(IntWritable value:values){
sum+= value.get();
}
context.write(key, new IntWritable(sum));
}
}
Driver code::
-----------------------
public class WcDriver extends Configured implements Tool{
public static void main(String[] args) throws Exception {
int status = ToolRunner.run(new WcDriver(), args);
System.exit(status);
}
@Override
public int run(String[] args) throws Exception {
Configuration c1=new Configuration();
Job j1= new Job(c1,"woc");
j1.setJarByClass(WcDriver.class);
j1.setMapperClass(WcMapper.class);
j1.setReducerClass(WcReducer.class);
j1.setInputFormatClass(TextInputFormat.class);
j1.setOutputFormatClass(TextOutputFormat.class);
j1.setOutputKeyClass(Text.class);
j1.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(j1, new Path(args[0]));
FileOutputFormat.setOutputPath(j1, new Path(args[1]));
FileSystem fs = FileSystem.newInstance(c1);
if (fs.exists(new Path(args[1]))) {
fs.delete(new Path(args[1]), true);
}
return j1.waitForCompletion(true) ? 0 : 1;
}
}
Appreciate all help.Please help....
Created 09-20-2015 06:08 AM
Created 09-18-2015 04:31 AM
Created 09-20-2015 12:43 AM
HI,
Normally as per the i/p I mentioned we should get the o/p as
f 4
g 2
h 6
...
...
r 4
But I need only the o/p as last key & its sum..ie 'r ' & its sum as 4.
How can we achieve this , anyway can we get only last key & its count as o/p.?
Created 09-20-2015 06:08 AM
Created 09-20-2015 06:55 AM