| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* https://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
package example; |
| 20 |
|
| 21 |
import java.io.IOException; |
| 22 |
import java.util.*; |
| 23 |
|
| 24 |
import org.apache.avro.*; |
| 25 |
import org.apache.avro.Schema.Type; |
| 26 |
import org.apache.avro.mapred.*; |
| 27 |
import org.apache.hadoop.conf.*; |
| 28 |
import org.apache.hadoop.fs.Path; |
| 29 |
import org.apache.hadoop.io.*; |
| 30 |
import org.apache.hadoop.mapred.*; |
| 31 |
import org.apache.hadoop.util.*; |
| 32 |
|
| 33 |
/** |
| 34 |
* The classic WordCount example modified to output Avro Pair<CharSequence, |
| 35 |
* Integer> records instead of text. |
| 36 |
*/ |
| 37 |
public class AvroWordCount extends Configured implements Tool { |
| 38 |
|
| 39 |
public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> { |
| 40 |
private final static IntWritable one = new IntWritable(1); |
| 41 |
private Text word = new Text(); |
| 42 |
|
| 43 |
public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) |
| 44 |
throws IOException { |
| 45 |
String line = value.toString(); |
| 46 |
StringTokenizer tokenizer = new StringTokenizer(line); |
| 47 |
while (tokenizer.hasMoreTokens()) { |
| 48 |
word.set(tokenizer.nextToken()); |
| 49 |
output.collect(word, one); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
public static class Reduce extends MapReduceBase |
| 55 |
implements Reducer<Text, IntWritable, |
| 56 |
AvroWrapper<Pair<CharSequence, Integer>>, NullWritable> { |
| 57 |
|
| 58 |
public void reduce(Text key, Iterator<IntWritable> values, |
| 59 |
OutputCollector<AvroWrapper<Pair<CharSequence, Integer>>, NullWritable> output, |
| 60 |
Reporter reporter) throws IOException { |
| 61 |
int sum = 0; |
| 62 |
while (values.hasNext()) { |
| 63 |
sum += values.next().get(); |
| 64 |
} |
| 65 |
output.collect(new AvroWrapper<Pair<CharSequence, Integer>>( |
| 66 |
new Pair<CharSequence, Integer>(key.toString(), sum)), |
| 67 |
NullWritable.get()); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public int run(String[] args) throws Exception { |
| 72 |
if (args.length != 2) { |
| 73 |
System.err.println("Usage: AvroWordCount <input path> <output path>"); |
| 74 |
return -1; |
| 75 |
} |
| 76 |
|
| 77 |
JobConf conf = new JobConf(AvroWordCount.class); |
| 78 |
conf.setJobName("wordcount"); |
| 79 |
|
| 80 |
// We call setOutputSchema first so we can override the configuration |
| 81 |
// parameters it sets |
| 82 |
AvroJob.setOutputSchema(conf, Pair.getPairSchema(Schema.create(Type.STRING), |
| 83 |
Schema.create(Type.INT))); |
| 84 |
|
| 85 |
conf.setMapperClass(Map.class); |
| 86 |
conf.setReducerClass(Reduce.class); |
| 87 |
|
| 88 |
conf.setInputFormat(TextInputFormat.class); |
| 89 |
|
| 90 |
conf.setMapOutputKeyClass(Text.class); |
| 91 |
conf.setMapOutputValueClass(IntWritable.class); |
| 92 |
conf.setOutputKeyComparatorClass(Text.Comparator.class); |
| 93 |
|
| 94 |
FileInputFormat.setInputPaths(conf, new Path(args[0])); |
| 95 |
FileOutputFormat.setOutputPath(conf, new Path(args[1])); |
| 96 |
|
| 97 |
JobClient.runJob(conf); |
| 98 |
return 0; |
| 99 |
} |
| 100 |
|
| 101 |
public static void main(String[] args) throws Exception { |
| 102 |
int res = ToolRunner.run(new Configuration(), new AvroWordCount(), args); |
| 103 |
System.exit(res); |
| 104 |
} |
| 105 |
} |