[Hadoop] Wordcount Example

根據 Hadoop 版本的不同,指令也會有所不同,建議在執行範例時,先確認所使用的指令是否符合規範。這裡我安裝的是 2.6.0 版 Hadoop,執行 wordcount v1.0 example。詳情可看 MapReduce Tutorial


範例程式

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

file1

Hello World Bye World

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
Bye 1
Goodbye 1
Hadoop 2
Hello 2
World 2

參考資料

Hadoop Tutorial

Jerry
Jerry

樂於分享的軟體工程師,曾在新創與大型科技公司實習,獲得黑客松競賽冠軍,擔任資安研討會講者。長期熱衷於資訊安全、雲端服務、網路行銷等領域,希望將科技知識分享給更多人。內容轉載請來信:jlee58tw@gmail.com

發表回應