| 1 | |
| 2 | |
| 3 | {{{ |
| 4 | #!java |
| 5 | package org.nchc.hadoop; |
| 6 | import java.io.IOException; |
| 7 | |
| 8 | import org.apache.hadoop.io.LongWritable; |
| 9 | import org.apache.hadoop.io.Text; |
| 10 | import org.apache.hadoop.mapreduce.Mapper; |
| 11 | |
| 12 | public class HelloMapperV2 extends Mapper<LongWritable, Text, Text, Text> { |
| 13 | |
| 14 | public void map(LongWritable key, Text value, Context context) |
| 15 | throws IOException, InterruptedException { |
| 16 | context.write(new Text(key.toString()), value); |
| 17 | } |
| 18 | |
| 19 | } |
| 20 | |
| 21 | }}} |
| 22 | |
| 23 | = HelloReducerV2.java = |
| 24 | |
| 25 | {{{ |
| 26 | #!java |
| 27 | package org.nchc.hadoop; |
| 28 | import java.io.IOException; |
| 29 | |
| 30 | import org.apache.hadoop.io.Text; |
| 31 | import org.apache.hadoop.mapreduce.Reducer; |
| 32 | |
| 33 | public class HelloReducerV2 extends Reducer<Text, Text, Text, Text> { |
| 34 | public void reduce(Text key, Iterable<Text> values, Context context) |
| 35 | throws IOException, InterruptedException { |
| 36 | |
| 37 | String str = new String(""); |
| 38 | Text final_key = new Text(); |
| 39 | Text final_value = new Text(); |
| 40 | // 將key值相同的values,透過 && 符號分隔之 |
| 41 | for (Text tmp : values) { |
| 42 | str += tmp.toString() + " &&"; |
| 43 | } |
| 44 | |
| 45 | final_key.set(key); |
| 46 | final_value.set(str); |
| 47 | |
| 48 | context.write(final_key, final_value); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | }}} |
| 53 | |
| 54 | = HelloHadoopV2.java = |
| 55 | |
| 56 | {{{ |
| 57 | #!java |
| 58 | package org.nchc.hadoop; |
| 59 | import java.io.IOException; |
| 60 | |
| 61 | import org.apache.hadoop.conf.Configuration; |
| 62 | import org.apache.hadoop.fs.Path; |
| 63 | import org.apache.hadoop.io.Text; |
| 64 | import org.apache.hadoop.mapreduce.Job; |
| 65 | import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; |
| 66 | import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; |
| 67 | |
| 68 | public class HelloHadoopV2 { |
| 69 | |
| 70 | |
| 71 | public static void main(String[] args) throws IOException, |
| 72 | InterruptedException, ClassNotFoundException { |
| 73 | |
| 74 | Configuration conf = new Configuration(); |
| 75 | Job job = new Job(conf, "Hadoop Hello World 2"); |
| 76 | job.setJarByClass(HelloHadoopV2.class); |
| 77 | // 設定 map and reduce 以及 Combiner class |
| 78 | job.setMapperClass(HelloMapperV2.class); |
| 79 | job.setCombinerClass(HelloReducerV2.class); |
| 80 | job.setReducerClass(HelloReducerV2.class); |
| 81 | |
| 82 | // 設定map的輸出型態 |
| 83 | job.setMapOutputKeyClass(Text.class); |
| 84 | job.setMapOutputValueClass(Text.class); |
| 85 | // 設定reduce的輸出型態 |
| 86 | job.setOutputKeyClass(Text.class); |
| 87 | job.setOutputValueClass(Text.class); |
| 88 | |
| 89 | FileInputFormat.addInputPath(job, new Path("/user/hadoop/input")); |
| 90 | |
| 91 | FileOutputFormat.setOutputPath(job, new Path("/user/hadoop/output-hh2")); |
| 92 | |
| 93 | // 呼叫checkAndDelete函式,檢查是否存在該資料夾,若有則刪除之 |
| 94 | CheckAndDelete.checkAndDelete("/user/hadoop/output-hh2", conf); |
| 95 | |
| 96 | boolean status = job.waitForCompletion(true); |
| 97 | |
| 98 | if (status) { |
| 99 | System.err.println("Integrate Alert Job Finished !"); |
| 100 | |
| 101 | } else { |
| 102 | System.err.println("Integrate Alert Job Failed !"); |
| 103 | System.exit(1); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | }}} |