Skip to content

Commit

Permalink
Documenting binary encoding used with Canal-JSON (#14774)
Browse files Browse the repository at this point in the history
  • Loading branch information
qiancai authored Oct 10, 2023
1 parent c1bfddb commit dda4ee6
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions ticdc/ticdc-canal-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,45 @@ TiCDC 涉及的 Java SQL Type 及其 Code 映射关系如下表所示。

想要了解 Java SQL Type 的更多信息,请参考 [Java SQL Class Types](https://docs.oracle.com/javase/8/docs/api/java/sql/Types.html)

## Binary 和 Blob 类型

TiCDC 在编码[二进制类型](/data-type-string.md#binary-类型)的数据为 Canal-JSON 格式时,会按照以下规则将每个字节转换为其字符表示形式:

- 可打印字符:使用 ISO/IEC 8859-1 字符编码表示。
- 不可打印字符和某些在 HTML 中具有特殊含义的字符:使用其 UTF-8 转义序列表示。

下表列出了具体的表示信息:

| 字符类型 | 值范围 | 字符表示形式 |
|:---------------------|:-----------|:------------------------------------|
| 控制字符 | `[0, 31]` | UTF-8 转义(如 `\u0000``\u001F`|
| 水平制表符 | `[9]` | `\t` |
| 换行符 | `[10]` | `\n` |
| 回车符 | `[13]` | `\r` |
| 可打印字符 | `[32, 127]` | 字符本身(如 `A`|
| `&` 符号 | `[38]` | `\u0026` |
| `<`| `[60]` | `\u0038` |
| `>`| `[62]` | `\u003E` |
| 扩展控制字符 | `[128, 159]` | 字符本身 |
| ISO 8859-1 (Latin-1) 字符 | `[160, 255]` | 字符本身 |

### 编码示例

例如,对于存储在 `VARBINARY` 类型的 `c_varbinary` 列中的 16 个字节 `[5 7 10 15 36 50 43 99 120 60 38 255 254 45 55 70]`,其在 Canal-JSON 的 `Update` 事件中的编码如下:

```json
{
...
"data": [
{
...
"c_varbinary": "\u0005\u0007\n\u000f$2+cx\u003c\u0026ÿþ-7F"
}
]
...
}
```

## TiCDC Canal-JSON 和 Canal 官方实现对比

TiCDC 对 Canal-JSON 数据格式的实现,包括 `Update` 类型事件和 `mysqlType` 字段,和官方有些许不同。主要差异见下表。
Expand Down

0 comments on commit dda4ee6

Please sign in to comment.