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 | * http://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 org.apache.hadoop.examples; |
---|
20 | |
---|
21 | import java.io.IOException; |
---|
22 | import java.util.ArrayList; |
---|
23 | import java.util.Map.Entry; |
---|
24 | |
---|
25 | import org.apache.hadoop.io.Text; |
---|
26 | import org.apache.hadoop.mapred.JobClient; |
---|
27 | import org.apache.hadoop.mapred.JobConf; |
---|
28 | import org.apache.hadoop.mapred.lib.aggregate.ValueAggregatorBaseDescriptor; |
---|
29 | import org.apache.hadoop.mapred.lib.aggregate.ValueAggregatorJob; |
---|
30 | |
---|
31 | /** |
---|
32 | * This is an example Aggregated Hadoop Map/Reduce application. Computes the |
---|
33 | * histogram of the words in the input texts. |
---|
34 | * |
---|
35 | * To run: bin/hadoop jar hadoop-*-examples.jar aggregatewordhist <i>in-dir</i> |
---|
36 | * <i>out-dir</i> <i>numOfReducers</i> textinputformat |
---|
37 | * |
---|
38 | */ |
---|
39 | public class AggregateWordHistogram { |
---|
40 | |
---|
41 | public static class AggregateWordHistogramPlugin |
---|
42 | extends ValueAggregatorBaseDescriptor { |
---|
43 | |
---|
44 | /** |
---|
45 | * Parse the given value, generate an aggregation-id/value pair per word. |
---|
46 | * The ID is of type VALUE_HISTOGRAM, with WORD_HISTOGRAM as the real id. |
---|
47 | * The value is WORD\t1. |
---|
48 | * |
---|
49 | * @return a list of the generated pairs. |
---|
50 | */ |
---|
51 | @Override |
---|
52 | public ArrayList<Entry<Text, Text>> generateKeyValPairs(Object key, Object val) { |
---|
53 | String words[] = val.toString().split(" |\t"); |
---|
54 | ArrayList<Entry<Text, Text>> retv = new ArrayList<Entry<Text, Text>>(); |
---|
55 | for (int i = 0; i < words.length; i++) { |
---|
56 | Text valCount = new Text(words[i] + "\t" + "1"); |
---|
57 | Entry<Text, Text> en = generateEntry(VALUE_HISTOGRAM, "WORD_HISTOGRAM", |
---|
58 | valCount); |
---|
59 | retv.add(en); |
---|
60 | } |
---|
61 | return retv; |
---|
62 | } |
---|
63 | |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | * The main driver for word count map/reduce program. Invoke this method to |
---|
68 | * submit the map/reduce job. |
---|
69 | * |
---|
70 | * @throws IOException |
---|
71 | * When there is communication problems with the job tracker. |
---|
72 | */ |
---|
73 | @SuppressWarnings("unchecked") |
---|
74 | public static void main(String[] args) throws IOException { |
---|
75 | JobConf conf = ValueAggregatorJob.createValueAggregatorJob(args |
---|
76 | , new Class[] {AggregateWordHistogramPlugin.class}); |
---|
77 | |
---|
78 | JobClient.runJob(conf); |
---|
79 | } |
---|
80 | |
---|
81 | } |
---|