Mapreduce编程模型

mapreduce编程模型的分布式编程发的步骤
1)迭代 , 遍历输入数据 , 将数据解析成key/value对
2)将输入的key/value对,映射(map)成新的key/value对
3)根据key对中间数据进行分组(grouping)
4)以组为单位对数据进行规约(reduce)
5)迭代,将最终产生的key/value对输出到文件中
组件化和并行化InputFormat Mapper Partitioner Redece OutputForMat

.WordCount案例
.map
public class WordCountMapper 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[] split = value.toString().split("\s");
for (String word : split) {
context.write(new Text(word), new IntWritable(1));
}
}
}
reduce
public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
@Override
protected void reduce(Text key, Iterable values,
Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
int count = 0;
Iterator iterator = values.iterator();
while (iterator.hasNext()) {
IntWritable value = iterator.next();
count += value.get();
}
context.write(key, new IntWritable(count));
}
}

mapreduce运算模型默认在本地运行, 方便测试可以将mapreduce编写的业务代码在windows中测试

本地模式(默认模式)
默认运行在windows中 , 不需要任何的参数设置
public class WordCountJobSubmit {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
/conf.set(“mapreduce.framework.name”,“local”);
conf.set(“fs,defaultFS”, “file:///”);
/
Job job = Job.getInstance(conf);
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReducer.class);

	job.setMapOutputKeyClass(Text.class);
	job.setMapOutputValueClass(IntWritable.class);
	
	job.setOutputKeyClass(Text.class);
	job.setOutputValueClass(IntWritable.class);
	
	FileInputFormat.setInputPaths(job, new Path("d:/data/wc/input"));
	FileOutputFormat.setOutputPath(job, new Path("d:/data/wc/output"));
	
	job.setNumReduceTasks(2);
	boolean b = job.waitForCompletion(true);
	System.exit(b?0:-1);
}

}

yarn模式
将MR程序运行在yarn中,保证集群运行正常 , yarn工作正常
public class JobSubmit {
public static void main(String[] args) throws Exception {
//设置操作的用户信息
System.setProperty(“HADOOP_USER_NAME” , “root”);
Configuration conf = new Configuration();
// 设置访问的集群的位置
conf.set(“fs.defaultFS”, “hdfs://doit01:9000”);
//设置yarn的位置
conf.set(“mapreduce.framework.name”, “yarn”);
//yarn的resourcemanager的位置
conf.set(“yarn.resourcemanager.hostname”, “doit01”);
//设置MapReduce程序运行在windows上的跨平台参数
conf.set(“mapreduce.app-submission.cross-platform”,“true”);
Job job = Job.getInstance(conf);
job.setJarByClass(JobSubmit.class);
//job.setJar(“C:\Users\ThinkPad\Desktop\wc.jar”);
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReducer.class);

	job.setMapOutputKeyClass(Text.class);
	job.setMapOutputValueClass(IntWritable.class);
	
	job.setOutputKeyClass(Text.class);
	job.setOutputValueClass(IntWritable.class);
	
	FileInputFormat.setInputPaths(job, new Path("/data/wc/input"));
	FileOutputFormat.setOutputPath(job, new Path("/data/wc/output"));
	
	job.setNumReduceTasks(2);
	boolean b = job.waitForCompletion(true);
	System.exit(b?0:-1);
}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值