HADOOP序列化应该注意的问题

hadoop在序列化的时候,如果被序列化的BEAN里面含有其他对象如list等,一定要在readFields方法里面new出这个新对象。要不然由于序列化里数据重用的原因,会导致list被循环利用,从而使多个BEAN对象的list里面的元素叠加到一个BEAN里。

例如:

public class UserPortrait2RedisBean
  implements Writable
{
  private String deviceId;
  private String model;
  private List<String> tags;

  public UserPortrait2RedisBean()
  {
    this.tags = new ArrayList();
  }


  public void readFields(DataInput in)
    throws IOException
  {
    this.deviceId = in.readUTF();
    this.model = in.readUTF();
    int len = in.readInt();

    this.tags = new ArrayList();
    for (int i = 0; i < len; i++)
      this.tags.add(in.readUTF());
  }

  public void write(DataOutput out)
    throws IOException
  {
    out.writeUTF(this.deviceId);
    out.writeUTF(this.model);
    out.writeInt(this.tags.size());
    for (String tag : this.tags)
      out.writeUTF(tag);
  }
}

猜你喜欢

转载自zhangxiong0301.iteye.com/blog/2142985