| 108 | |
| 109 | ---------- |
| 110 | 改成以下的map 與 reduce 檔就可以正常運作 |
| 111 | |
| 112 | * kvM.java |
| 113 | |
| 114 | {{{ |
| 115 | #!java |
| 116 | package nchc.keyvalue; |
| 117 | |
| 118 | import java.io.IOException; |
| 119 | |
| 120 | import org.apache.hadoop.io.LongWritable; |
| 121 | import org.apache.hadoop.io.Text; |
| 122 | import org.apache.hadoop.mapred.MapReduceBase; |
| 123 | import org.apache.hadoop.mapred.Mapper; |
| 124 | import org.apache.hadoop.mapred.OutputCollector; |
| 125 | import org.apache.hadoop.mapred.Reporter; |
| 126 | |
| 127 | public class kvM extends MapReduceBase implements |
| 128 | Mapper<LongWritable, Text, LongWritable, Text> { |
| 129 | |
| 130 | public void map(LongWritable key, Text value, |
| 131 | OutputCollector<LongWritable, Text> output, Reporter report) |
| 132 | throws IOException { |
| 133 | output.collect(key, value); |
| 134 | } |
| 135 | |
| 136 | } |
| 137 | }}} |
| 138 | |
| 139 | * kvR.java |
| 140 | |
| 141 | {{{ |
| 142 | #!java |
| 143 | package nchc.keyvalue; |
| 144 | |
| 145 | import java.io.IOException; |
| 146 | import java.util.Iterator; |
| 147 | |
| 148 | import org.apache.hadoop.io.LongWritable; |
| 149 | import org.apache.hadoop.io.Text; |
| 150 | import org.apache.hadoop.mapred.MapReduceBase; |
| 151 | import org.apache.hadoop.mapred.OutputCollector; |
| 152 | import org.apache.hadoop.mapred.Reducer; |
| 153 | import org.apache.hadoop.mapred.Reporter; |
| 154 | |
| 155 | public class kvR extends MapReduceBase implements |
| 156 | Reducer< LongWritable, Text, Text, Text> { |
| 157 | public void reduce(LongWritable key, Iterator<Text> values, |
| 158 | OutputCollector<Text, Text> output, Reporter report) |
| 159 | throws IOException { |
| 160 | while (values.hasNext()) { |
| 161 | Text keyv = new Text("< "+key+" , "); |
| 162 | Text val = new Text(values.next()+">"); |
| 163 | output.collect(keyv, val); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | }}} |
| 168 | |
| 169 | 然而就看不到key被reduce起來 |
| 170 | |
| 171 | * output/part-00000 |
| 172 | {{{ |
| 173 | < 0 , This eBook is for the use of anyone anywhere at no cost and with> |
| 174 | < 0 , This eBook is for the use of anyone anywhere at no cost and with> |
| 175 | < 66 , almost no restrictions whatsoever. You may copy it, give it away or> |
| 176 | < 66 , almost no restrictions whatsoever. You may copy it, give it away or> |
| 177 | < 136 , re-use it under the terms of the Project Gutenberg License included> |
| 178 | < 136 , re-use it under the terms of the Project Gutenberg License included> |
| 179 | < 205 , with this eBook or online at www.gutenberg.net> |
| 180 | < 205 , with this eBook or online at www.gutenberg.org> |
| 181 | |
| 182 | .... (省略) |
| 183 | }}} |