hbase的java序列化字节[]

f87krz0w  于 2021-06-09  发布在  Hbase
关注(0)|答案(0)|浏览(191)

我想储存一个 UUID 在hbase中使用java。
今天,我把它写成一本书 String 36个字符,但占用了太多的空间。我需要优化。我想写一个原始的 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 {}
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题