gpt4 book ai didi

hadoop - Hadoop 中的映射器输入键值对

转载 作者:可可西里 更新时间:2023-11-01 14:10:56 26 4
gpt4 key购买 nike

通常,我们以以下形式编写映射器:

public static class Map extends Mapper<**LongWritable**, Text, Text, IntWritable>

这里映射器的输入键值对是<LongWritable, Text> - 据我所知,当映射器逐行获取输入数据时 - 因此映射器的键表示行号 - 如果我错了请纠正我。

我的问题是:如果我将映射器的输入键值对指定为 <Text, Text>然后它给出了错误

 java.lang.ClassCastException: org.apache.hadoop.io.LongWritable cannot be cast to org.apache.hadoop.io.Text

是否必须将映射器的输入键值对指定为<LongWritable, Text>? - 如果是那么为什么?如果不是那么错误的原因是什么?你能帮我理解错误的正确原因吗?

提前致谢。

最佳答案

映射器的输入取决于使用的 InputFormat。 InputFormat 负责读取传入数据并将其整形为 Mapper 期望的任何格式。默认的 InputFormat 为 TextInputFormat。 , 它扩展了 FileInputFormat<LongWritable, Text> .

如果您不更改 InputFormat,则使用具有与 <LongWritable, Text> 不同的键值类型签名的映射器会导致这个错误。如果你期待 <Text, Text>输入,您必须选择合适的输入格式。您可以在作业设置中设置 InputFormat:

job.setInputFormatClass(MyInputFormat.class);

正如我所说,默认情况下它设置为 TextInputFormat。

现在,假设您的输入数据是一堆以逗号分隔的换行符分隔的记录:

  • “A,值 1”
  • “B,value2”

如果您希望映射器的输入键为 ("A", "value1"), ("B", "value2") 您将必须使用 <Text, Text> 实现自定义 InputFormat 和 RecordReader签名。 幸运的是,这很容易。有 an example here可能还有一些关于 StackOverflow 的示例。

简而言之,添加一个扩展 FileInputFormat<Text, Text> 的类和一个扩展 RecordReader<Text, Text> 的类.覆盖 FileInputFormat#getRecordReader方法,并让它返回您的自定义 RecordReader 的实例。

然后您将必须实现所需的 RecordReader 逻辑。最简单的方法是创建一个 LineRecordReader 的实例。在您的自定义 RecordReader 中,并将所有基本职责委托(delegate)给此实例。在 getCurrentKey 和 getCurrentValue 方法中,您将通过调用 LineRecordReader#getCurrentValue 实现提取逗号分隔文本内容的逻辑。并以逗号分隔。

最后,将您的新 InputFormat 设置为 Job InputFormat,如上面第二段之后所示。

关于hadoop - Hadoop 中的映射器输入键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19624607/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com