Chinaunix首页 | 论坛 | 博客
  • 博客访问: 611299
  • 博文数量: 69
  • 博客积分: 1891
  • 博客等级: 上尉
  • 技术积分: 1359
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-20 23:38
文章分类

全部博文(69)

文章存档

2012年(46)

2011年(23)

分类: LINUX

2012-03-16 00:28:58

     Hadoop的中文文档更新速度有点慢,照着敲的大部分API都是过时的,而且设置也有些问题,按照官网的方法,跑不起来,报路径找不到。


简单敲了一下代码先熟练一下基本的编程模式和API
官网的例子是统计单词,在一个目录下有多个文本文件,要统计其中不同单词出现的次数。

点击(此处)折叠或打开

  1. import java.io.IOException;
  2. import java.util.Iterator;
  3. import java.util.StringTokenizer;

  4. import org.apache.hadoop.fs.Path;
  5. import org.apache.hadoop.io.IntWritable;
  6. import org.apache.hadoop.io.LongWritable;
  7. import org.apache.hadoop.io.Text;
  8. import org.apache.hadoop.mapred.FileInputFormat;
  9. import org.apache.hadoop.mapred.FileOutputFormat;
  10. import org.apache.hadoop.mapred.JobClient;
  11. import org.apache.hadoop.mapred.JobConf;
  12. import org.apache.hadoop.mapred.MapReduceBase;
  13. import org.apache.hadoop.mapred.Mapper;
  14. import org.apache.hadoop.mapred.OutputCollector;
  15. import org.apache.hadoop.mapred.Reducer;
  16. import org.apache.hadoop.mapred.Reporter;
  17. import org.apache.hadoop.mapred.TextInputFormat;
  18. import org.apache.hadoop.mapred.TextOutputFormat;




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

  23.         @Override
  24.         public void map(LongWritable key, Text value,
  25.                 OutputCollector<Text, IntWritable> output, Reporter reporter)
  26.                 throws IOException {
  27.             String line = value.toString();
  28.             StringTokenizer tokenizer = new StringTokenizer(line);
  29.             while(tokenizer.hasMoreTokens()){
  30.                 word.set(tokenizer.nextToken());
  31.                 output.collect(word, one);
  32.             }
  33.             
  34.         }
  35.         
  36.         

  37.         
  38.     }
  39.     
  40.     public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable>{
  41.         
  42.         @Override
  43.         public void reduce(Text key, Iterator<IntWritable> values,
  44.                 OutputCollector<Text, IntWritable> output, Reporter reporter)
  45.                 throws IOException {
  46.             int sum = 0;
  47.             while(values.hasNext()){
  48.                 sum+=values.next().get();
  49.             }
  50.             output.collect(key, new IntWritable(sum));
  51.             
  52.         }
  53.         
  54.     }

  55.     /**
  56.      * @param args
  57.      * @throws IOException
  58.      */
  59.     public static void main(String[] args) throws IOException {
  60.         JobConf conf = new JobConf(WordCount.class);
  61.         conf.setJobName("word count");
  62.         
  63.         conf.setOutputKeyClass(Text.class);
  64.         conf.setOutputValueClass(IntWritable.class);
  65.         
  66.         conf.setMapperClass(Map.class);
  67.         conf.setCombinerClass(Reduce.class);
  68.         conf.setReducerClass(Reduce.class);
  69.         
  70.         conf.setInputFormat(TextInputFormat.class);
  71.         conf.setOutputFormat(TextOutputFormat.class);
  72.         
  73.         FileInputFormat.setInputPaths(conf, new Path(args[0]));
  74.         FileOutputFormat.setOutputPath(conf,new Path(args[1]));
  75.         
  76.         JobClient.runJob(conf);

  77.     }

  78. }

上面是官网的代码,代码基本没问题,这个程序的流程是:
1、准备一个Mapper
     Mapper的作用是,读入一行文本,将文本分隔成单词串,将每个单词的数目设为1,并分发下去
2、准备一个Reducer
    Reducer的工作恰好相反,它是收集同一个单词的所有结果,并进行统计
3、准备一个Combiner
     这里的Combiner和Reducer作用相同
3、配置好一个Job
     设置Job的key,value类型,设置好Mapper,Reducer,Combiner,输入输出格式以及输入输出路径
4、运行Job
      直接运行

好了,下面设置运行参数,代码里又两个args参数要设置,分别是输入输出,输入是需要文本文件的,所以还要准备好输入文件:
1、首先在本地准备两个文本文件file01,file02
file01

点击(此处)折叠或打开

  1. Hello World Goodbye World
file 02

点击(此处)折叠或打开

  1. Hello Hadoop Goodbye Hadoop
2、在hadoop节点上建立输入文件路径

点击(此处)折叠或打开

  1. ./bin/hadoop fs -mkdir /usr/kenvi/wordcount/input

3、将文本文件传到节点服务器上去

点击(此处)折叠或打开

  1. ./bin/hadoop fs -put file01 /usr/kenvi/wordcount/input/file01
  2. ./bin/hadoop fs -put file01 /usr/kenvi/wordcount/input/file01
这样,服务器上的环境就部署好了,还差最后一部,设置本地的参数,官方文档是通过以下命令来运行的:

点击(此处)折叠或打开

  1. $ bin/hadoop jar /usr/joe/wordcount.jar org.myorg.WordCount /usr/joe/wordcount/input /usr/joe/wordcount/output
指定的输入输出路径分别是/usr/joe/wordcount/input /usr/joe/wordcount/output,这样直接跑是运行不起来的,会报路径找不到。应该分别设置成hdfs://host:9000/usr/kenvi/wordcount/input   和hdfs:///host:9000/usr/kenvi/wordcount/output
其中host为hdfs服务器的名字或者ip

这样就可以正常运行了,运行完后,可以在服务器上的输出目录下看到一个文件part-0000,用

点击(此处)折叠或打开

  1. ./bin/hadoop fs -cat /usr/kenvi/wordcount/output/part-00000
可以查看文件内容:

点击(此处)折叠或打开

  1. Goodbye 1
  2. Hadoop 2
  3. Hello 1






阅读(4572) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~