I want to store an UUID
in Hbase using Java.
Today, I write it as a String
of 36 characters, but it takes too much space. I need to optimize. I want to write a raw byte[16]
, but it is not Serializable
.
Is there any easy way to do that?
Am I doing something terribly wrong? I feel there is something I do not understand well enough. :(
Thanks
Here is my approach, which do not work because 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 {}
}