Read values wrapped in Hadoop ArrayWritable

时光毁灭记忆、已成空白 提交于 2020-01-13 18:14:08

问题


I am new to Hadoop and Java. My mapper outputs text and Arraywritable. I having trouble to read ArrayWritable values. Unbale to cast .get() values to integer. Mapper and reducer code are attached. Can someone please help me to correct my reducer code in order to read ArrayWritable values?

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) );
        }

}

回答1:


ArrayWritable#get method returns an array of Writable.

You can't cast an array of Writable to int. What you can do is:

  1. iterate over this array
  2. cast each item (which will be of type Writable) of the array to IntWritable
  3. use IntWritable#get method to get the int value.
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
  }
}


来源:https://stackoverflow.com/questions/21106146/read-values-wrapped-in-hadoop-arraywritable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!