How to customize Writable class in Hadoop?

前端 未结 2 2029
醉话见心
醉话见心 2021-02-07 13:15

I\'m trying to implement Writable class, but i have no idea on how to implement a writable class if in my class there is nested object, such as list, etc. Could any body help me

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-07 13:34

    Serializing collections is quite simple.

    @Override
    public void readFields(DataInput in) throws IOException {
        int size = in.readInt();
        list= new ArrayList(size);
        for(int i = 0; i < size; i++){
            Field f = new Field();
            f.readFields(in);
            list.add(f);
        }
    }
    
    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(list.size());
        for (Field l : list) {
            l.write(out);
        }
    }
    

    Field has to implement Writable as well.

提交回复
热议问题