WordCount案例---MapReduce学习小结(-)

距离第一次接触MapReduce已经过去了很久很久.回忆起来最开始的时候,看到一个程序那么多代码.都不知道如何入手…走了很多的弯路.到后来,慢慢的一步步学习中,才发现它并不是一个什么不可跨越的坑.
多练习,多总结.就能发现其中的秘密.

经典的WordCount

思路:
1. Map阶段:读取文件,这里读的时候,是一行一行的读取.然后,便可以分割开.输出的结果是这样的格式:

package day1;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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 wordCountDemo {

    public static void main(String[] args) throws Exception {
        if (args.length!=2) {
            System.err.println("input err!");
            System.exit(-1);
        }
        Job job=new Job(new Configuration(), "WordCount");
        job.setJarByClass(wordCountDemo.class);
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        job.setMapperClass(wMap.class);
        job.setReducerClass(wReduce.class); 
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        job.waitForCompletion(true);
    }
    //map阶段
    public static class wMap extends Mapper<LongWritable, Text, Text,IntWritable>{
        @Override
        protected void map(LongWritable key, Text value,
                Mapper<LongWritable, Text, Text, IntWritable>.Context context)
                throws IOException, InterruptedException {
            String[] lines = value.toString().split(" ");
            for (String words : lines) {
                context.write(new Text(words), new IntWritable(1));
            }
        }
    }
    //map阶段:<key,value>
    //shuffle:<key,{value,value....}
    //reduce阶段
    public static class wReduce extends Reducer<Text, IntWritable, Text, IntWritable>{
        @Override
        protected void reduce(Text k1, Iterable<IntWritable> v1,
                Reducer<Text, IntWritable, Text, IntWritable>.Context context)
                throws IOException, InterruptedException {
            int sum=0;
            for (IntWritable count : v1) {
                sum+=count.get();
            }
            context.write(k1, new IntWritable(sum));
        }
    }

}

wordCount 其实不难.虽然是入门程序.但也相当的有用啊.很多都和这个的做法一样的..额,复习的时候自己写了一段.

package day1;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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;


/**
 * 统计学生总分.
 * 数据格式:
 * 姓名:科目:成绩
 * 张三:语文:12
 * 李四:数学:23
 * 张三:英语:1000
 * ....
 * @author YXY
 *
 */
public class SumDemo {
    public static void main(String[] args) throws Exception {
        if (args.length!=2) {
            System.err.println("input err!");
            System.exit(-1);
        }
        Job job=new Job(new Configuration(), "Sumdemo");
        job.setJarByClass(wordCountDemo.class);
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        job.setMapperClass(SumMap.class);
        job.setReducerClass(SumReduce.class);

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

        job.waitForCompletion(true);
    }
    public static class SumMap extends Mapper<LongWritable, Text, Text, IntWritable>{
        @Override
        protected void map(LongWritable key, Text value,
                Mapper<LongWritable, Text, Text, IntWritable>.Context context)
                throws IOException, InterruptedException {
            String[] lines = value.toString().split(":");
            String name = lines[0].trim();
            int score=Integer.parseInt(lines[2].trim());
            context.write(new Text(name), new IntWritable(score));
        }
    }
    public static class SumReduce extends Reducer<Text, IntWritable, Text,IntWritable>{
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values,
                Reducer<Text, IntWritable, Text, IntWritable>.Context context)
                throws IOException, InterruptedException {
            int sum=0;
            for (IntWritable score : values) {
                sum+=score.get();
            }
            context.write(key, new IntWritable(sum));
        }
    }
}


注意点
1. 使用智能提示的时候,注意要导入正确的包…因为很多时候,你会看到一样的名字.
2. 理解这种思路.复习MapReduce的工作原理.
3. alt+ctrl+L..快捷键.
4. 不要懒惰…刚开始不连续,想通过复制来解决.其实是对自己最大的不负责.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白日与明月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值