英文:
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 < 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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论