栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Hadoop单词计数:接收以字母“ c”开头的单词总数

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Hadoop单词计数:接收以字母“ c”开头的单词总数

克里斯·格肯 的答案是正确的。

如果您要输出单词作为关键字,则只会帮助您计算以“ c”开头的唯一单词的数量

并非所有“ c”的总数。

因此,您需要从mapper输出一个唯一的密钥。

 while (itr.hasMoreTokens()) { String token = itr.nextToken(); if(token.startsWith("c")){     word.set("C_Count");     output.collect(word, one); }        }

这是使用New Api的示例

驾驶舱

import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Job;import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;public class WordCount {    public static void main(String[] args) throws Exception {        Configuration conf = new Configuration();        Job job = new Job(conf, "wordcount");        FileSystem fs = FileSystem.get(conf);        job.setOutputKeyClass(Text.class);        job.setOutputValueClass(IntWritable.class);        if (fs.exists(new Path(args[1]))) fs.delete(new Path(args[1]), true);        job.setMapperClass(Map.class);        job.setReducerClass(Reduce.class);        job.setInputFormatClass(TextInputFormat.class);        job.setOutputFormatClass(TextOutputFormat.class);        FileInputFormat.addInputPath(job, new Path(args[0]));        FileOutputFormat.setOutputPath(job, new Path(args[1]));        job.setJarByClass(WordCount.class);  job.waitForCompletion(true);    }}

映射器类

import java.io.IOException;import java.util.StringTokenizer;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Mapper;public class Map extends Mapper<LongWritable, Text, Text, IntWritable> {    private final static IntWritable one = new IntWritable(1);    private Text word = new Text();    public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {        String line = value.toString();        StringTokenizer itr = new StringTokenizer(line);        while (itr.hasMoreTokens()) { String token = itr.nextToken(); if(token.startsWith("c")){     word.set("C_Count");     context.write(word, one); }        }    }}

减速机类

import java.io.IOException;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Reducer;public class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {    public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {        int sum = 0;        for (IntWritable val : values) { sum += val.get();        }        context.write(key, new IntWritable(sum));    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/573674.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号