499.Insert Interval-单词计数 (Map Reduce版本)(容易题)

来源:互联网 发布:手机中文解压软件 编辑:程序博客网 时间:2024/06/05 05:51

翻转字符串

  1. 题目

    使用 map reduce 来计算单词频率
    https://hadoop.apache.org/docs/r1.2.1/mapred_tutorial.html#Example%3A+WordCount+v1.0

  2. 样例

这里写图片描述

  1. ###题解###
/** * Definition of OutputCollector: * class OutputCollector<K, V> { *     public void collect(K key, V value); *         // Adds a key/value pair to the output buffer * } */public class WordCount {    //http://www.cnblogs.com/inevermore/p/5232534.html    public static class Map {        public void map(String key, String value, OutputCollector<String, Integer> output) {            String[] arr = value.split(" ");            for (int i=0;i<arr.length;i++)            {                output.collect(arr[i],1);            }        }    }    public static class Reduce {        public void reduce(String key, Iterator<Integer> values,                           OutputCollector<String, Integer> output) {            int sum = 0;            while (values.hasNext())             {              sum += values.next().intValue();            }            output.collect(key, sum);        }    }}

Last Update 2016.9.18

0 0
原创粉丝点击