我目前正在尝试通过 JDBC 优化 Postgres 中的数据加载。我们正在使用 COPY FROM STDIN 和 FORMAT 'binary' 现在构建二进制字节数组对于字符串、长整型、uuid 等来说非常简单。但是在一个实例中,我们表中有一个 JSONB 字段,我不知道如何序列化我的json 对象转换为二进制 jsonb 格式。jsonb 有任何规范吗?
注意:我已经排除了只发送 utf-8 二进制序列化 json 字符串的可能性。
小智 5
你只需要将 json 对象视为普通字符串,但在版本号前添加 1(字节),这是他们目前唯一支持的版本。还要确保指定字段的长度是“string.length”+ 1(版本号)
所以,基本上,如果 j 是你的 json 并且 dos 是输出流:
val utfBytes = j.toString.getBytes("UTF8")
dos.writeInt(utfBytes.length + 1)
dos.write(Array(1.toByte))
dos.write(utfBytes)
Run Code Online (Sandbox Code Playgroud)
这是来自postgres 源代码的注释(镜像到github):
/*
104 * jsonb type recv function
105 *
106 * The type is sent as text in binary mode, so this is almost the same
107 * as the input function, but it's prefixed with a version number so we
108 * can change the binary format sent in future if necessary. For now,
109 * only version 1 is supported.
110 */
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1525 次 |
| 最近记录: |