码迷,mamicode.com
首页 > 其他好文 > 详细

编译hadoop版的hello,world

时间:2014-05-23 22:22:04      阅读:341      评论:0      收藏:0      [点我收藏+]

标签:des   style   class   blog   c   code   

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

标签:des   style   class   blog   c   code   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!