2014-01-14 75 views
2

我是Hadoop和Java的新手。我的映射器輸出文本和Arraywritable。我無法讀取ArrayWritable值。 Unbale將.get()值轉換爲整數。映射器和減速器代碼已附加。有人可以幫我糾正我的reducer代碼,以便讀取ArrayWritable值嗎?閱讀包裝在Hadoop 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)); 
     } 

} 

回答

4

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