How to customize Writable class in Hadoop?

前端 未结 2 2030
醉话见心
醉话见心 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<Field>(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.

    0 讨论(0)
  • 2021-02-07 13:41

    This tutorial explains better : http://www.hadoopmaterial.com/2013/10/custom-hadoop-writable-data-type.html

    0 讨论(0)
提交回复
热议问题