hadoop mapreduce获取每个单词的百分比

a0zr77ik  于 2021-06-02  发布在  Hadoop
关注(0)|答案(2)|浏览(760)

我正在使用hadoopmapreduce获取单词和单词计数信息。除了每个单词的计数,我还需要找到文档中显示的每个单词的百分比。输出是这样的。如果文档只包含三个词“hello”、“world”和“kitty”。结果应该是这样的。
字数百分比
你好40 0.4
世界50 0.5
基蒂10 0.1
我可以设定一个 TOTAL_KEY 要计算所有单词,问题是当每个单词计数返回时,结果将同时返回。当输出每个字到hdfs时,无法计算当时的百分比。

sczxawaw

sczxawaw1#

可以在Map器中设置计数器。
从Map器发出单词时,递增全局计数器以计算单词总数。
在减速机中使用计数器来获得发出的单词总数。
使用一般方法计算百分比。

ui7jx7zq

ui7jx7zq2#

job.setJarByClass(WinnerCount.class);
    job.setMapperClass(WinnerCountMapper.class);
    //job.setCombinerClass(WinnerCountCombiner.class);
    job.setCombinerClass(WinnerCountCombiner.class);
    job.setReducerClass(WinnerCountReducer.class);
    job.setInputFormatClass(ChessInputFormat.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    FileInputFormat.addInputPath(job, new Path(inputPath));
    FileOutputFormat.setOutputPath(job, temp);

    job.waitForCompletion(true);

    Counters counters = job.getCounters();
    Counter total = counters.findCounter(WinnerCountMapper.MAP_COUNTER.TOTAL_TUPLE);
    //System.out.println(total.getDisplayName()+":"+total.getValue());

    /*if (hdfs.exists(output))
        hdfs.delete(output, true);*/

    Configuration conf2 = new Configuration();
    conf2.set("total", total.getValue()+"");
    Job job2 = Job.getInstance(conf2,"aggregation");

    job2.setJarByClass(WinnerCount.class);
    job2.setMapperClass(AggregationMapper.class);
    job2.setReducerClass(AggregationReducer.class);
    job2.setOutputKeyClass(Text.class);
    job2.setOutputValueClass(Text.class);

相关问题