| | 76 | |
| | 77 | * mapper |
| | 78 | |
| | 79 | {{{ |
| | 80 | #!java |
| | 81 | import java.io.IOException; |
| | 82 | import org.apache.hadoop.io.LongWritable; |
| | 83 | import org.apache.hadoop.io.Text; |
| | 84 | import org.apache.hadoop.mapreduce.Mapper; |
| | 85 | |
| | 86 | public class HelloMapperV2 extends Mapper<LongWritable, Text, Text, Text> { |
| | 87 | |
| | 88 | public void map(LongWritable key, Text value, Context context) |
| | 89 | throws IOException, InterruptedException { |
| | 90 | context.write(new Text(key.toString()), value); |
| | 91 | } |
| | 92 | |
| | 93 | } |
| | 94 | |
| | 95 | }}} |
| | 96 | |
| | 97 | * reducer |
| | 98 | |
| | 99 | {{{ |
| | 100 | #!java |
| | 101 | import java.io.IOException; |
| | 102 | import org.apache.hadoop.io.Text; |
| | 103 | import org.apache.hadoop.mapreduce.Reducer; |
| | 104 | |
| | 105 | public class HelloReducerV2 extends Reducer<Text, Text, Text, Text> { |
| | 106 | public void reduce(Text key, Iterable<Text> values, Context context) |
| | 107 | throws IOException, InterruptedException { |
| | 108 | |
| | 109 | String str = new String(""); |
| | 110 | Text final_key = new Text(); |
| | 111 | Text final_value = new Text(); |
| | 112 | // 將key值相同的values,透過 && 符號分隔之 |
| | 113 | for (Text tmp : values) { |
| | 114 | str += tmp.toString() + " &&"; |
| | 115 | } |
| | 116 | |
| | 117 | final_key.set(key); |
| | 118 | final_value.set(str); |
| | 119 | |
| | 120 | context.write(final_key, final_value); |
| | 121 | } |
| | 122 | } |
| | 123 | }}} |