如何从存储在FusionAuth数据库中的blob(一个ID)中获取字符串?

huangapple go评论68阅读模式
英文:

How can I get string from the blob (an id) stored in fusionauth DB?

问题

为了分析目的,我想针对Fusionauth数据库运行查询。但是id字段存储为二进制Blob。你是否有一个可以将此Blob转换为字符串的函数(Java或SQL)可供我使用?

英文:

For analytical purposes I want to run query against Fusionauth DB. But the id fields are stored as binary blobs. Do you have a function (java or sql) I can use to convert this blob into a string?

答案1

得分: 1

我假设你正在使用MySQL。FusionAuth使用UUID作为唯一标识,在MySQL中,我们将其存储为BINARY(16)

如果你想以人类可读的形式选择此值,你可以执行类似于SELECT HEX(id) FROM table_name的选择查询。

如果你想选择此列并将其反序列化为Java的UUID类型,你可以使用类似以下代码的方法:

public UUID fromByteArray(byte[] ba) {
  long msb = 0;
  long lsb = 0;
  for (int i = 0; i < 8; i++) {
    msb = (msb << 8) | (ba[i] & 0xff);
  }
  for (int i = 8; i < 16; i++) {
    lsb = (lsb << 8) | (ba[i] & 0xff);
  }
  return new UUID(msb, lsb);
}
英文:

I'm assuming you are using MySQL. FusionAuth uses UUIDs for unique Ids, and in MySQL we store these as BINARY(16).

If you want to select this value in a human readable form, you can perform a select such as SELECT HEX(id) FROM table_name.

If you want to select this column and deserialize it into a Java UUID type, you can use code similar to the following:

public UUID fromByteArray(byte[] ba) {
  long msb = 0;
  long lsb = 0;
  for (int i = 0; i &lt; 8; i++) {
    msb = (msb &lt;&lt; 8) | (ba[i] &amp; 0xff);
  }
  for (int i = 8; i &lt; 16; i++) {
    lsb = (lsb &lt;&lt; 8) | (ba[i] &amp; 0xff);
  }
  return new UUID(msb, lsb);
}

huangapple
  • 本文由 发表于 2020年10月8日 07:01:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64253472.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定