Java为hbase序列化byte []

时间:2015-11-24 09:20:58

标签: java serialization hbase bytearray

我想使用Java在Hbase中存储UUID

今天,我把它写成36个字符的String,但它需要太多空间。我需要优化。我想写一个原始的byte[16],但它不是Serializable

有没有简单的方式来做到这一点?

我做错了什么吗?我觉得有些事情我不太了解。 :(

由于

这是我的方法,因为Type '...UserId' does not have a serializer

而无效
public class UserId implements Serializable {    
  private static final long serialVersionUID = 1L;

  private byte[] uuid;

  public UserId() {      }

  public UserId(byte[] uuid) { this.uuid = uuid; }

  public UserId(String uuid) { this(UUID.fromString(uuid)); }

  public UserId(UUID uuid) {
    this(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits());
  }

  public UserId(long msb, long lsb) {
    this.uuid = Bytes.concat(Longs.toByteArray(msb), Longs.toByteArray(lsb));
  }

  public byte[] getUuid() {
    return uuid;
  }

  public void setUuid(byte[] uuid) {
    this.uuid = uuid;
  }

  @Override
  public String toString() {
    ByteBuffer bb = ByteBuffer.wrap(uuid);
    return new UUID(bb.getLong(), bb.getLong()).toString();
  }


  private void writeObject(ObjectOutputStream out) throws IOException {
    out.write(uuid);
  }

  private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.readFully(uuid);
  }

  private void readObjectNoData() throws ObjectStreamException {}
}

0 个答案:

没有答案