根據hadoop版本的不同,指令也會有所不同,建議在執行範例時,先確認所使用的指令是否符合規範。

這裏我安裝的是2.6.0版hadoop,執行官網提供的wordcount v1.0 example。

詳情可看MapReduce Tutorial


準備工作

確認Hadoop版本為2.6.0,其他版本的指令用法可能有所不同

確認Hadoop正確運作(用jps可以看到datanode,namenode等等)


範例程式

這是官網附的Source Code

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

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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class WordCount {

  public static class TokenizerMapper
       extends Mapper<Object, Text, Text, IntWritable>{

    private final static IntWritable one = new IntWritable(1);
    private 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);
      }
    }
  }

  public static class IntSumReducer
       extends Reducer<Text,IntWritable,Text,IntWritable> {
    private 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);
    }
  }

  public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    Job job = Job.getInstance(conf, "word count");
    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(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}

我們要編譯WordCount.java,然後打包成jar檔

$ hadoop com.sun.tools.javac.Main WordCount.java 
$ jar cf wc.jar WordCount*.class

在hdfs上建立上傳目錄

這裏我建立了/user/hadoopuser/input,其實也不用那麼多層,當初只是看人家教學就先照打了XD

但要注意的是hdfs目錄和本機目錄是不同的,當檔案上傳至hdfs,本機相對應的位置是不會有東西的唷

$ hdfs dfs -mkdir /user
$ hdfs dfs -mkdir /user/hadoopuser
$ hdfs dfs -mkdir /user/hadoopuser/input

編輯並上傳測試檔案至hdfs

$ vim file1
Hello World Bye World
$ vim file2
Hello Hadoop Goodbye Hadoop
$ hdfs dfs -put file1 /user/hadoopuser/input
$ hdfs dfs -put file2 /user/hadoopuser/input

執行程式

$ hadoop jar wc.jar WordCount /user/hadoopuser/input /user/hadoopuser/output

輸出結果

輸出的檔案會被放在output資料夾下,並依照part-r-*的檔名存放,我們可以用cat看到輸出結果

$ hdfs dfs -cat /user/hadoopuser/output/part-r-00000

hadoophello

參考資料

Hadoop