Changes between Initial Version and Version 1 of waue/helloHadoop


Ignore:
Timestamp:
Dec 17, 2011, 12:37:32 PM (13 years ago)
Author:
waue
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • waue/helloHadoop

    v1 v1  
     1{{{
     2#!java
     3
     4import java.io.IOException;
     5
     6import org.apache.hadoop.conf.Configuration;
     7import org.apache.hadoop.fs.Path;
     8import org.apache.hadoop.io.LongWritable;
     9import org.apache.hadoop.io.Text;
     10import org.apache.hadoop.mapreduce.Job;
     11import org.apache.hadoop.mapreduce.Mapper;
     12import org.apache.hadoop.mapreduce.Reducer;
     13import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
     14import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
     15
     16
     17public class HelloHadoop {
     18
     19  static public class HelloMapper extends
     20      Mapper<LongWritable, Text, LongWritable, Text> {
     21
     22    public void map(LongWritable key, Text value, Context context)
     23        throws IOException, InterruptedException {
     24      // 將出入資料 原封不動的寫入 輸出
     25      context.write((LongWritable) key, (Text) value);
     26    }
     27
     28  }
     29
     30  static public class HelloReducer extends
     31      Reducer<LongWritable, Text, LongWritable, Text> {
     32    public void reduce(LongWritable key, Iterable<Text> values,
     33        Context context) throws IOException, InterruptedException {
     34      Text val = new Text();
     35      // 取回 val 的資料
     36      for (Text str : values) {
     37        val.set(str.toString());
     38      }
     39      // 將取回的資料引入輸出
     40      context.write(key, val);
     41    }
     42  }
     43
     44  public static void main(String[] args) throws IOException,
     45      InterruptedException, ClassNotFoundException {
     46    // 引入 $HADOOP_HOME/conf 內控制檔內的資料
     47    Configuration conf = new Configuration();
     48    // 宣告job 取得conf 並設定名稱 Hadoop Hello World
     49    Job job = new Job(conf, "Hadoop Hello World");
     50    // 設定此運算的主程式
     51    job.setJarByClass(HelloHadoop.class);
     52    // 設定輸入路徑
     53    FileInputFormat.setInputPaths(job, "/user/hadoop/input");
     54    // 設定輸出路徑
     55    FileOutputFormat.setOutputPath(job, new Path("/user/hadoop/output-hh1"));
     56    // 指定定map class
     57    job.setMapperClass(HelloMapper.class);
     58    // 指定reduce class
     59    job.setReducerClass(HelloReducer.class);
     60    // 開使運算
     61    job.waitForCompletion(true);
     62
     63  }
     64}
     65
     66}}}