第7章. Hadoop MapReduce介紹


7.1 wordCount.java介紹
在下列網址hadoop說明文件中有wordcount.java 的程式碼:

http://hadoop.apache.org/docs/current/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html

7.2 編輯wordCount.java
Step1 建立wordcount目錄
mkdir -p ~/wordcount/input
cd  ~/wordcount
Step2 編輯WordCount.java
gedit WordCount.java
Step3~6 編輯WordCount.java
在gedit 輸入 WordCount.java完整程式碼
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{

    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 {
    private IntWritable result = new IntWritable();

    public void reduce(Text key, Iterable 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);
  }
}

7.3 編譯wordCount.java
Step1 修改編譯所需要的環境變數檔
sudo gedit ~/.bashrc
輸入下列內容
export PATH=${JAVA_HOME}/bin:${PATH}
export HADOOP_CLASSPATH=${JAVA_HOME}/lib/tools.jar
Step2 讓 ~/.bashrc 修改的設定值生效
source ~/.bashrc
Step3 開始編譯
hadoop com.sun.tools.javac.Main WordCount.java
jar cf wc.jar WordCount*.class
ll
7.4 建立測試文字檔
cp /usr/local/hadoop/LICENSE.txt ~/wordcount/input
ll ~/wordcount/input
start-all.sh
hadoop fs -mkdir -p /user/hduser/wordcount/input
cd ~/wordcount/input
hadoop fs -copyFromLocal LICENSE.txt /user/hduser/wordcount/input
hadoop fs -ls /user/hduser/wordcount/input
7.5 執行wordCount.java
cd ~/wordcount
hadoop jar wc.jar WordCount /user/hduser/wordcount/input/LICENSE.txt /user/hduser/wordcount/output
7.6 查看執行結果
hadoop fs -ls /user/hduser/wordcount/output
hadoop fs -cat /user/hduser/wordcount/output/part-r-00000



 


以上內容節錄自這本書有詳細介紹:
  Hadoop+Spark大數據巨量分析與機器學習整合開發實戰 http://www.books.com.tw/products/0010695285



Share on Google Plus

About kevin

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

5 意見:

  1. 您好:我的照上面編譯,會出現:
    hduser@master:~/wordcount$ hadoop com.sun.tools.javac.Main WordCount.java
    WordCount.java:40: error: incompatible types: Object cannot be converted to IntWritable
    for (IntWritable val : values) {
    ^
    Note: WordCount.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    1 error
    不知如何解決,
    麻煩您了!
    謝謝!

    回覆刪除
    回覆
    1. 建議你直接copy 這裡的程式碼 http://hadoop.apache.org/docs/current/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html 我試過是OK的

      刪除
  2. 您好:
    執行hadoop fs -ls /user/hduser/wordcount/output後,
    資料夾內空無一物,
    不知哪裡出了問題?
    煩請撥冗惠覆。

    回覆刪除