[120] | 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 | #include "hadoop/Pipes.hh" |
---|
| 20 | #include "hadoop/TemplateFactory.hh" |
---|
| 21 | #include "hadoop/StringUtils.hh" |
---|
| 22 | |
---|
| 23 | const std::string WORDCOUNT = "WORDCOUNT"; |
---|
| 24 | const std::string INPUT_WORDS = "INPUT_WORDS"; |
---|
| 25 | const std::string OUTPUT_WORDS = "OUTPUT_WORDS"; |
---|
| 26 | |
---|
| 27 | class WordCountMap: public HadoopPipes::Mapper { |
---|
| 28 | public: |
---|
| 29 | HadoopPipes::TaskContext::Counter* inputWords; |
---|
| 30 | |
---|
| 31 | WordCountMap(HadoopPipes::TaskContext& context) { |
---|
| 32 | inputWords = context.getCounter(WORDCOUNT, INPUT_WORDS); |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | void map(HadoopPipes::MapContext& context) { |
---|
| 36 | std::vector<std::string> words = |
---|
| 37 | HadoopUtils::splitString(context.getInputValue(), " "); |
---|
| 38 | for(unsigned int i=0; i < words.size(); ++i) { |
---|
| 39 | context.emit(words[i], "1"); |
---|
| 40 | } |
---|
| 41 | context.incrementCounter(inputWords, words.size()); |
---|
| 42 | } |
---|
| 43 | }; |
---|
| 44 | |
---|
| 45 | class WordCountReduce: public HadoopPipes::Reducer { |
---|
| 46 | public: |
---|
| 47 | HadoopPipes::TaskContext::Counter* outputWords; |
---|
| 48 | |
---|
| 49 | WordCountReduce(HadoopPipes::TaskContext& context) { |
---|
| 50 | outputWords = context.getCounter(WORDCOUNT, OUTPUT_WORDS); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | void reduce(HadoopPipes::ReduceContext& context) { |
---|
| 54 | int sum = 0; |
---|
| 55 | while (context.nextValue()) { |
---|
| 56 | sum += HadoopUtils::toInt(context.getInputValue()); |
---|
| 57 | } |
---|
| 58 | context.emit(context.getInputKey(), HadoopUtils::toString(sum)); |
---|
| 59 | context.incrementCounter(outputWords, 1); |
---|
| 60 | } |
---|
| 61 | }; |
---|
| 62 | |
---|
| 63 | int main(int argc, char *argv[]) { |
---|
| 64 | return HadoopPipes::runTask(HadoopPipes::TemplateFactory<WordCountMap, |
---|
| 65 | WordCountReduce>()); |
---|
| 66 | } |
---|
| 67 | |
---|