wal2json
2. 安装
| 源码安装环境为 Ubuntu 24.04(x86_64),环境中已经安装了IvorySQL5及以上版本,安装路径为/usr/ivory-5 |
2.1. 源码安装
# 从 https://github.com/eulerto/wal2json/releases/tag/wal2json_2_6 下载 2.6 的源码包 wal2json_2_6.zip unzip wal2json_2_6.zip cd wal2json_2_6 # 编译安装插件 make PG_CONFIG=/usr/ivory-5/bin/pg_config make PG_CONFIG=/usr/ivory-5/bin/pg_config install
| 如果出现找不到xlocale.h的错误,需要手动修改 /usr/ivory-5/include/postgresql/server/pg_config.h 删除或者注释掉 #define HAVE_XLOCALE_H 1 这一行 |
3. 使用
在第一个终端中执行命令:
sudo -u ivorysql /usr/ivory-5/bin/bin/pg_recvlogical -d postgres --slot wal2json_slot --create-slot -P wal2json 启动监听,实时输出变更的JSON格式 sudo -u ivorysql /usr/ivory-5/bin/bin/pg_recvlogical -d postgres --slot wal2json_slot --start -o pretty-print=1 -f -
在第二个终端中连接数据库:
/usr/ivory-5/bin/psql -d postgres -p 1521
执行下面的SQL语句:
CREATE TABLE test_cdc (id int primary key, name varchar(50)); INSERT INTO test_cdc VALUES (1, 'test1'); UPDATE test_cdc SET name = 'test1_update' WHERE id = 1; DELETE FROM test_cdc WHERE id = 1; DROP TABLE test_cdc;
此时在第一个终端上可以看到下面的输出:
{
"change": [
]
}
{
"change": [
{
"kind": "insert",
"schema": "public",
"table": "test_cdc",
"columnnames": ["id", "name"],
"columntypes": ["integer", "sys.oravarcharbyte(50)"],
"columnvalues": [1, "test1"]
}
]
}
{
"change": [
{
"kind": "update",
"schema": "public",
"table": "test_cdc",
"columnnames": ["id", "name"],
"columntypes": ["integer", "sys.oravarcharbyte(50)"],
"columnvalues": [1, "test1_update"],
"oldkeys": {
"keynames": ["id"],
"keytypes": ["integer"],
"keyvalues": [1]
}
}
]
}
{
"change": [
{
"kind": "delete",
"schema": "public",
"table": "test_cdc",
"oldkeys": {
"keynames": ["id"],
"keytypes": ["integer"],
"keyvalues": [1]
}
}
]
}
{
"change": [
]
}