MapReduce

所需的组件

HDFS 分布式存储系统

MapReduce 分布式计算系统

YARN hadoop 的资源调度系统

Common 以上三大组件的底层支撑组件,提供基础工具包和 RPC 框架等

mapreduce功能

Mapper负责“分”
分解计算任务,规模大大缩小;
“计算向数据靠近” ;
这些小任务可以并行计算。

Reducer负责“汇总” map阶段的结果
~~

不同函数的功能

~~
一、Combiner函数
本地化的reducer
二、Partitioner函数
决定着Map节点的输出将被分区到哪个Reduce节点
三、什么是shuffle
怎样把map task的输出结果有效地传送到reduce端?
map输出之前,在内存里经过sort和combiner,再将所有的输出集合到partitioner进行划分到不同的reducer,在每个分区(partition)中,再进行内存中排序,再运行combiner,最后输出到HDFS。

Map处理
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>
Reduce处理
public class WordCountReducer extends Reducer <Text, IntWritable, Text, IntWritable>
Job配置
public class WordCountDriver
//关联使用的Mapper类
job.setMapperClass(WordCountMapper.class);
//关联使用的Reducer类
job.setReducerClass(WordCountReducer.class);

WordCountMapper类

public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
//对数据进行打散
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//输入数据 hello world love work
String line = value.toString();
//对数据切分
String[] words=line.split(" ");
//写出<hello, 1>
for(String w:words) {
//写出reducer端
context.write(new Text(w), new IntWritable(1));
}}}

WordCountReducer类

public class WordCountReducer extends Reducer <Text, IntWritable, Text, IntWritable>{
protected void reduce(Text Key, Iterable values, Context context) throws IOException, InterruptedException {
//记录出现的次数
int sum=0;
//累加求和输出
for(IntWritable v:values) {
sum +=v.get();
}
context.write(Key, new IntWritable(sum));
}
}

WordCountDriver类

public class WordCountDriver {
public static void main(String[] args) throws IllegalArgumentException, IOException, ClassNotFoundException, InterruptedException {
// 设置root权限
System.setProperty(“HADOOP_USER_NAME”, “root”);
//创建job任务
Configuration conf=new Configuration();
Job job=Job.getInstance(conf);
//指定jar包位置
job.setJarByClass(WordCountDriver.class);
//关联使用的Mapper类
job.setMapperClass(WordCountMapper.class);
//关联使用的Reducer类
job.setReducerClass(WordCountReducer.class);
//设置Mapper阶段输出的数据类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
//设置Reducer阶段输出的数据类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
//设置数据输入路径和文件名
FileInputFormat.setInputPaths(job, new Path(“c:\in\aa.txt”));
//设置数据输出路径
FileOutputFormat.setOutputPath(job, new Path(“c:\out”));
//提交任务
Boolean rs=job.waitForCompletion(true);
//退出
System.exit(rs?0:1);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值