当前位置: 首页 > 面试题库 >

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

吴高远
2023-03-14
问题内容

这是Hadoop字数统计Java映射并减少源代码:

在map函数中,我已经到了可以输出所有以字母“ c”开头的单词以及该单词出现的总次数的位置,但是我想做的就是输出总数以字母“
c”开头的单词,但我在获取总数上有些停留。任何帮助将不胜感激,谢谢。

我得到的输出:

可以2

罐3

猫5

我想要得到的是:

合计10

public static class MapClass extends MapReduceBase
   implements 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,
                OutputCollector<Text, IntWritable> output,
                Reporter reporter) throws IOException {
  String line = value.toString();
  StringTokenizer itr = new StringTokenizer(line);
  while (itr.hasMoreTokens()) {
    word.set(itr.nextToken());
    if(word.toString().startsWith("c"){
    output.collect(word, one);
   }
  }
 } 
}


public static class Reduce extends MapReduceBase
implements Reducer<Text, IntWritable, Text, IntWritable> {

public void reduce(Text key, Iterator<IntWritable> values,
                   OutputCollector<Text, IntWritable> output,
                   Reporter reporter) throws IOException {
  int sum = 0;
  while (values.hasNext()) {
    sum += values.next().get(); //gets the sum of the words and add them together
  }
  output.collect(key, new IntWritable(sum)); //outputs the word and the number
  }
 }

问题答案:

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

如果您要输出单词作为关键字,则只会帮助您计算以“ 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));
    }
}


 类似资料:
  • 我被分配了一个任务,从数组列表中返回以特定字母开头的单词,但是我选择了字母c;我被告知我可以用另一种方式做到这一点,除了专门返回每个以字母开头的元素和许多println之外,我不知道除了使用ArrayList startswith方法之外,还有什么其他方法可以做到这一点,有人愿意启发我吗? }

  • 我想得到给定字符串中一个单词以给定字母开头的计数次数。 例如,在这个短语中:“这个模式很好,但猪喜欢牛奶”,如果我想找到以“g”开头的单词数,只有1个“很棒”,但现在我得到了2个“很棒”和“猪”。 这是我使用的代码:

  • 我在大学学习Java,我需要写一个静态int countCapitals(String s)方法,返回字符串s中有多少个单词以大写字母开头。 大写字母是UPPERCASE_LETTER类型或TITLECASE_LETTER类型的字符(Character)。单词是由一个或多个空格、字符、符号或标点符号分隔的字母或数字序列。 New.countCapitals("亲爱的朋友们,你们好!这里—以大写字母

  • 当我输入注册号时,当它以后一个B开头时,它应该返回值true,反之亦然,但在这个编码中,当我输入bhu132时,它将值变为false。这里怎么了..请帮帮我

  • 我指的是学习 C 的 K 和 R 书;它是关于在字数统计程序中使用 EOF 的 while 循环,书中给出的程序运行良好,但我想知道它如何在一次输入后停止接受输入并给出带有行、单词、 请帮助我理解这个程序中到底发生了什么来打破循环。 附加代码和输出 -

  • 问题 你想把字符串中每个单词的首字母转换为大写形式。 解决方案 使用“拆分-映射-拼接”模式:先把字符串拆分成单词,然后通过映射来大写单词第一个字母小写其他字母,最后再将转换后的单词拼接成字符串。 ("foo bar baz".split(' ').map (word) -> word[0].toUpperCase() + word[1..-1].toLowerCase()).join ' ' #