基本概念

Hadoop:的框架最核心的设计就是:HDFS和MapReduce。HDFS为海量的数据提供了存储,则MapReduce为海量的数据提供了计算。
MapReduce:是处理大量半结构化数据集合的编程模型。最简单的 MapReduce应用程序至少包含 3 个部分:一个 Map 函数、一个 Reduce 函数和一个 main 函数。我的简单理解是map按照一定规则对输入做一系列的处理,reduce进行输出,输出前可能做一些操作,比如统计之类的。

一个有趣的例子

hadoop mapreduce 为空 输出到本地文件夹 hadoop中mapreduce实例_hadoop


你想数出一摞牌中有多少张黑桃。直观方式是一张一张检查并且数出有多少张是黑桃?

MapReduce方法则是:

1. 给在座的所有玩家中分配这摞牌

2. 让每个玩家数自己手中的牌有几张是黑桃,然后把这个数目汇报给你

3. 你把所有玩家告诉你的数字加起来,得到最后的结论

实例需求

计算输入文件中的单词出现的次数。输入文件中的内容单词之间用空格隔开,要求输出文件中输出单词、空格、单词出现的次数。

输入文件

I am a pretty girl
I am a programmer I am so proud

输出文件

I   3
a   2
am  3
girl    1
pretty  1
programmer  1
proud   1
so  1

设计思路

根据map的规则,我们读取输入文件里的每一行,拿到每一个单词,输出[单词 1],reduce拿到[key,value-list],循环list统计每一个key出现的次数,输出[单词 次数]

代码实现

代码已上传至我的git:https://github.com/chenxiaoqiong/worldCountMapReduce
主要代码:

/**
* <h1> MapReduce实例(一) </h1>
* WordCountMapReduce:计算每个单词出现的次数
* Created by chenxiaoqiong on 2017/3/01 0017 下午 2:14.
*/
public class WordCountMapReduce extends Configured implements Tool {

    /**
     * map:处理输入文件,输出(单词 1)
     */
    public static class WordCountMapper
            extends Mapper<LongWritable, Text, Text, IntWritable> {

        private final static IntWritable ints = new IntWritable(1);
        private Text keyword = new Text();

        @Override
        protected void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
            String[] values = value.toString().split(" ");
            for (String vs : values) {
                keyword.set(vs);
                context.write(keyword, ints);
            }
        }
    }

    /**
     * reduce:统计map输出,输出(单词,count)
     */
    public static class WordCountReducer
            extends Reducer<Text, IntWritable, Text, IntWritable> {

        private IntWritable inw = new IntWritable();

        @Override
        protected void reduce(Text key, Iterable<IntWritable> value, Context context)
                throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable val : value) {
                sum += val.get();
            }
            inw.set(sum);
            context.write(key, inw);
        }
    }

    public int run(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        //获取配置文件:
        Configuration conf = super.getConf();

        //创建job:
        Job job = Job.getInstance(conf, this.getClass().getSimpleName());
        job.setJarByClass(WordCountMapReduce.class);

        //配置作业:
        // Input --> Map --> Reduce --> Output
        // Input:
        Path inPath = new Path(args[0]);
        FileInputFormat.addInputPath(job, inPath);
        //FileInputFormat过程会将文件处理(Format)成 <偏移量,每一行内容> 的key value对。

        //Map  设置Mapper类,设置Mapper类输出的Key、Value的类型:
        job.setMapperClass(WordCountMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        //Reduce  设置Reducer类, 设置最终输出的 Key、Value的类型(setOutputKeyClass、setOutputValueClass):
        job.setReducerClass(WordCountReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        //Output 设置输出路径
        Path outPath = new Path(args[1]);
        FileOutputFormat.setOutputPath(job, outPath);

        //提交任务
        boolean isSucess = job.waitForCompletion(true);
        return isSucess ? 1 : 0;     //成功返回1 ,失败返回0
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        int status = ToolRunner.run(conf, new WordCountMapReduce(), args);
        System.exit(status);
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>hadoop</groupId>
    <artifactId>countMapReduce</artifactId>
    <version>1.0-SNAPSHOT</version>

    <repositories>
        <repository>
            <id>apache</id>
            <url>http://maven.apache.org</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-core</artifactId>
            <version>1.2.1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <configuration>
                    <excludeTransitive>false</excludeTransitive>
                    <stripVersion>true</stripVersion>
                    <outputDirectory>./lib</outputDirectory>
                </configuration>

            </plugin>
        </plugins>
    </build>
</project>