2014-12-24 50 views
0

我想存儲 迭代器值 這是的參數減少方法即貯藏迭代<Pojo>減速方法的目的是陣列

public void reduce(IntWritable key, Iterator<Pojo> values, 
     OutputCollector<IntWritable, SubArrayWritable> output, Reporter reporter) 

在定製ArrayWritable類對象即PojoArrayWritable對象。 我已經創建PojoArrayWritable類和代碼是

import java.io.DataInput; 
import java.io.DataOutput; 
import java.io.IOException; 
import org.apache.hadoop.io.ArrayWritable; 
import org.apache.hadoop.io.DoubleWritable; 
import org.apache.hadoop.io.IntWritable; 
import org.apache.hadoop.io.LongWritable; 
import org.apache.hadoop.io.Writable; 
import org.apache.hadoop.io.WritableComparable; 

public class SubArrayWritable extends ArrayWritable 
{ 
public SubArrayWritable() { 
    super(Sub.class); 
} 
public SubArrayWritable(Sub[] values) { 
    super(Sub.class, values); 
} 
} 

而且Sub.class具有以下代碼 -

import java.io.DataInput; 
import java.io.DataOutput; 
import java.io.IOException; 
import org.apache.hadoop.io.DoubleWritable; 
import org.apache.hadoop.io.IntWritable; 
import org.apache.hadoop.io.LongWritable; 
import org.apache.hadoop.io.Writable; 

public class Sub implements Writable { 
private IntWritable id = new IntWritable(); 
private LongWritable pno = new LongWritable(); 
private DoubleWritable sal = new DoubleWritable(); 

public IntWritable getid() { 
    return id; 
} 

public void setId(IntWritable id) { 
    this.id = id; 
} 

public LongWritable getPno() { 
    return pno; 
} 

public void setPno(LongWritable pno) { 
    this.pno = pno; 
} 

public DoubleWritable getSal() { 
    return sal; 
} 

public void setSal(DoubleWritable sal) { 
    this.sal = sal; 
} 

@Override 
public void readFields(DataInput in) throws IOException { 
    id.readFields(in); 
    pno.readFields(in); 
    sal.readFields(in); 

} 

@Override 
public void write(DataOutput out) throws IOException { 
    id.write(out); 
    pno.write(out); 
    sal.write(out); 
} 


} 

而在減速類

public void reduce(IntWritable key, Iterator<Pojo> values, 
     OutputCollector<IntWritable, SubArrayWritable> output, Reporter reporter) 

我想將Iterator的值對象存儲到數組中,即將其存儲到SubArrayWritable的對象中,那麼我該如何做,因爲我不知道Iterator的值對象的大小/長度,那麼將會是什麼我將創建的SubArrayWritable對象的長度?

所以最後基本上我有Iterator<Pojo> values,我必須把它轉換爲array of Pojo

回答

0

我發現這個解決方案這一點 -

Pojo pojo=new Pojo(); 
    List<Pojo> list=new ArrayList<Pojo>(); 

    if (values.hasNext()) { 
     pojo = values.next(); 
     list.add(pojo); 
     } 

現在我可以包含迭代器的值用戶此POJO對象的值對象