osheroff/mysql-binlog-connector-java

Resolving a UUID as original string from BINARY(16)

Closed this issue · 1 comments

MySQL version: 8.0.30
mysql-binlog-connector version: 0.27.5

I have a BINARY(16) column used to store a UUID (originating as a java.util.UUID)
I can successfully retrieve and view the string UUID value with:
SELECT BIN_TO_UUID(EXTERNAL_ID)

In working with the mysql-binlog-connector tool in de-serializing write events on the table in question, I noticed the column type for BINARY(16) is actually java.lang.String. Ultimately I want the human readable UUID string of that EXTERNAL_ID column.

I've made a number of attempts with no success thus far.
java.util.UUID.fromString and nameUUIDFromBytes are failing; the latter seems to at least generate the expected format of 32 chars and 4 dashes, but it's incorrect.

Hoping for some direction, guidance, or suggestion on how to go about resolving the BINARY/UUID column.
Thank you in advance.

I have a solution for this, posting here in case it might be helpful to anyone else.

The BinaryLogClient was not being set with an EventDeserializer configured with:
EventDeserializer.CompatibilityMode.CHAR_AND_BINARY_AS_BYTE_ARRAY

On de-serialization, previously resolved java.lang.String column types are now byte[]
Attempting to do UUID uidVal = java.util.UUID.fromString(valStr);, where valStr is a String created from the byte[] did not work. What did work involved the following:

byte[] bin = (byte[])obj;
UUID uidVal = convertBytesToUUID(bin);

private UUID convertBytesToUUID(byte[] bytes) {
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
long high = byteBuffer.getLong();
long low = byteBuffer.getLong();
return new UUID(high, low);
}