java - Hadoop Mapper参数说明

标签 java hadoop mapreduce cluster-computing distributed-computing

<分区>

我是 Hadoop 的新手,对 Mapper 感到困惑参数。

以众所周知的WordCount为例:

class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
  private Text outputKey;
  private IntWritable outputVal;

  @Override
  public void setup(Context context) {
    outputKey = new Text();
    outputVal = new IntWritable(1);
  }

  @Override
  public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
    StringTokenizer stk = new StringTokenizer(value.toString());
    while(stk.hasMoreTokens()) {
      outputKey.set(stk.nextToken());
      context.write(outputKey, outputVal);
    }
  }
}

查看map函数,参数为Object keyText valueContext上下文,我是对 Object key 的样子感到困惑(你看,key 从未在 Map 函数中使用过)。

因为输入文件格式是这样的:

Deer
Beer
Bear
Beer
Deer
Deer
Bear
...

我知道 value 看起来像每一行 DeerBeer 等等。它们是逐行处理的。

但是 key 看起来怎么样?如何决定 key 应使用哪种数据类型?

最佳答案

这里的一切都取决于您使用的 InputFormat 类。它解析输入数据源并为您提供 (Key, Value) 对。即使具有相同的输入源,不同的输入格式实现也可以为您提供不同的流。

这是演示方法的文章:

https://hadoopi.wordpress.com/2013/05/31/custom-recordreader-processing-string-pattern-delimited-records/

此处的主要驱动程序是 RecordReader

关于java - Hadoop Mapper参数说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29363735/

相关文章:

java - 在 iText pdf 处理过程中识别下划线和删除线属性

java - 直接分配或通过分配进行控制

hadoop - 指定并发的Hadoop作业中的映射槽数

hadoop - 确定 Hive "order by"子句中的 reducer 数量

sorting - MapReduce 框架如何实现排序阶段?

java - 如何在java中添加自己的数字范围

java - 合适的数据结构

security - 在所有节点上使用相同主体的Hadoop安全性

javascript - CouchDB View 使用来自两个单独文档的嵌入式数组组成 JSON 对象

java - 在自定义 java 类中反序列化 MapWritable