| 1 | {{{ |
| 2 | #!java |
| 3 | |
| 4 | import java.io.IOException; |
| 5 | |
| 6 | import org.apache.hadoop.conf.Configuration; |
| 7 | import org.apache.hadoop.fs.Path; |
| 8 | import org.apache.hadoop.io.LongWritable; |
| 9 | import org.apache.hadoop.io.Text; |
| 10 | import org.apache.hadoop.mapreduce.Job; |
| 11 | import org.apache.hadoop.mapreduce.Mapper; |
| 12 | import org.apache.hadoop.mapreduce.Reducer; |
| 13 | import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; |
| 14 | import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; |
| 15 | |
| 16 | |
| 17 | public 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 | }}} |