| | 1 | {{{ |
| | 2 | #!java |
| | 3 | import java.io.IOException; |
| | 4 | import java.util.Iterator; |
| | 5 | |
| | 6 | import org.apache.hadoop.fs.Path; |
| | 7 | import org.apache.hadoop.io.LongWritable; |
| | 8 | import org.apache.hadoop.io.Text; |
| | 9 | import org.apache.hadoop.mapred.FileInputFormat; |
| | 10 | import org.apache.hadoop.mapred.FileOutputFormat; |
| | 11 | import org.apache.hadoop.mapred.JobClient; |
| | 12 | import org.apache.hadoop.mapred.JobConf; |
| | 13 | import org.apache.hadoop.mapred.MapReduceBase; |
| | 14 | import org.apache.hadoop.mapred.Mapper; |
| | 15 | import org.apache.hadoop.mapred.OutputCollector; |
| | 16 | import org.apache.hadoop.mapred.Reducer; |
| | 17 | import org.apache.hadoop.mapred.Reporter; |
| | 18 | import org.apache.hadoop.mapred.TextInputFormat; |
| | 19 | import org.apache.hadoop.mapred.TextOutputFormat; |
| | 20 | |
| | 21 | public class Hello { |
| | 22 | |
| | 23 | public static class Map extends MapReduceBase implements |
| | 24 | Mapper<LongWritable, Text, LongWritable, Text> { |
| | 25 | |
| | 26 | public void map(LongWritable key, Text value, |
| | 27 | OutputCollector<LongWritable, Text> output, Reporter reporter) |
| | 28 | throws IOException { |
| | 29 | |
| | 30 | output.collect(key, value); |
| | 31 | } |
| | 32 | } |
| | 33 | |
| | 34 | public static class Reduce extends MapReduceBase implements |
| | 35 | Reducer<LongWritable, Text, LongWritable, Text> { |
| | 36 | Text ret = new Text(""); |
| | 37 | |
| | 38 | public void reduce(LongWritable key, Iterator<Text> values, |
| | 39 | OutputCollector<LongWritable, Text> output, Reporter reporter) |
| | 40 | throws IOException { |
| | 41 | |
| | 42 | while (values.hasNext()) { |
| | 43 | ret = values.next(); |
| | 44 | } |
| | 45 | output.collect(key, ret); |
| | 46 | } |
| | 47 | } |
| | 48 | |
| | 49 | public static void main(String[] args) throws Exception { |
| | 50 | JobConf conf = new JobConf(Hello.class); |
| | 51 | conf.setJobName("Hello"); |
| | 52 | |
| | 53 | conf.setMapperClass(Map.class); |
| | 54 | conf.setCombinerClass(Reduce.class); |
| | 55 | conf.setReducerClass(Reduce.class); |
| | 56 | conf.setMapOutputKeyClass(LongWritable.class); |
| | 57 | conf.setMapOutputValueClass(Text.class); |
| | 58 | conf.setOutputKeyClass(LongWritable.class); |
| | 59 | conf.setOutputValueClass(Text.class); |
| | 60 | |
| | 61 | conf.setInputFormat(TextInputFormat.class); |
| | 62 | conf.setOutputFormat(TextOutputFormat.class); |
| | 63 | |
| | 64 | FileInputFormat.setInputPaths(conf, new Path(args[0])); |
| | 65 | FileOutputFormat.setOutputPath(conf, new Path(args[1])); |
| | 66 | |
| | 67 | JobClient.runJob(conf); |
| | 68 | } |
| | 69 | } |
| | 70 | |
| | 71 | }}} |