gpt4 book ai didi

arrays - 将Reducer方法的Iterator 对象存储到数组

转载 作者:行者123 更新时间:2023-12-02 21:42:34 26 4
gpt4 key购买 nike

我要储存
迭代器值
这是Reduce方法的参数,即

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


}

而在Reducer类中
public void reduce(IntWritable key, Iterator<Pojo> values,
OutputCollector<IntWritable, SubArrayWritable> output, Reporter reporter)

我想将Iterator的values对象存储到数组中,即存储到SubArrayWritable的对象中,所以我该怎么做,因为我不知道Iterator的values对象的大小/长度,所以我将创建的SubArrayWritable对象的长度是多少? ??

所以最后基本上我有了 Iterator<Pojo> values,我必须将其转换为 array of Pojo

最佳答案

我为此找到了解决方案-

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

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

现在我可以使用包含迭代器对象值的pojo对象值

关于arrays - 将Reducer方法的Iterator <Pojo>对象存储到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27633993/

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