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 | | }}} |