srinu srinu -4 years ago 174
Scala Question

How can I convert binary UUID to UUID in scala?

I'm reading data from Hbase using spark and the UUID in Hbase is in binary format and I want to convert that binary type of UUID into regular UUID in scala. Anyone know how can it be done?

Answer Source

Below is the code for resolving the question.

def touuid(a: Array[Byte]): String = 
  val bb   = ByteBuffer.wrap(a)
  val high = bb.getLong();
  val low  = bb.getLong();
  val uuid = new UUID(high, low);
  return uuid.toString()

val havid = udf((x: Array[Byte]) => touuid(x))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download