| | 1 | |
| | 2 | {{{ |
| | 3 | #!html |
| | 4 | <div style="text-align: center; color:#151B8D"><big style="font-weight: bold;"><big><big> |
| | 5 | Hadoop 進階課程 |
| | 6 | </big></big></big></div> <div style="text-align: center; color:#7E2217"><big style="font-weight: bold;"><big> |
| | 7 | 範例練習 |
| | 8 | </big></big></div> |
| | 9 | }}} |
| | 10 | |
| | 11 | = 第一關 [wiki:NCHCCloudCourse100928_4_EXM2 下一關 ] = |
| | 12 | |
| | 13 | * 看懂以下程式碼,並成功執行它 |
| | 14 | |
| | 15 | {{{ |
| | 16 | #!java |
| | 17 | package org.nchc.hadoop; |
| | 18 | import java.io.IOException; |
| | 19 | |
| | 20 | import org.apache.hadoop.conf.Configuration; |
| | 21 | import org.apache.hadoop.fs.Path; |
| | 22 | import org.apache.hadoop.io.LongWritable; |
| | 23 | import org.apache.hadoop.io.Text; |
| | 24 | import org.apache.hadoop.mapreduce.Job; |
| | 25 | import org.apache.hadoop.mapreduce.Mapper; |
| | 26 | import org.apache.hadoop.mapreduce.Reducer; |
| | 27 | import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; |
| | 28 | import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; |
| | 29 | |
| | 30 | // HelloHadoop |
| | 31 | // 說明: |
| | 32 | // hadoop的hello world程式 |
| | 33 | // 此程式可用來瞭解 hadoop的 <key , value> 為何值,並且練習hadoop api coding |
| | 34 | // |
| | 35 | // 測試方法: |
| | 36 | // 將此程式運作在hadoop 0.20 平台上,執行: |
| | 37 | // --------------------------- |
| | 38 | // hadoop jar HelloHadoop.jar |
| | 39 | // --------------------------- |
| | 40 | // |
| | 41 | // 注意: |
| | 42 | // 1. 在hdfs 上來源檔案的路徑為 "/user/$YOUR_NAME/input" |
| | 43 | // 請注意必須先放資料到此hdfs上的資料夾內,且此資料夾內只能放檔案,不可再放資料夾 |
| | 44 | // input資料內的檔案若個數超過1個,則運算結果將會以某一個為主 |
| | 45 | // 2. 運算完後,程式將執行結果放在hdfs 的輸出路徑為 "/user/$YOUR_NAME/output-hh1" |
| | 46 | // 請注意此資料夾為運算之後才產生的,故運算之前不可有此資料夾 |
| | 47 | |
| | 48 | public class HelloHadoop { |
| | 49 | |
| | 50 | static public class HelloMapper extends |
| | 51 | Mapper<LongWritable, Text, LongWritable, Text> { |
| | 52 | |
| | 53 | public void map(LongWritable key, Text value, Context context) |
| | 54 | throws IOException, InterruptedException { |
| | 55 | // 將出入資料 原封不動的寫入 輸出 |
| | 56 | context.write((LongWritable) key, (Text) value); |
| | 57 | } |
| | 58 | |
| | 59 | } |
| | 60 | |
| | 61 | static public class HelloReducer extends |
| | 62 | Reducer<LongWritable, Text, LongWritable, Text> { |
| | 63 | public void reduce(LongWritable key, Iterable<Text> values, |
| | 64 | Context context) throws IOException, InterruptedException { |
| | 65 | Text val = new Text(); |
| | 66 | // 取回 val 的資料 |
| | 67 | for (Text str : values) { |
| | 68 | val.set(str.toString()); |
| | 69 | } |
| | 70 | // 將取回的資料引入輸出 |
| | 71 | context.write(key, val); |
| | 72 | } |
| | 73 | } |
| | 74 | |
| | 75 | public static void main(String[] args) throws IOException, |
| | 76 | InterruptedException, ClassNotFoundException { |
| | 77 | // 引入 $HADOOP_HOME/conf 內控制檔內的資料 |
| | 78 | Configuration conf = new Configuration(); |
| | 79 | // 宣告job 取得conf 並設定名稱 Hadoop Hello World |
| | 80 | Job job = new Job(conf, "Hadoop Hello World"); |
| | 81 | // 設定此運算的主程式 |
| | 82 | job.setJarByClass(HelloHadoop.class); |
| | 83 | // 設定輸入路徑 |
| | 84 | FileInputFormat.setInputPaths(job, "/user/hadooper/input"); |
| | 85 | // 設定輸出路徑 |
| | 86 | FileOutputFormat.setOutputPath(job, new Path("/user/hadooper/output-hh1")); |
| | 87 | // 指定定map class |
| | 88 | job.setMapperClass(HelloMapper.class); |
| | 89 | // 指定reduce class |
| | 90 | job.setReducerClass(HelloReducer.class); |
| | 91 | // 開使運算 |
| | 92 | job.waitForCompletion(true); |
| | 93 | |
| | 94 | } |
| | 95 | } |
| | 96 | |
| | 97 | }}} |
| | 98 | |