| 1 | {{{ |
| 2 | /* |
| 3 | |
| 4 | * hadoop sample code |
| 5 | */ |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | import java.io.IOException; |
| 11 | |
| 12 | import java.util.Iterator; |
| 13 | |
| 14 | import java.util.StringTokenizer; |
| 15 | |
| 16 | |
| 17 | |
| 18 | import org.apache.hadoop.fs.FileSystem; |
| 19 | |
| 20 | import org.apache.hadoop.fs.Path; |
| 21 | |
| 22 | import org.apache.hadoop.io.IntWritable; |
| 23 | |
| 24 | import org.apache.hadoop.io.LongWritable; |
| 25 | |
| 26 | import org.apache.hadoop.io.Text; |
| 27 | |
| 28 | import org.apache.hadoop.mapred.JobClient; |
| 29 | |
| 30 | import org.apache.hadoop.mapred.JobConf; |
| 31 | |
| 32 | import org.apache.hadoop.mapred.MapReduceBase; |
| 33 | |
| 34 | import org.apache.hadoop.mapred.Mapper; |
| 35 | |
| 36 | import org.apache.hadoop.mapred.OutputCollector; |
| 37 | |
| 38 | import org.apache.hadoop.mapred.Reducer; |
| 39 | |
| 40 | import org.apache.hadoop.mapred.Reporter; |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | public class WordCount { |
| 46 | |
| 47 | |
| 48 | |
| 49 | // mapper: emits (token, 1) for every word occurrence |
| 50 | |
| 51 | private static class MapClass extends MapReduceBase implements |
| 52 | |
| 53 | Mapper<LongWritable, Text, Text, IntWritable> { |
| 54 | |
| 55 | |
| 56 | |
| 57 | // reuse objects to save overhead of object creation |
| 58 | |
| 59 | private final static IntWritable one = new IntWritable(1); |
| 60 | |
| 61 | private Text word = new Text(); |
| 62 | |
| 63 | |
| 64 | |
| 65 | public void map(LongWritable key, Text value, |
| 66 | |
| 67 | OutputCollector<Text, IntWritable> output, Reporter reporter) |
| 68 | |
| 69 | throws IOException { |
| 70 | |
| 71 | String line = ((Text) value).toString(); |
| 72 | |
| 73 | StringTokenizer itr = new StringTokenizer(line); |
| 74 | |
| 75 | while (itr.hasMoreTokens()) { |
| 76 | |
| 77 | word.set(itr.nextToken()); |
| 78 | |
| 79 | output.collect(word, one); |
| 80 | |
| 81 | } |
| 82 | |
| 83 | } |
| 84 | |
| 85 | } |
| 86 | |
| 87 | |
| 88 | |
| 89 | // reducer: sums up all the counts |
| 90 | |
| 91 | private static class ReduceClass extends MapReduceBase implements |
| 92 | |
| 93 | Reducer<Text, IntWritable, Text, IntWritable> { |
| 94 | |
| 95 | |
| 96 | |
| 97 | // reuse objects |
| 98 | |
| 99 | private final static IntWritable SumValue = new IntWritable(); |
| 100 | |
| 101 | |
| 102 | |
| 103 | public void reduce(Text key, Iterator<IntWritable> values, |
| 104 | |
| 105 | OutputCollector<Text, IntWritable> output, Reporter reporter) |
| 106 | |
| 107 | throws IOException { |
| 108 | |
| 109 | // sum up values |
| 110 | |
| 111 | int sum = 0; |
| 112 | |
| 113 | while (values.hasNext()) { |
| 114 | |
| 115 | sum += values.next().get(); |
| 116 | |
| 117 | } |
| 118 | |
| 119 | SumValue.set(sum); |
| 120 | |
| 121 | output.collect(key, SumValue); |
| 122 | |
| 123 | } |
| 124 | |
| 125 | } |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | /** |
| 131 | |
| 132 | * Runs the demo. |
| 133 | |
| 134 | */ |
| 135 | |
| 136 | public static void main(String[] args) throws IOException { |
| 137 | |
| 138 | String filename = "/user/waue/test/132.txt"; |
| 139 | |
| 140 | String outputPath = "sample-counts"; |
| 141 | |
| 142 | int mapTasks = 20; |
| 143 | |
| 144 | int reduceTasks = 1; |
| 145 | |
| 146 | |
| 147 | |
| 148 | JobConf conf = new JobConf(DemoWordCount.class); |
| 149 | |
| 150 | conf.setJobName("wordcount"); |
| 151 | |
| 152 | |
| 153 | |
| 154 | conf.setNumMapTasks(mapTasks); |
| 155 | |
| 156 | conf.setNumReduceTasks(reduceTasks); |
| 157 | |
| 158 | |
| 159 | |
| 160 | conf.setInputPath(new Path(filename)); |
| 161 | |
| 162 | conf.setOutputKeyClass(Text.class); |
| 163 | |
| 164 | conf.setOutputValueClass(IntWritable.class); |
| 165 | |
| 166 | conf.setOutputPath(new Path(outputPath)); |
| 167 | |
| 168 | |
| 169 | |
| 170 | conf.setMapperClass(MapClass.class); |
| 171 | |
| 172 | conf.setCombinerClass(ReduceClass.class); |
| 173 | |
| 174 | conf.setReducerClass(ReduceClass.class); |
| 175 | |
| 176 | |
| 177 | |
| 178 | // Delete the output directory if it exists already |
| 179 | |
| 180 | Path outputDir = new Path(outputPath); |
| 181 | |
| 182 | FileSystem.get(conf).delete(outputDir); |
| 183 | |
| 184 | |
| 185 | |
| 186 | JobClient.runJob(conf); |
| 187 | |
| 188 | } |
| 189 | |
| 190 | } |
| 191 | |
| 192 | }}} |