Hadoop-Mapreduce入门

Hadoop-Mapreduce入门

  • MapReduce介绍
    • mapreduce设计
    • MapReduce编程规范
    • 入门案例WordCount

MapReduce介绍

MapReduce的思想核心 是“分而治之”,适用于大量复杂的任务处理场景(大规模数据处理场景)。
知识。

  • Map负责“分”,把复杂的任务分解为若干个“简单的任务”来并行处理。可以进行拆分的前提是这些小任务可以并行计算,彼此间几乎没有依赖关系。
  • Reduce负责“合”,即对map阶段的结果进行全局汇总。
  • MapReduce运行在yarn集群 1. ResourceManager 2. NodeManager

这两个阶段合起来正是MapReduce思想的体现。

mapreduce设计

Map和Reduce为程序员提供了一个清晰的操作接口抽象描述。MapReduce中定义了如下的Map 和Reduce两个抽象的编程接口,由用户去编程实现.Map和Reduce,MapReduce处理的数据类型 是键值对。

  • Map: (k1; v1) → [(k2; v2)]
  • Reduce: (k2; [v2]) → [(k3; v3)]

MapReduce编程规范

Map 阶段 2 个步骤

  1. 设置 InputFormat 类, 将数据切分为 Key-Value(K1和V1) 对, 输入到第二步
  2. 自定义 Map 逻辑, 将第一步的结果转换成另外的 Key-Value(K2和V2) 对, 输出结果

Shule 阶段 4 个步骤

  1. 对输出的 Key-Value 对进行分区
  2. 对不同分区的数据按照相同的 Key 排序
  3. 对分组过的数据初步规约, 降低数据的网络拷贝
  4. 对数据进行分组, 相同 Key 的 Value 放入一个集合中

其中分区、排序、规约是在map阶段进行,分组是在reduce阶段进行

Reduce 阶段 2 个步骤

  1. 对多个 Map 任务的结果进行排序以及合并, 编写 Reduce 函数实现自己的逻辑, 对输入的 Key-Value 进行处理, 转为新的 Key-Value(K3和V3)输出
  2. 设置 OutputFormat 处理并保存 Reduce 输出的 Key-Value 数据

入门案例WordCount

hello,world,hadoop
hive,sqoop,flume,hello
kitty,tom,jerry,world
hadoop

上传hdfs

hdfs dfs -mkdir /wordcount/
hdfs dfs -put wordcount.txt /wordcount/

程序的Mapper类

public class WordCountMapper extends Mapper<LongWritable,Text,Text,LongWritable> {
    @Override
    public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String[] split = line.split(",");
            for (String word : split) {
                context.write(new Text(word),new LongWritable(1));
            }
        } 
    }

程序的Reducer类

public class WordCountReducer extends Reducer<Text,LongWritable,Text,LongWritable> {
    /**
    * 自定义我们的reduce逻辑
    * 所有的key都是我们的单词,所有的values都是我们单词出现的次数
    * @param key
    * @param values
    * @param context
    * @throws IOException
    * @throws InterruptedException
    */
        @Override    
        protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
        long count = 0;
        for (LongWritable value : values) {
            count += value.get();
        }
        context.write(key,new LongWritable(count));
    } 
}

程序的主类

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.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class JobMain extends Configured implements Tool {

    //该方法用于指定一个job任务
    @Override
    public int run(String[] strings) throws Exception {
        //1:创建一个job任务对象
        Job job = Job.getInstance(super.getConf(), "wordcount");

        //2:配置job任务对象(八个步骤)

        //第一步:指定文件的读取方式和读取路径
        job.setInputFormatClass(TextInputFormat.class);
        TextInputFormat.addInputPath(job, new Path("hdfs://node01:8020/wordcount"));

        //第二步:指定map阶段的处理方式和数据类型
        job.setMapperClass(WordCountMapper.class);
        //设置map阶段K2的类型
        job.setMapOutputKeyClass(Text.class);
        //设置map阶段V2的类型
        job.setMapOutputValueClass(LongWritable.class);

        //第三,四,五,六采用默认的方式

        //第七步: 指定reduce阶段的处理方式和数据类型
        job.setReducerClass(WordCountReducer.class);
        job.setOutputKeyClass(Text.class);
        //设置V3的类型
        job.setOutputValueClass(LongWritable.class);

        //第八步:
        job.setOutputFormatClass(TextOutputFormat.class);
        //设置输出的路径
        TextOutputFormat.setOutputPath(job, new Path("hdfs://node01:8020/wordcount_out"));

        //等待任务结束
        boolean bl = job.waitForCompletion(true);

        return bl ? 0 : 1;
    }

    public static void main(String[] args) throws Exception {
        Configuration configuration = new Configuration();

        //启动job任务
        int run = ToolRunner.run(configuration, new JobMain(), args);
        System.exit(run);
    }
}

本地运行模式

本地模式非常便于进行业务逻辑的 Debug

TextInputFormat.addInputPath(job,new Path("file:///F:\\wordcount\\input"));
TextOutputFormat.setOutputPath(job,new Path("file:///F:\\wordcount\\output"));

只需将hdfs路径改为本地路径即可

集群运行模式

将程序打成JAR包,然后在集群的任意一个节点上用hadoop命令启动

hadoop jar hadoop_hdfs_operate-1.0-SNAPSHOT.jar cn.itcast.hdfs.demo1.JobMain

你可能感兴趣的:(Hadoop-Mapreduce入门)