Changes between Initial Version and Version 1 of waue/2010/0115-HelloHadoopV2


Ignore:
Timestamp:
Jan 19, 2010, 7:48:21 PM (14 years ago)
Author:
waue
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • waue/2010/0115-HelloHadoopV2

    v1 v1  
     1 = Hello Hadoop V2  =
     2
     3{{{
     4#!java
     5import java.io.IOException;
     6
     7import org.apache.hadoop.conf.Configuration;
     8import org.apache.hadoop.fs.Path;
     9import org.apache.hadoop.io.Text;
     10import org.apache.hadoop.mapreduce.Job;
     11import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
     12import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
     13
     14//      HelloHadoopV2
     15//      說明:
     16//              此程式碼比HelloHadoop 增加
     17//              * 檢查輸出資料夾是否存在並刪除
     18//              * input 資料夾內的資料若大於兩個,則資料不會被覆蓋
     19//              * map 與 reduce 拆開以利程式再利用
     20//
     21//      測試方法:
     22//              將此程式運作在hadoop 0.20 平台上,執行:
     23//              ---------------------------
     24//              hadoop jar HelloHadoopV2.jar
     25//              ---------------------------
     26//
     27//      注意:
     28//      1.      在hdfs 上來源檔案的路徑為 "/user/$YOUR_NAME/input"
     29//              請注意必須先放資料到此hdfs上的資料夾內,且此資料夾內只能放檔案,不可再放資料夾
     30//      2.      運算完後,程式將執行結果放在hdfs 的輸出路徑為 "/user/$YOUR_NAME/output-hh2"
     31//             
     32
     33public class HelloHadoopV2 {
     34
     35
     36        public static void main(String[] args) throws IOException,
     37                        InterruptedException, ClassNotFoundException {
     38
     39                Configuration conf = new Configuration();
     40                Job job = new Job(conf, "Hadoop Hello World 2");
     41                job.setJarByClass(HelloHadoopV2.class);
     42                // 設定 map and reduce 以及 Combiner class
     43                job.setMapperClass(HelloMapperV2.class);
     44                job.setCombinerClass(HelloReducerV2.class);
     45                job.setReducerClass(HelloReducerV2.class);
     46
     47                // 設定map的輸出型態
     48                job.setMapOutputKeyClass(Text.class);
     49                job.setMapOutputValueClass(Text.class);
     50                // 設定reduce的輸出型態
     51                job.setOutputKeyClass(Text.class);
     52                job.setOutputValueClass(Text.class);
     53
     54                FileInputFormat.addInputPath(job, new Path("input"));
     55
     56                FileOutputFormat.setOutputPath(job, new Path("output-hh2"));
     57
     58                // 呼叫checkAndDelete函式,檢查是否存在該資料夾,若有則刪除之
     59                CheckAndDelete.checkAndDelete("output-hh2", conf);
     60
     61                boolean status = job.waitForCompletion(true);
     62
     63                if (status) {
     64                        System.err.println("Integrate Alert Job Finished !");
     65
     66                } else {
     67                        System.err.println("Integrate Alert Job Failed !");
     68                        System.exit(1);
     69                }
     70        }
     71}
     72
     73}}}