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

2023-12-20

这是 Hadoop 字数统计 java map 和 reduce 源代码:

在地图函数中,我已经可以输出所有以字母“c”开头的单词以及该单词出现的总次数,但我想做的只是输出总数以字母“c”开头的单词,但我在获取总数方面有点困难。任何帮助将不胜感激,谢谢。

Example

我得到的输出:

could 2

can 3

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

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

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

并非所有“c”的总数。

因此,您需要从映射器输出一个唯一的键。

 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));
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Hadoop字数统计:接收以字母“c”开头的单词总数 的相关文章

随机推荐

  • 通过快捷键激活扩展

    是否可以通过快捷键打开 启动 google chrome 扩展 例如 我想分配一个快捷方式 比如说 CTRL E打开我的扩展并启动它 以前有人这样做过吗 UPDATE
  • 让 clangd 了解编译器给出的宏

    我有两个从同一源 客户端和服务器 构建的可执行文件 并且它们是使用编译选项构建的 D CLIENT 0 D SERVER 1对于服务器和 D CLIENT 1 D SERVER 0为客户 如果我做类似的事情 if CLIENT Client
  • 如何设置 Paint.setTextSize() 的单位

    是否可以更改单位Paint setTextSize 据我所知 它是像素 但我喜欢在 DIP 中设置文本大小以支持多屏幕 我知道这个主题很旧并且已经得到解答 但我还想建议这段代码 int MY DIP VALUE 5 5dp int pixe
  • 如何在 Android 上实现选项卡之间的滑动?

    Android 4 0 中针对选项卡的关键设计建议之一是允许刷卡 http developer android com design patterns swipe views html between tabs在适当的情况下在它们之间进行
  • 为什么“echo strcmp('60', '100');”在php输出5中?

    PHP 关于这个函数的文档有点稀疏 我读过这个函数比较 ASCII 值 所以 echo strcmp hello hello outputs 0 as expected strings are equal echo hr echo strc
  • MPAndroidChart,如何删除小数百分比并且不显示低于10的百分比?

    我在用MPAndroid图表 https github com PhilJay MPAndroidChart我有两个问题 MPAndroid 饼图 去除小数百分比 饼图上不显示小于 10 的值 但显示切片 只是对于低于 10 的百分比不应显
  • PayPal CreateRecurringPaymentsProfile 账单频率

    我已经设法让我的网站与 paypal billing CreateRecurringPaymentsProfile 一起使用 但是我对以下字段感到困惑 计费周期和计费频率 如果我将第一个设置为每月 第二个设置为 12 希望它每月向我的客户收
  • 在 Area2D 中覆盖 KinematicBody2D 运动?

    I m trying to create a windy area within which the player would be pushed continuously to the left lt 到目前为止 这就是我想出的Windy
  • 将 Oracle 的时间戳转换为纪元中的秒数

    我在 Oracle 数据库的表中存储了一个时间戳 例如 01 03 12 16 13 33 000000000 我想将其转换为自 Unix Epoch 以来的秒数以在查询中返回 最简单的方法是什么 编辑 哦 我需要时间戳精度 不能依赖这里的
  • Swift:创建 UIImage 数组

    使用 Swift 我尝试为简单的动画创建 UIImage 对象数组 上下文帮助animationImages内容为 数组必须包含 UI Image 对象 我尝试按如下方式创建所述数组 但似乎无法获得正确的语法 var logoImages
  • 如何将 CSS 样式应用到元素?

    我是 CSS 新手 不是程序员 我了解什么是类 也了解什么是 div 但我似乎找不到的是如何在特定元素 例如我网站的 div 上设置样式 在你的 HTML 中 div class myClass Look at me div 在你的 CSS
  • 通过蓝牙将 OBDSim 连接到 Windows 上的 Torque

    我正在尝试在 Win7 上安装 OBDSim 但遇到了一些麻烦 我的最终目标是将 OBDSim 作为蓝牙 ELM327 OBDII 模拟器运行 并使用 Android 设备上的 Torque 应用程序连接到它 我在 youtube 上观看了
  • 如何调试从玉石打印对象

    如何调试从玉石打印对象 例如console log 在 JavaScript 中 您可以使用以下命令进行调试console log from jade像这样 div console log the object you want to lo
  • 如何在 Dart 中对末尾为空对象的 List 进行排序

    开始着手 Flutter 进行一个研究项目 我想知道如何对文件列表进行排序 事实上 我的程序有一个包含 4 个文件的列表 初始化如下 List
  • Python VLC - 获取位置轮询率解决方法

    我使用 Python VLC 在 pyqt 中构建自定义播放应用程序 我画了一个漂亮的自定义滑块来跟踪视频 但遇到了一些恼人的问题 无论我多久告诉我的滑块更新一次 它都会出现故障 每 1 4 秒左右跳跃一次 并且看起来不稳定 只是时间线 而
  • SVG textPath 上的希伯来语文本仅在 Safari 中反转

    将希伯来语文本放在 SVG textPath 上时 仅在 Safari 中会反转 macOS ventura 13 2 Safari 16 3 在 Firefox 和 Chrome 中都可以 不在路径上的文本总是可以的 此外 我可以使用 b
  • 防止使用 HTML 锚点播放 MP3 文件

    如何下载 MP3 链接而不是 在浏览器中 播放 我尝试将目标更改为空白 但这只是在新窗口中打开了播放器 您无法通过修改链接来做到这一点 您必须让提供该文件的 HTTP 服务器发送一个Content Type 应用程序 八位字节流 据推测 它
  • 将所有列中的值替换为该列的值

    如何用该列的编号替换所有列中的所有 1 我已经可以逐列执行此操作 输出 输出 3 1 3 3 输出 输出 4 1 4 4 输出 输出 5 1 5 5 etc 但我觉得自己像个傻瓜一样为每一个专栏写下这些内容 应该有一种方法可以同时完成所有这
  • 使用 IN 从 sqlite 数据库中删除

    我正在使用这样的记录 ID 从 sqlite 数据库中删除 这dirID是一个 ID 数组 Dim i As Integer 0 Dim conn As New SQLiteConnection Data Source DBPath Dim
  • Hadoop字数统计:接收以字母“c”开头的单词总数

    这是 Hadoop 字数统计 java map 和 reduce 源代码 在地图函数中 我已经可以输出所有以字母 c 开头的单词以及该单词出现的总次数 但我想做的只是输出总数以字母 c 开头的单词 但我在获取总数方面有点困难 任何帮助将不胜