MapReduce的学习笔记

MapReduce的学习笔记

MapReduce的官网文档地址:https://hadoop.apache.org/docs/current/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html

1. 概述

Hadoop MapReduce 是一个软件框架,用于轻松编写应用程序,以可靠、容错的方式在大型商用硬件集群(数千个节点)上并行处理大量数据(多TB数据集)。

一个MapReduce job 通常将输入的数据集拆分成独立的快。Map任务以完全并行的方式处理这些块。框架对map的输出进行排序,进而作为输入提供给reduce任务。通常来说,job的输入和输出都保存在一个文件系统中。框架负责调度任务,监控任务并重新执行失败了的任务。

通常来说,计算节点和存储节点是相同的,也就是说,MapReduce框架和HDFS运行在相同的节点集上。这样的配置能够保证框架在已经存在数据的节点上有效的调度任务,进而在不同集群间获得一个非常高的总带宽。

MapReduce框架由一个单一的主Resourcemanager,每个集群节点上的一个从Nodemanager以及每个应用上一个MRAppMaster组成。

应用至少会指定输入/输出位置以及通过实现合适的接口和抽象类来提供map和reduce功能。这些,以及其他job参数,组成job配置(configuration)。

然后,Hadoop job客户端提交job(jar/可执行的文件等等)以及配置ResourceManger。ResoureManger负责给从节点分发软件/配置,调度和监督任务,反馈状态和诊断信息给job客户端。

2. 输入和输出

MapReduce框架完全以<键,值>形式操作,也就是说,框架将输入给job的数据视为<键,值>对,并且产生一个<键,值>对集作为job的输出。

键和值类必须通过框架序列化,因此需要实现Writable接口。除此之外,key类必须实现WritableComparable接口以辅助框架的排序。

一个MapReducejob的输入输出类型如下所示:

(输入)<k1,v1> -> map -> <k2,v2> -> combine -> <k2,v2> -> reduce -> <k3,v3>(输出)

3. 示例:WordCount v1.0

通过一个MapReduce应用程序示例来了解它们的工作原理。

WordCount 是一个简单的应用程序,它计算给定输入集中每个单词的出现次数。

这适用于本地独立、伪分布式或完全分布式 Hadoop 安装。

源代码:

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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.output.FileOutputFormat;

public class WordCount {

  public static class TokenizerMapper
       extends Mapper<Object, Text, Text, IntWritable>{

    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();

    public void map(Object key, Text value, Context context
                    ) throws IOException, InterruptedException {
      StringTokenizer itr = new StringTokenizer(value.toString());
      while (itr.hasMoreTokens()) {
        word.set(itr.nextToken());
        context.write(word, one);
      }
    }
  }

  public static class IntSumReducer
       extends Reducer<Text,IntWritable,Text,IntWritable> {
    private IntWritable result = new IntWritable();

    public void reduce(Text key, Iterable<IntWritable> values,
                       Context context
                       ) throws IOException, InterruptedException {
      int sum = 0;
      for (IntWritable val : values) {
        sum += val.get();
      }
      result.set(sum);
      context.write(key, result);
    }
  }

  public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    Job job = Job.getInstance(conf, "word count");
    job.setJarByClass(WordCount.class);
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}
用法:

假设环境变量的设置如下:

export JAVA_HOME=/usr/java/default
export PATH=JAVAHOME/bin:{JAVA_HOME}/bin:JAVAHOME/bin:{PATH}
export HADOOP_CLASSPATH=${JAVA_HOME}/lib/tools.jar

编译WordCount.java并创建一个Jar:

$ bin/hadoop com.sun.tools.javac.Main WordCount.java
$ jar cf wc.jar WordCount*.class

假如:

  • /user/joe/wordcount/input - HDFS的输入目录
  • /user/joe/wordcount/output - HDFS的输出目录

示例文本文件作为输入:

$ bin/hadoop fs -ls /user/joe/wordcount/input/
/user/joe/wordcount/input/file01
/user/joe/wordcount/input/file02

$ bin/hadoop fs -cat /user/joe/wordcount/input/file01
Hello World Bye World

