20150812-Mapreduce笔记
来源:互联网 发布:编程入门书籍下载 编辑:程序博客网 时间:2024/03/28 19:47
Mapreduce笔记
1、map函数可以作为一个数据准备阶段(去除已损记录,筛选掉错误的数据),reduce在该准备数据上继续处理。
2、Mapreduce数据处理样例:
Map阶段:
对数据进行筛选
根据键对键/值对进行排序和分组
Reduce阶段(找出其中最大的数):
Mapreduce完整的流程:
说明:下面的例子采用了Hadoop的0.20.0版本的API(上下文对象-context object)旨在是API在今后更容易扩展。新的API在类型上不兼容先前的API,所以,需要重写以前的应用程序才能使得新的API发挥作用。
- 新的API倾向于使用虚类,而不是接口,因为更容易扩展。例如,可以无需修改类的实现而在虚类中添加一个方法。在新的API中,mapper和reducer都是虚类。
- 新的API放在org.apache.hadoop.mapreduce包中。之前版本在API依旧放在org.apache.hadoop.mapred中。
- 新的API充分使用了上下文对象,是用户代码能与MapReduce系统光通信。例如,MapContext基本具备了JobConf、OutputCollector和Reporter的功能。
- package jtlyuan.csdn;
- import java.io.IOException;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.conf.Configured;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.LongWritable;
- import org.apache.hadoop.io.IntWritable;
- 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.input.TextInputFormat;
- import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
- import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
- import org.apache.hadoop.util.Tool;
- import org.apache.hadoop.util.ToolRunner;
- //求最大值
- public class MaxNum extends Configured implements Tool {
- public static class MapClass extends Mapper<LongWritable, Text, IntWritable, IntWritable> {
- private int maxNum = 0;
- public void map(LongWritable key, Text value, Context context)
- throws IOException, InterruptedException {
- String[] str = value.toString().split(",", 2);
- try {// 对于非数字字符我们忽略掉
- int temp = Integer.parseInt(str[0]);
- if (temp > maxNum) {
- maxNum = temp;
- }
- } catch (NumberFormatException e) {
- }
- }
- @Override
- protected void cleanup(Context context) throws IOException,
- InterruptedException {
- context.write(new IntWritable(maxNum), new IntWritable(maxNum));
- }
- }
- public static class Reduce extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {
- private int maxNum = 0;
- public void reduce(IntWritable key, Iterable<IntWritable> values, Context context)
- throws IOException, InterruptedException {
- for (IntWritable val : values) {
- if ( val.get() > maxNum) {
- maxNum = val.get();
- }
- }
- }
- @Override
- protected void cleanup(Context context) throws IOException,
- InterruptedException {
- context.write(new IntWritable(maxNum), new IntWritable(maxNum));
- }
- }
- public int run(String[] args) throws Exception {
- Configuration conf = getConf();
- Job job = new Job(conf, "MaxNum");
- job.setJarByClass(MaxNum.class);
- FileInputFormat.setInputPaths(job, new Path(args[0]));
- FileOutputFormat.setOutputPath(job, new Path(args[1]));
- job.setMapperClass(MapClass.class);
- job.setCombinerClass(Reduce.class);
- job.setReducerClass(Reduce.class);
- job.setInputFormatClass(TextInputFormat.class);
- job.setOutputFormatClass(TextOutputFormat.class);
- job.setOutputKeyClass(IntWritable.class);
- job.setOutputValueClass(IntWritable.class);
- System.exit(job.waitForCompletion(true) ? 0 : 1);
- return 0;
- }
- public static void main(String[] args) throws Exception {
- long start = System.nanoTime();
- int res = ToolRunner.run(new Configuration(), new MaxNum(), args);
- System.out.println(System.nanoTime()-start);
- System.exit(res);
- }
- }
- /*
- * 最终输出:6009554 6009554
- *
- */
3、MapReduce的数据流
一个reduce:
多个reduce:
扩展阅读:
http://www.tuicool.com/articles/aqU7Rb
0 0
- 20150812-Mapreduce笔记
- MapReduce笔记
- MapReduce笔记
- MapReduce笔记
- MapReduce笔记
- mapreduce笔记
- Google MapReduce阅读笔记
- MapReduce处理过程笔记
- Google MapReduce 学习笔记
- Hadoop 笔记3 Mapreduce
- mapreduce WordCount 学习笔记
- 论文学习笔记:MapReduce
- mapreduce论文笔记
- nutch MapReduce 笔记
- MapReduce学习笔记
- Hadoop学习笔记---MapReduce
- Hadoop学习笔记---MapReduce
- Hadoop学习笔记---MapReduce
- C++笔试总结-面试笔试常考题型(一)指针-引用-宏定义-sizeof
- Tree
- Random类和Math.radnom()方法的联系
- POJ 2431 Expedition(优先队列)
- HDU 3743 Frosh Week 【归并排序模板题】
- 20150812-Mapreduce笔记
- jquery Deferred 数组
- Service的使用(一)
- 在.gitignore中过滤不必要的文件
- 又见回文
- hdu5373
- 在CodeWarrior中指定堆栈大小及堆栈指针
- SQLite集成与用法
- gflags调试工具的使用