转载请注明出处:http://blog.youkuaiyun.com/l1028386804/article/details/46135857
一、Mapper类的实现
-
-
-
-
-
-
- static class MyMapper extends Mapper<LongWritable, Text, Text, LongWritable>{
- protected void map(LongWritable k1, Text v1, Context context) throws java.io.IOException ,InterruptedException {
- final String[] splited = v1.toString().split("\t");
- for (String word : splited) {
- context.write(new Text(word), new LongWritable(1));
- System.out.println("Mapper输出<"+word+","+1+">");
- }
- };
- }
二、Reducer类的实现
-
-
-
-
-
-
-
- static class MyReducer extends Reducer<Text, LongWritable, Text, LongWritable>{
- protected void reduce(Text k2, java.lang.Iterable<LongWritable> v2s, Context ctx) throws java.io.IOException ,InterruptedException {
-
- System.out.println("MyReducer输入分组<"+k2.toString()+",...>");
- long times = 0L;
- for (LongWritable count : v2s) {
- times += count.get();
-
- System.out.println("MyReducer输入键值对<"+k2.toString()+","+count.get()+">");
- }
- ctx.write(k2, new LongWritable(times));
- };
- }
三、Combiner的类实现
- static class MyCombiner extends Reducer<Text, LongWritable, Text, LongWritable>{
- protected void reduce(Text k2, java.lang.Iterable<LongWritable> v2s, Context ctx) throws java.io.IOException ,InterruptedException {
-
- System.out.println("Combiner输入分组<"+k2.toString()+",...>");
- long times = 0L;
- for (LongWritable count : v2s) {
- times += count.get();
-
- System.out.println("Combiner输入键值对<"+k2.toString()+","+count.get()+">");
- }
-
- ctx.write(k2, new LongWritable(times));
-
- System.out.println("Combiner输出键值对<"+k2.toString()+","+times+">");
- };
- }
四、完整代码
- package combine;
-
- import java.net.URI;
-
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.FileSystem;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.LongWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapreduce.Job;
- import org.apache.hadoop.mapreduce.Mapper;
- import org.apache.hadoop.mapreduce.Reducer;
- import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
- import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
-
-
-
-
-
-
-
-
-
-
-
-
- public class WordCountApp {
- static final String INPUT_PATH = "hdfs://liuyazhuang:9000/hello";
- static final String OUT_PATH = "hdfs://liuyazhuang:9000/out";
-
- public static void main(String[] args) throws Exception {
- Configuration conf = new Configuration();
- final FileSystem fileSystem = FileSystem.get(new URI(INPUT_PATH), conf);
- final Path outPath = new Path(OUT_PATH);
- if(fileSystem.exists(outPath)){
- fileSystem.delete(outPath, true);
- }
-
- final Job job = new Job(conf , WordCountApp.class.getSimpleName());
-
- FileInputFormat.setInputPaths(job, INPUT_PATH);
-
-
-
-
- job.setMapperClass(MyMapper.class);
-
-
-
-
-
-
-
-
-
-
-
-
- job.setCombinerClass(MyCombiner.class);
-
-
- job.setReducerClass(MyReducer.class);
-
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(LongWritable.class);
-
-
- FileOutputFormat.setOutputPath(job, outPath);
-
-
-
-
- job.waitForCompletion(true);
- }
-
-
-
-
-
-
-
- static class MyMapper extends Mapper<LongWritable, Text, Text, LongWritable>{
- protected void map(LongWritable k1, Text v1, Context context) throws java.io.IOException ,InterruptedException {
- final String[] splited = v1.toString().split("\t");
- for (String word : splited) {
- context.write(new Text(word), new LongWritable(1));
- System.out.println("Mapper输出<"+word+","+1+">");
- }
- };
- }
-
-
-
-
-
-
-
-
- static class MyReducer extends Reducer<Text, LongWritable, Text, LongWritable>{
- protected void reduce(Text k2, java.lang.Iterable<LongWritable> v2s, Context ctx) throws java.io.IOException ,InterruptedException {
-
- System.out.println("MyReducer输入分组<"+k2.toString()+",...>");
- long times = 0L;
- for (LongWritable count : v2s) {
- times += count.get();
-
- System.out.println("MyReducer输入键值对<"+k2.toString()+","+count.get()+">");
- }
- ctx.write(k2, new LongWritable(times));
- };
- }
-
-
- static class MyCombiner extends Reducer<Text, LongWritable, Text, LongWritable>{
- protected void reduce(Text k2, java.lang.Iterable<LongWritable> v2s, Context ctx) throws java.io.IOException ,InterruptedException {
-
- System.out.println("Combiner输入分组<"+k2.toString()+",...>");
- long times = 0L;
- for (LongWritable count : v2s) {
- times += count.get();
-
- System.out.println("Combiner输入键值对<"+k2.toString()+","+count.get()+">");
- }
-
- ctx.write(k2, new LongWritable(times));
-
- System.out.println("Combiner输出键值对<"+k2.toString()+","+times+">");
- };
- }
- }
五、运行结果