$ bin/hadoop fs -cat /user/joe/wordcount/input/file02
Hello Hadoop Goodbye Hadoop

运行应用程序:

$ bin/hadoop jar wc.jar WordCount /user/joe/wordcount/input /user/joe/wordcount/output

输出:

$ bin/hadoop fs -cat /user/joe/wordcount/output/part-r-00000
Bye 1
Goodbye 1
Hadoop 2
Hello 2
World 2

应用程序可以通过使用选项 -files来在该任务当前工作目录下指定多个以逗号分开的路径列表。-libjars选项允许应用程序将jars添加到map和reduce的类路径中。选项-archives允许传递以逗号分开的备份作为参数。

运行带有-libjars, -files和-archives的wordcount例子:

bin/hadoop jar hadoop-mapreduce-examples-.jar wordcount -files cachefile.txt -libjars mylib.jar -archives myarchive.zip input output

其中,myarchive.zip 会解压放在“myarchive.zip”所在的路径下。

用户可以使用 # 给通过 -files 和 -archives 传递的文件和备份指定一个不同的名称。

如:

bin/hadoop jar hadoop-mapreduce-examples-.jar wordcount -files dir1/dict.txt#dict1,dir2/dict.txt#dict2 -archives mytar.tgz#tgzdir input output

其中,文件 dir1/dict.txt 和 dir2/dict.txt 可以分别被使用了#的dict1 和 dict2 的任务访问。备份文件mytar.tgz将被放置并取消归档到名为“tgzdir”的目录中。

应用程序可以通过使用选项 -Dmapreduce.map.dev、-DmapReduce.reduce.env 和 -Dyarn.app.mapreduce.am.env在命令行上分别指定映射器、reducer和应用程序主任务的环境变量。

例如如下为映射器和reducer设置环境变量 FOO_VAR=bar 和 LIST_VAR=a,b,c。

bin/hadoop jar hadoop-mapreduce-examples-<ver>.jar wordcount -Dmapreduce.map.env.FOO_VAR=bar -Dmapreduce.map.env.LIST_VAR=a,b,c -Dmapreduce.reduce.env.FOO_VAR=bar -Dmapreduce.reduce.env.LIST_VAR=a,b,c input output
解析:

WordCount应用是非常简单的。

public void map(Object key, Text value, Context context
                ) throws IOException, InterruptedException {
  StringTokenizer itr = new StringTokenizer(value.toString());
  while (itr.hasMoreTokens()) {
    word.set(itr.nextToken());
    context.write(word, one);
  }
}

Mapper通过map方法实现,在指定的textInputFormat的辅助下,一次处理一行。然后,通过StringTokenizer将行按空格拆分成字符,且输出类似<,1>的键值对。

对于给定的样例,第一个map的输出:

< Hello, 1>
< World, 1>
< Bye, 1>
< World, 1>

第二个map输出:

< Hello, 1>
< Hadoop, 1>
< Goodbye, 1>
< Hadoop, 1>

我们已经了解到了为一个给定job而衍生的很多map,在教程的后面部分将会介绍如何以一种细粒度的方式控制它们。

job.setCombinerClass(IntSumReducer.class);

WordCount 同样指定了一个合并器。因此,在基于key值排序后,每一个map的输出都会传递给本地的合并器以实现本地聚合。

第一个map的输出:

< Bye, 1>
< Hello, 1>
< World, 2>

第二个map输出:

< Goodbye, 1>
< Hadoop, 2>
< Hello, 1>
public void reduce(Text key, Iterable<IntWritable> values,
                   Context context
                   ) throws IOException, InterruptedException {
  int sum = 0;
  for (IntWritable val : values) {
    sum += val.get();
  }
  result.set(sum);
  context.write(key, result);
}

Reducer通过reduce方法实现,目的是对值进行求和,也就是计算每个键出现的次数(比如,在这个例子中就是单词)

因此,job的输出为:

< Bye, 1>
< Goodbye, 1>
< Hadoop, 2>
< Hello, 2>
< World, 2>

main 方法指定job的不同方面,比如输入/输出路径(通过命令行传递),键/值类型,输入/输出格式等等。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白居不易.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值