首页 > 其他 > 详细

编译hadoop版的hello,world

时间:2014-05-23 22:21:08      阅读:453      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
cd ~/src
mkdir classes
javac -classpath ~/hadoop-0.20.2/hadoop-0.20.2-core.jar WordCount.java -d classes
jar -cvf WordCount.jar -C classes/ .
hadoop jar WordCount.jar com.codestyle.hadoop.WordCount input output
hadoop fs -ls output
hadoop fs -cat output/part-00000
bubuko.com,布布扣

要点:

编译WordCount.java时必须通过classpath指定hadoop的库文件。指定源码输出到classes目录

打包class文件成为jar文件

通过hadoop调用jar文件执行MapReduce, 内容输出到output目录 (如果该目录存在,则要先删掉这个目录)在命令参数中必须指定包名+类名


 

WordCount.java

bubuko.com,布布扣
package com.codestyle.hadoop;

import java.io.IOException;
import java.util.*;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;

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.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);
   }
}
bubuko.com,布布扣

查看执行结果

bubuko.com,布布扣
lishujun@lishujun-virtual-machine:~/src$ hadoop fs -cat output/part-00000
Hadoop    1
Hello    2
World    1
bubuko.com,布布扣

 

参考资料:

http://www.cnblogs.com/xia520pi/archive/2012/05/16/2504205.html

http://blog.csdn.net/xw13106209/article/details/6862480

http://blog.csdn.net/turkeyzhou/article/details/8121601

 

编译hadoop版的hello,world,布布扣,bubuko.com

编译hadoop版的hello,world

原文:http://www.cnblogs.com/code-style/p/3737035.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!