当前位置: 首页 > 知识库问答 >
问题:

Hadoop单词计数:接收以字母C开头的单词总数

东云
2023-03-14

以下是Hadoop word count Java地图和reduce源代码:

在map函数中,我可以输出所有以字母“c”开头的单词,以及该单词出现的总次数,但我要做的只是输出以字母“c”开头的单词的总次数,但我在获取总次数时遇到了一些困难。如果您能提供任何帮助,我将不胜感激,谢谢。

示例

我得到的结果是:

可2

can 3

第5类

我想要的是:

C-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
  }
 }

共有3个答案

阮健
2023-03-14

映射器的更简单代码:

public void map(LongWritable key, Text value,OutputCollector<Text,IntWritable> op, Reporter r)throws IOException
{
    String s = value.toString();
      for (String w : s.split("\\W+"))
       {
       if (w.length()>0)
        {
         if(w.startsWith("C")){
         op.collect(new Text("C-Count"), new IntWritable(1));        
         }
       }
  }
}
江佐
2023-03-14

而不是

output.collect(word, one);

在映射器中,尝试:

output.collect("c-total", one);
广瑞
2023-03-14

克里斯·格肯的回答是对的。

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

不是“C”的全部总数。

因此,您需要从Mapper输出一个唯一的键。

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

        }

下面是一个使用新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));
    }
}
 类似资料:
  • 问题内容: 这是Hadoop字数统计Java映射并减少源代码: 在map函数中,我已经到了可以输出所有以字母“ c”开头的单词以及该单词出现的总次数的位置,但是我想做的就是输出总数以字母“ c”开头的单词,但我在获取总数上有些停留。任何帮助将不胜感激,谢谢。 例 我得到的输出: 可以2 罐3 猫5 我想要得到的是: 合计10 问题答案: 克里斯·格肯 的答案是正确的。 如果您要输出单词作为关键字,

  • 我被分配了一个任务,从数组列表中返回以特定字母开头的单词,但是我选择了字母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 ' ' #

  • 我在大学上Java入门课程。我的作业是写一个程序来显示一个句子中1个字母单词的数量,一个句子中2个字母单词的数量...等等。句子是用户输入的。我应该使用一个循环,但不允许使用数组。 然而,现在只是开始,我只是想找出句子第一个单词的字母数。我得到的结果要么是字母数不正确,要么是字符串索引超出范围。 例如,当我输入“这是一个句子”时,它会给我“字符串索引超出范围:4”对此的任何帮助都将不胜感激。