可写

kuhbmx9i  于 2021-06-04  发布在  Hadoop
关注(0)|答案(1)|浏览(458)

这是实现可写。。

public class Test implements Writable {
    List<AtomicWritable> atoms = new ArrayList<AtomicWritable>();

    public void write(DataOutput out) throws IOException {
        IntWritable size = new IntWritable(atoms.size());
        size.write(out);
        for (AtomicWritable atom : atoms)
            atom.write(out);
    }

    public void readFields(DataInput in) throws IOException {
        atoms.clear();
        IntWritable size = new IntWritable();
        size.readFields(in);
        int n = size.get();
        while(n-- > 0) {
            AtomicWritable atom = new AtomicWritable();
            atom.readFields(in);
            atoms.add(atom);
        }
    }
}

如果有人能帮助我理解如何调用write和readfields方法,我将不胜感激。在这种情况下,我基本上无法理解如何构造测试对象。一旦对象被写入dataoutput对象,我们如何在datainput对象中恢复它。这听起来很傻,但我是hadoop的新手,并且被分配了一个使用hadoop的项目。请帮忙。
谢谢!!!

jhiyze9q

jhiyze9q1#

基本上我不明白如何构造 Test 对象。
是的,你没抓住重点。如果需要构造 Test 和填充 atoms ,则需要向 Test :

public Test(ArrayList<AtomicWritable> atoms) {
     this.atoms = atoms;
}

或者,您需要使用默认构造函数并添加一个方法或setter,使您可以将项添加到 atoms 或者设置 atoms . 后者在hadoop框架中非常常见,有一个默认构造函数和一个 set 方法。参见,例如。, Text.set .
你不打电话 readFields 以及 write ; 当hadoop框架需要对输入和输出进行序列化和反序列化时,它会为您做到这一点 map 以及 reduce .

相关问题