我正在尝试从给定键(从映射器)中找到两个值之间的范围/差异。
我想做TMAX减去TMIN(TMAX-TMIN),但是我得到的结果是减去的,我不知道如何解决。
如何找出通过键形成的两个值之间的范围/差异。
样本数据集
站点日期(YYYYMMDD)元素温度flag1 flag2 othervalue
我只需要数据集中的测站,日期(键),元素和温度(值)
Mapper Output Example
20180102,TMAX,12
20180102,TMIN,3
What I Want to do
20180102,12 - 3
Reducer Current Output
20180102,-9
Reducer Desired Output
20180102,9
public static class MaxMinReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
public IntWritable result = new IntWritable();
public void reduce(Text key,Iterable<IntWritable> values,Context context) throws
IOException,InterruptedException {
int value1 = 0;
int value2 = 0;
int differenceResult = 0;
Iterator<IntWritable> iterator = values.iterator();
while (values.iterator().hasnext()) {
if(value1 != 0){
value2 = iterator.next().get();
break;
} else {
value1 = iterator.next().get();
}
differenceResult = value1 - value2;
}
differenceResult = value1 - value2;
result.set(differenceResult);
context.write(key,result);
}
}
java.lang.ArrayIndexOutOfBoundsException: 1
更新1:我已经尝试过这种方法,但是却收到错误消息: public static class MaxMinReducer
extends Reducer<Text,Text> {
public void reduce(Text key,Iterable<Text> values,Context context) throws IOException,InterruptedException {
String result = "";
int value1 = 0;
int value2 = 0;
int differenceResult = 0;
String finalVal = "";
for (Text val : values) {
finalVal = finalVal.concat(val.toString()).concat(",");
}
finalVal = finalVal.substring(0,finalVal.length()-1);
String[] diffValues = finalVal.split(",");
value1 = Integer.parseInt(diffValues[1]);
value2 = Integer.parseInt(diffValues[0]);
differenceResult = value1 - value2;
result = Integer.toString(differenceResult);
context.write(key,new Text(result));
}
}
..为什么?
{{1}}