mapreduce中入门中要注意的几点

在 mapreduce中,比如有如下的词:

I love beijing
i love cina
beijing is the captial of china
统计的时候,如下图:

[img]https://thumbnail0.baidupcs.com/thumbnail/8e7f74dd710c5da81772bd8a04433907?fid=1443225280-250528-855548211348301&time=1525564800&rt=sh&sign=FDTAER-DCb740ccc5511e5e8fedcff06b081203-W5866LBedLK7cAI95DCpMdrZaGY%3D&expires=8h&chkv=0&chkbd=0&chkpc=&dp-logid=2907790120311793992&dp-callid=0&size=c710_u400&quality=100&vuk=-&ft=video[/img]


注意上图中,最左边的偏移量第一个为1,然后I LOVE CHINA中,I是第4个单词了;所以偏移量为4;
然后进行分词,
然后K3就是把每个单词归类的KEY,然后V3(1,1),就是说I这个单词,统计的频率;


相关的mapper的编写:

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

@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
/*
* key: 输入的key
* value: 数据 I love Beijing
* context: Map上下文
*/
String data= value.toString();
//分词
String[] words = data.split(" ");

//输出每个单词
for(String w:words){
context.write(new Text(w), new LongWritable(1));
}
}

}

reduce:



public class WordCountReducer extends Reducer<Text, LongWritable, Text, LongWritable>{

@Override
protected void reduce(Text k3, Iterable<LongWritable> v3,Context context) throws IOException, InterruptedException {
//v3: 是一个集合,每个元素就是v2
long total = 0;
for(LongWritable l:v3){
total = total + l.get();
}

//输出
context.write(k3, new LongWritable(total));
}

}


主程序:

public class WordCountMain {

public static void main(String[] args) throws Exception {
//创建一个job = map + reduce
Configuration conf = new Configuration();

//创建一个Job
Job job = Job.getInstance(conf);
//指定任务的入口
job.setJarByClass(WordCountMain.class);

//指定job的mapper
job.setMapperClass(WordCountMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class);

//指定job的reducer
job.setReducerClass(WordCountReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);

//指定任务的输入和输出
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));

//提交任务
job.waitForCompletion(true);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值