Hadoop的“Hello world”---WordCount

在安装并配置好Hadoop环境以后,须要运行一个实例来验证配置是否正确,Hadoop就提供了一个简单的wordcount程序,其实就是统计单词个数的程序,这个程序能够算是Hadoop中的“Hello World”了。java

MapReduce

原理

MapReduce其实就是采用分而治之的思想,将大规模的数据分红各个节点共同完成,而后再整合各个节点的结果,获得最终的结果。这些分节点处理数据均可以作到并行处理,大大缩减了工做的复杂度。apache

过程

MapReduce能够分红两个阶段,其实就是单词拆成map和reduce,这实际上是两个函数。map函数会产生一个中间输出,而后reduce函数接受多个map函数产生的一系列中间输出而后再产生一个最终输出。app

WordCount展现

前期工做

启动hadoop

cd /usr/hadoop/hadoop-2.6.2/
sbin/start-dfs.sh
sbin/start-yarn.sh

建立本地数据文件

cd ~/
mkdir ~/file
cd file
echo "Hello World" > test1.txt
echo "Hello Hadoop" > test2.txt

这样就建立了两个txt文件,里面分别有一个字符串:Hello World,Hello Hadoop。咱们经过wordcount想要获得的结果是这样的:Hello 2,World 1,Hadoop 1。函数

在HDFS上建立输入文件夹

hadoop fs -mkdir /input

建立好咱们能够经过oop

hadoop fs -ls /

来查看结果:源码分析

将数据文件传到input目录下

hadoop fs -put ~/file/test*.txt /input

一样,咱们能够经过spa

hadoop fs -ls /input

来查看是否上传成功:3d

若是看不到任何结果,说明在hadoop的配置中存在问题,或者是防火墙没有关闭,致使节点链接不通。code

运行程序

运行wordcount

hadoop jar /你的hadoop根目录/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.6.2.jar wordcount /input /output

运行这条命令后,Hadoop会启动一个JVM来运行MapReduce程序,并且会在集群上建立一个output文件夹,将结果存在其中。orm

咱们来看看结果:

注意点:

  • 这个目录必定要填对,要否则会报jar不存在。

  • 输出文件夹必定要是空文件夹。

查看结果

output文件夹中如今有两个文件,咱们须要的结果在part-r-00000这个文件夹中。

hadoop fs -cat /output/part-r-00000

咱们就能够看到最终的wordcount结果了:

WordCount源码分析

Map过程

源码:

import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
    IntWritable one = new IntWritable(1);
    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);
        }
    }
}

继承Mapper类,重写map方法。

咱们了解到mapreduce中数据都是经过<key,value>传递的。咱们能够经过控制台来看看其中的value值和key值是什么样的。在map方法中加入如下代码:

System.out.println("key= "+key.toString());//查看key值
System.out.println("value= "+value.toString());//查看value值

运行程序后控制台输出以下:

咱们能够看出,map方法中的value值存储的是文本文件中的一行,而key值为该行的首字符相对于文本文件的首地址的偏移量。

程序中的StringTokenizer这个类的功能是将每一行拆分红一个一个的单词,并将<word,1>做为map方法的结果输出。

Reduce过程

源码:

import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
    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);
    }
}

一样,Reduce过程也须要继承一个Reducer类,并重写reduce方法。

咱们能够看到reduce的输入参数是Text keyIterable<Intrable>。咱们知道reduce方法的输入参数key是一个单词,而values是由各个Mapper上对应单词的计数值所组成的列表,咱们能够看到values实现了一个Iterable接口,能够理解成values里面包含了多个IntWritable整数,其实也就是计数值。

而后咱们只要遍历values而且求和,就能够获得各单词的总次数了。

执行MapReduce

咱们已经写好了map函数和reduce函数,如今就是要执行mapreduce了。

源码:

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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class WordCount {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
        if(otherArgs.length != 2) {
            System.err.println("Usage: wordcount <in> <out>");
            System.exit(2);
        }
        Job job = new Job(conf, "wordcount");
        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(otherArgs[0]));
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
        System.exit(job.waitForCompletion(true)?0:1);
    } 
}

代码中的job.set*()方法是为对任务的参数进行相关的设置,而后调用job.waitForCompletion()方法执行任务。

原文连接:http://axuebin.com/blog/2016/02/14/hadoop-wordcount/

相关文章
相关标签/搜索