使用旧Rust 2015

0.0.7 2016年4月26日

#181 in #change

Apache 许可证

1MB
26K SLoC

JSONCDC

Join the chat at https://gitter.im/posix4e/jsoncdc.

#jsoncdc on freenode irc

JSONCDC为Postgres提供变更数据捕获,将Postgres预写日志转换为JSON。

它用Rust编写,由于代码短小,是其他想要使用Rust编写Postgres扩展的插件作者的好样板项目。

我们的库需要rust stable 1.1或更高版本。

待处理任务可以在: HuBoard徽章

Linux Status

版权(c)2016 Alex Newman, Jason Dusek

JSONCDC可在多个许可证下使用

  • 与Postgres本身相同的许可证(licenses/postgres),

  • Apache 2.0许可证(licenses/apache)。

状态

JSONCDC目前可以通过pgxn从不稳定频道安装:pgxn install jsoncdc --unstable

用法

基本演示

SELECT * FROM pg_create_logical_replication_slot('jsoncdc', 'jsoncdc');
--- Wait for some transactions, and then:
SELECT * FROM pg_logical_slot_get_changes('jsoncdc', NULL, NULL);

jsoncdc的输出格式非常规范,由JSON对象组成,包括begintableinsertupdatedelete子句,每行一个

{ "begin": <xid> }
{ "table": <name of table>, "schema": <column names and type> }
...inserts, updates and deletes for this table...
{ "table": <name of next table>, "schema": <column names and type> }
...inserts, updates and deletes for next table...
{ "commit": <xid>, "t": <timestamp with timezone> }

通过pg_recvlogical和一点shell脚本,你可以利用这种非常规范的格式将每个事务批量到一个单独的文件中

pg_recvlogical -S jsoncdc -d postgres:/// --start -f - |
while read -r line
do
  case "$line" in
    '{ "begin": '*)                # Close and reopen FD 9 for each new XID
      fields=( $line )
      xid="${fields[2]}"
      exec 9>&-
      exec 9> "txn-${xid}.json" ;;
  esac
  printf '%s\n' "$line" >&9       # Use printf because echo is non-portable
done

格式

  • JSON输出

特性

  • 由Rust度量监控

无运行时依赖