gpt4 book ai didi

hadoop - 读取包装在 Hadoop ArrayWritable 中的值

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

我是 Hadoop 和 Java 的新手。我的映射器输出文本和 Arraywritable。我无法读取 ArrayWritable 值。 Unbale 将 .get() 值转换为整数。附上映射器和缩减器代码。有人可以帮我更正我的 reducer 代码以读取 ArrayWritable 值吗?

public static class Temp2Mapper extends Mapper<LongWritable, Text, Text, ArrayWritable>{
private static final int MISSING=9999;

@Override public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{
String line = value.toString();
String date = line.substring(07,14);
int maxTemp,minTemp,avgTemp;

IntArrayWritable carrier = new IntArrayWritable();
IntWritable innercarrier[] = new IntWritable[3];
maxTemp=Integer.parseInt(line.substring(39,45));
minTemp=Integer.parseInt(line.substring(47,53));
avgTemp=Integer.parseInt(line.substring(63,69));
if (maxTemp!= MISSING)
innercarrier[0]=new IntWritable(maxTemp); // maximum Temperature
if (minTemp!= MISSING)
innercarrier[1]=new IntWritable(minTemp); //minimum temperature
if (avgTemp!= MISSING)
innercarrier[2]=new IntWritable(avgTemp); // average temperature of 24 hours

carrier.set(innercarrier);
context.write(new Text(date), carrier); // Output Text and ArrayWritable
}
}

public static class Temp2Reducer
extends Reducer<Text, ArrayWritable, Text, IntWritable>{
@Override public void reduce(Text key, Iterable<ArrayWritable> values, Context context )
throws IOException, InterruptedException {

int max = Integer.MIN_VALUE;
int[] arr= new int[3];

for (ArrayWritable val : values) {
arr = (Int) val.get(); // Error: cannot cast Writable to int
max = Math.max(max, arr[0]);
}

context.write( key, new IntWritable(max) );
}

}

最佳答案

ArrayWritable#get方法返回一个 Writable 数组。

您不能将 Writable 的数组转换为 int。你可以做的是:

  1. 遍历这个数组
  2. 将数组的每个项目(类型为Writable)转换为IntWritable
  3. 使用IntWritable#get获取 int 值的方法。
for (ArrayWritable val: values) {
for (Writable writable: val.get()) { // iterate
IntWritable intWritable = (IntWritable)writable; // cast
int value = intWritable.get(); // get
// do your thing with int value
}
}

关于hadoop - 读取包装在 Hadoop ArrayWritable 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21106146/

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