单词计数的mapreduce原理

Hello you

 Hello  me

1.1 读取hdfs中的文件。每一行解析成一个<k,v>。每一个键值对调用一次map函数

解析成2<k,v>,分别是<0,hello you> <10,hello me>

调用2map函数

1.2覆盖map()函数,接受1.1<k,v>进行处理,转换成新<k,v>输出

Public  void  map(k,v,ctx){

 String[] splited = v.split(“\t”)

   for(string word:splited){

     //return <word,1>

      ctx.write(word,1)

     }

 }

 

1.31.2输出<k,v>进行分区.默认一个分区

 

 

1.4对不同分区的的数据进行排序(按照k)、分组。分组是指相同的keyvalue放到一个集合中。

 Map输出后的数据是:<hello,1><you,1><hello,1><me,1>

 排序后为:<hello,1> <hello,1> <me,1> <you,1>

  分组:<hello,{1,1}> <me,{1}> <you,{1}>  ----->分组的数量有3

 

1.5(可选)对分组后的数据进行规约。(是指大文件转换成小文件)

 

 

2.1多个map的任务输出,安装不同的分区,通过网络拷贝到不同的reduce

  

 

2.2对多个map的输出进行合并、排序。覆盖reduce函数,接受的是分组后的数据,实现自己的业务逻辑,处理后形成新的<k,v>输出

  //reduce被调用的次数为3等于分组后的次数

Public void reduce(k,vs,ctx){

     Long count = 0L;

     for(Long times:vs){

       count += times

}

ctx.write(k,count) ;

 }

 

2.3reduce的输出<k,v>写入到hdfs

  hello 2

  me  1

you  1


代码为:

package mapreduce;


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://hadoop:9000/hello";
static final String OUT_PATH = "hdfs://hadoop: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());
//1.1指定读取的文件位于哪里
FileInputFormat.setInputPaths(job, INPUT_PATH);
//指定如何对输入文件进行格式化,把输入文件每一行解析成键值对
//job.setInputFormatClass(TextInputFormat.class);

//1.2 指定自定义的map类
job.setMapperClass(MyMapper.class);
//map输出的<k,v>类型。如果<k3,v3>的类型与<k2,v2>类型一致,则可以省略
//job.setMapOutputKeyClass(Text.class);
//job.setMapOutputValueClass(LongWritable.class);

//1.3 分区
//job.setPartitionerClass(HashPartitioner.class);
//有一个reduce任务运行
//job.setNumReduceTasks(1);

//1.4 TODO 排序、分组

//1.5 TODO 规约

//2.2 指定自定义reduce类
job.setReducerClass(MyReducer.class);
//指定reduce的输出类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);

//2.3 指定写出到哪里
FileOutputFormat.setOutputPath(job, outPath);
//指定输出文件的格式化类
//job.setOutputFormatClass(TextOutputFormat.class);

//把job提交给JobTracker运行
job.waitForCompletion(true);
}

/**
* KEYIN 即k1 表示行的偏移量
* VALUEIN 即v1 表示行文本内容
* KEYOUT 即k2 表示行中出现的单词
* VALUEOUT 即v2 表示行中出现的单词的次数,固定值1
*/
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) {
//System.out.println(word);
context.write(new Text(word), new LongWritable(1));
}
//System.out.println("***************");
System.out.println(v1);
};
}

/**
* KEYIN 即k2 表示行中出现的单词
* VALUEIN 即v2 表示行中出现的单词的次数
* KEYOUT 即k3 表示文本中出现的不同单词
* VALUEOUT 即v3 表示文本中出现的不同单词的总次数
*
*/
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 {
long times = 0L;
for (LongWritable count : v2s) {
times += count.get();
}
ctx.write(k2, new LongWritable(times));
System.out.println(k2+"  ---->"+times);
};
}

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值