Coder Social home page Coder Social logo

zhaoshiling1017 / jsoncdc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from instructure/jsoncdc

0.0 3.0 0.0 657 KB

Takes a logical decoding stream from postgresql and outputs it in a pluggable and safe way

Dockerfile 1.75% Makefile 20.15% Rust 45.21% C 0.26% PLpgSQL 24.62% Shell 8.01%

jsoncdc's Introduction

JSONCDC

JSONCDC provides change data capture for Postgres, translating the Postgres write ahead log to JSON.

It is written in Rust and, being short, is a good skeleton project for other would be plugin authors who'd like to use Rust to write Postgres extensions.

Our library Requires rust stable 1.1 or greater.

Copyright and License

Copyright (c) 2016 Alex Newman, Jason Dusek Copyright (c) 2018 Instructure, Inc.

JSONCDC is available under multiple licenses:

  • the same license as Postgres itself (licenses/postgres),

  • the Apache 2.0 license (licenses/apache).

Status

JSONCDC is presently installable with pgxn, from the testing channel: pgxn install jsoncdc --testing.

Usage

A basic demo:

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);

The output format of jsoncdc is very regular, consisting of begin, table, insert, update, delete and message clauses as JSON objects, one per line:

{ "begin": <xid> }
{ "schema": <column names and type>, "table": <name of table> }
...inserts, updates and deletes for this table...
{ "schema": <column names and type>, "table": <name of next table> }
...inserts, updates and deletes for next table...
{ "prefix": <prefix>, "message": <message>, "transactional": <true|false> }
...messages may be mixed in at any point; they don't belong to a table...
{ "commit": <xid>, "t": <timestamp with timezone> }

With pg_recvlogical and a little shell, you can leverage this very regular formatting to get each transaction batched into a separate file:

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

jsoncdc's People

Contributors

solidsnack avatar posix4e avatar jmealo avatar addisonj avatar securityinsanity avatar allenap avatar gitter-badger avatar

Watchers

James Cloos avatar lenzhao avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.