Debian Codegraph @

doc/examples/mr-example/src/main/java/example/MapReduceAvroWordCount.java

raw
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.Schema;
25 import org.apache.avro.Schema.Type;
26 import org.apache.avro.mapred.AvroWrapper;
27 import org.apache.avro.mapred.Pair;
28 import org.apache.avro.mapreduce.AvroJob;
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.conf.Configured;
31 import org.apache.hadoop.fs.Path;
32 import org.apache.hadoop.io.IntWritable;
33 import org.apache.hadoop.io.LongWritable;
34 import org.apache.hadoop.io.NullWritable;
35 import org.apache.hadoop.io.Text;
36 import org.apache.hadoop.mapreduce.Job;
37 import org.apache.hadoop.mapreduce.Mapper;
38 import org.apache.hadoop.mapreduce.Reducer;
39 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
40 import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
41 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
42 import org.apache.hadoop.util.Tool;
43 import org.apache.hadoop.util.ToolRunner;
44
45 /**
46 * The classic WordCount example modified to output Avro Pair<CharSequence,
47 * Integer> records instead of text.
48 */
49 public class MapReduceAvroWordCount extends Configured implements Tool {
50
51 public static class Map
52 extends Mapper<LongWritable, Text, Text, IntWritable> {
53
54 private final static IntWritable one = new IntWritable(1);
55 private Text word = new Text();
56
57 public void map(LongWritable key, Text value, Context context)
58 throws IOException, InterruptedException {
59 String line = value.toString();
60 StringTokenizer tokenizer = new StringTokenizer(line);
61 while (tokenizer.hasMoreTokens()) {
62 word.set(tokenizer.nextToken());
63 context.write(word, one);
64 }
65 }
66 }
67
68 public static class Reduce
69 extends Reducer<Text, IntWritable,
70 AvroWrapper<Pair<CharSequence, Integer>>, NullWritable> {
71
72 public void reduce(Text key, Iterable<IntWritable> values,
73 Context context)
74 throws IOException, InterruptedException {
75 int sum = 0;
76 for (IntWritable value : values) {
77 sum += value.get();
78 }
79 context.write(new AvroWrapper<Pair<CharSequence, Integer>>
80 (new Pair<CharSequence, Integer>(key.toString(), sum)),
81 NullWritable.get());
82 }
83 }
84
85 public int run(String[] args) throws Exception {
86 if (args.length != 2) {
87 System.err.println("Usage: AvroWordCount <input path> <output path>");
88 return -1;
89 }
90
91 Job job = new Job(getConf());
92 job.setJarByClass(MapReduceAvroWordCount.class);
93 job.setJobName("wordcount");
94
95 // We call setOutputSchema first so we can override the configuration
96 // parameters it sets
97 AvroJob.setOutputKeySchema(job,
98 Pair.getPairSchema(Schema.create(Type.STRING),
99 Schema.create(Type.INT)));
100 job.setOutputValueClass(NullWritable.class);
101
102 job.setMapperClass(Map.class);
103 job.setReducerClass(Reduce.class);
104
105 job.setInputFormatClass(TextInputFormat.class);
106
107 job.setMapOutputKeyClass(Text.class);
108 job.setMapOutputValueClass(IntWritable.class);
109 job.setSortComparatorClass(Text.Comparator.class);
110
111 FileInputFormat.setInputPaths(job, new Path(args[0]));
112 FileOutputFormat.setOutputPath(job, new Path(args[1]));
113
114 job.waitForCompletion(true);
115
116 return 0;
117 }
118
119 public static void main(String[] args) throws Exception {
120 int res =
121 ToolRunner.run(new Configuration(), new MapReduceAvroWordCount(), args);
122 System.exit(res);
123 }
124 }