2

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 {}
}
Costin
  • 2,699
  • 5
  • 25
  • 43
  • Is "Longs" from Guava ? Also, is "Type '...UserId' does not have a serializer" the error message you get on your ide with that class ? Where did you get that Byte.concat method ? – Asoub Nov 24 '15 at 10:25
  • Yes, both `Longs` and `Bytes` are from Guava: `import com.google.common.primitives.Bytes; import com.google.common.primitives.Longs;` – Costin Nov 24 '15 at 10:51
  • The `"Type '...UserId' does not have a serializer"` error message is from a custom Exception. It was implemented a while ago by somebody else. – Costin Nov 24 '15 at 10:53
  • You should give the full stack trace of the error if it comes from custom libraries. Maybe you need some kind of special "Serializer" class, bound to this one, in order to serialize it ? Also, you should try to use two longs "msl" and "lsl" (most significant and less significant "long") instead of byte array. Like in one of your constructor. I don't know how the serialization process will know how many bytes it must write, and how many it must read. Also, be carefull in which order you put read and write for the longs (msl first or lsl first). – Asoub Nov 24 '15 at 13:00
  • Finally I can get the data in Hbase, but I can't read back the date `20151124`. The `byte[]` in key corresponds to the `value`. 4w\x00 **;\xA1\x5C\x1A\x914J\xB4\x92\xC04=\xBB\xA8\x93\xA1** 4 **20151124** \x00 column=i:country, timestamp=1448362136391, value="**3ba15c1a-9134-4ab4-92c0-343dbba893a1**" – Costin Nov 24 '15 at 13:41
  • I don't really understand... which byte contains the value ? Isn"t the timestamp enough ? – Asoub Nov 24 '15 at 15:03
  • Real data looks like this: ROW `4w\x00\xA1\x5C\x1A\x914J\xB4\x92\xC04=\xBB\xA8\x93\xA1420151124\x00` -- COLUMN+CELL `column=i:country, timestamp=1448362136391, value="FR"` ; I have replaced value in order to validate the `byte[]` part from the ROW (key). – Costin Nov 24 '15 at 16:21
  • @Asoub, thanks for your help, but for the time being I've fixed the problem without using `UserId`. But I still do not know how to fix it using `UserId`. I will be back in the next days when I will better understand the problem. Thanks. – Costin Nov 24 '15 at 16:28

0 Answers0