为什么LongWritable(key)没有在Mapper类中使用?

制图员:

Mapper类是generics类型,有四个forms类型参数,用于指定map函数的输入键,输入值,输出键和输出值类型

public class MaxTemperatureMapper extends Mapper { private static final int MISSING = 9999; @Override public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); String year = line.substring(15, 19); int airTemperature; if (line.charAt(87) == '+') { // parseInt doesn't like leading plus signs airTemperature = Integer.parseInt(line.substring(88, 92)); } else { airTemperature = Integer.parseInt(line.substring(87, 92)); } String quality = line.substring(92, 93); if (airTemperature != MISSING && quality.matches("[01459]")) { context.write(new Text(year), new IntWritable(airTemperature)); } } 

减速器:

四个正式类型参数用于指定输入和输出类型,这次是reduce函数。 reduce函数的输入类型必须与map函数的输出类型匹配:Text和IntWritable

 public class MaxTemperatureReducer extends Reducer { @Override public void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException { int maxValue = Integer.MIN_VALUE; for (IntWritable value : values) { maxValue = Math.max(maxValue, value.get()); } context.write(key, new IntWritable(maxValue)); } } 

但在这个例子中,密钥从未使用过。

Mapper中key的用途是什么,还没有被使用过?

为什么密钥是LongWritable?

此示例中使用的输入格式是TextInputFormat ,它将键/值对生成为LongWritable/Text

这里的LongWritable键表示从给定输入文件的Input Split读取的当前行的偏移位置。 Text表示实际当前行本身。

我们不能说文件中每一行的LongWritable键给出的行偏移值没有用。 这取决于用例,根据您的情况,此输入键不重要。

我们有多种类型的InputFormat类型,而不是TextInputFormat ,它以不同的方式解析输入文件中的行并生成相关的键/值对。

例如, KeyValueTextInputFormat是TextInputFormat的子类,它使用配置delimiter解析每一行,并将键/值生成为Text/Text

编辑: – 在下面找到几个输入格式和键/值类型的列表,

 KeyValueTextInputFormat Text/Text NLineInputFormat LongWritable/Text FixedLengthInputFormat LongWritable/BytesWritable 

除了我们有几个输入格式,它们在声明时采用基于generics的自定义键/值类型。 如SequenceFileInputFormat, CombineFileInputFormat 。 请查看Hadoop权威指南中的输入格式章节。

希望这可以帮助。

如果您没有设置, JobConf类将返回LongWritable作为默认类

 job.setMapOutputValueClass(...) 

JobConf代码中: –

 public Class getOutputKeyClass() { return getClass(JobContext.OUTPUT_KEY_CLASS, LongWritable.class, Object.class); }