您的位置:首页 > 运维架构

Hadoop入门程序WordCount的执行过程

2016-01-22 15:29 393 查看
  首先编写WordCount.java源文件,分别通过map和reduce方法统计文本中每个单词出现的次数,然后按照字母的顺序排列输出,

  Map过程首先是多个map并行提取多个句子里面的单词然后分别列出来每个单词,出现次数为1,全部列举出来

  


  Reduce过程首先将相同key的数据进行查找分组然后合并,比如对于key为Hello的数据分组为:<Hello, 1>、<Hello,1>、<Hello,1>,合并之后就是<Hello,1+1+1>,分组也可以理解为reduce的操作,合并减少数据时reduce的主要任务,叠加运算之后就是<Hello, 3>所以最后可以输出Hello 3,这样就完成了一轮MapReduce处理

  


  WordCount.java代码如下:

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

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;

public class WordCount {

public static class Map extends MapReduceBase implements
Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();

public void map(LongWritable key, Text value,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
output.collect(word, one);
}
}
}

public static class Reduce extends MapReduceBase implements
Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
int sum = 0;
while (values.hasNext()) {
sum += values.next().get();
}
output.collect(key, new IntWritable(sum));
}
}

public static void main(String[] args) throws Exception {
JobConf conf = new JobConf(WordCount.class);
conf.setJobName("wordcount");

conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);

conf.setMapperClass(Map.class);
conf.setCombinerClass(Reduce.class);
conf.setReducerClass(Reduce.class);

conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);

FileInputFormat.setInputPaths(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));

JobClient.runJob(conf);
}
}


  将此java文件上传到服务器后,首先要进行编译,如果是eclipse会自动完成,如果没有配置开发环境需要手动对源文件进行编译,命令如下:

javac -classpath /hadoop/hadoop-1.2.1/hadoop-core-1.2.1.jar:/hadoop/hadoop-1.2.1/lib/commons-cli-1.2.jar WordCount.java


  编译的时候需要制定上面两个jar包,编译完成之后除了生成WordCount.class字节码之外还有WordCount$Map.class和WordCount$Reduce.class,我们知道这两个文件是内部类Map和Reduce生成的

  然后开始对class文件打包生成wordcount.jar

jar -cvf wordcount.jar *.class


  


  现在就打包生成了wordcount.jar文件

  接下来可以通过传给main方法参数执行参数是两个字符串,分别为args[0]和args[1],可以把它放到文件中进行输入,那么可以在hdfs文件系统中建立两个文件file01和file02并写入内容,依次执行命令:

$ echo "Hello World Hello Java" > file01
$ echo "Hello World Hello Hadoop" > file02
$ hadoop fs -mkdir input
$ hadoop fs -put file0* input/


  现在hdfs文件系统中/user/用户名/input下就有两个文件file01和file02,同样我们可以用命令查看文件的存在性和内容

  接下来就可以提交任务用hadoop来运行jar包中的函数进行数据处理了

hadoop jar wordcount.jar WordCount input output


  WordCount代码jar包里的主类,input是传入的文件作为参数,output参数就是hadoop作业完毕之后结果存放目录,开始执行会看到map和reduce的处理进度

  


  处理完毕后,通过hadoop fs -ls output/ 查看生成的结果文件是否存在

  


  通过结果可以看到任务执行正常并输出了结果文件,可以用hadoop fs -get output localdata将文件传到本地查看,也可以执行下面命令查看文件的内容

hadoop fs -cat output/part-00000


  


  可以看到结果按顺序统计出来了,到这里一个简单的WordCount程序就手动开发成功了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: