singer 是一个很不错的开源etl 解决方案,以下演示一个简单的数据从pg 同步到pg

很简单就是使用tap-postgres + target-postgres

环境准备

对于测试的环境的数据库使用docker-compose 运行

  • docker-compose 文件
version: "3"
services:
  tap:
    image: postgres:9.6.11
    ports:
    - "5433:5432"
    environment:
    - "POSTGRES_PASSWORD:dalong"
  target:
    image: postgres:9.6.11
    ports:
    - "5432:5432"
    environment:
    - "POSTGRES_PASSWORD:dalong"
 
  • tap 以及target 环境的配置

    singer 推荐的环境配置使用python venv 虚拟环境

tap 配置

mkdir tap-pg
cd tap-pg
python3 -m venv venv
source venv/bin/activate
pip install tap-postgre

target 配置

mkdir target-pg
cdtarget-pg
python3 -m venv venv
source venv/bin/activate
pip installtarget-postgres
  • 项目结构
-rw-r--r-- 1 dalong staff 251B 6 5 14:20 docker-compose.yaml
 -rw-r--r-- 1 dalong staff 145B 6 5 14:27 tap-pg.json
 -rw-r--r-- 1 dalong staff 143B 6 5 14:27 target-pg.json
  • 启动pg 数据库以及初始化测试数据
docker-compose up -d
 

导入测试数据: 注意连接 localhost 5433 端口pg 服务

CREATE TABLE userapps (
    id SERIAL PRIMARY KEY,
    username text,
    userappname text
);
INSERT INTO "public"."userapps"("id","username","userappname")
VALUES
(1,E'dalong',E'app'),
(2,E'first',E'login');

使用tap 以及target

  • 配置数据库连接
    tap: tap-pg.json
 
{
    "host": "localhost",
    "port": 5433,
    "dbname": "postgres",
    "user": "postgres",
    "password": "dalong",
    "schema": "public"
}

target: target 数据库配置

{
    "host": "localhost",
    "port": 5432,
    "dbname": "postgres",
    "user": "postgres",
    "password": "dalong",
    "schema": "copy"
}
 
 
  • tap 模式发现
    运行方式
 
./tap-pg/venv/bin/tap-postgres -c ta-pg.json -d > catalog.json
  • 选择需要同步的表以及同步方式
    以下为一个简单的demo,实际可以自己根据情况调整
 
{
  "streams": [
    {
      "table_name": "userapps",
      "stream": "userapps",
      "metadata": [
        {
          "breadcrumb": [],
          "metadata": {
            "table-key-properties": [
              "id"
            ],
+ "selected": true,
+ "replication-method": "FULL_TABLE",
            "schema-name": "public",
            "database-name": "postgres",
            "row-count": 0,
            "is-view": false
          }
        },
        {
          "breadcrumb": [
            "properties",
            "id"
          ],
          "metadata": {
            "sql-datatype": "integer",
            "inclusion": "automatic",
            "selected-by-default": true
          }
        },
        {
          "breadcrumb": [
            "properties",
            "username"
          ],
          "metadata": {
            "sql-datatype": "text",
            "inclusion": "available",
            "selected-by-default": true
          }
        },
        {
          "breadcrumb": [
            "properties",
            "userappname"
          ],
          "metadata": {
            "sql-datatype": "text",
            "inclusion": "available",
            "selected-by-default": true
          }
        }
      ],
      "tap_stream_id": "postgres-public-userapps",
      "schema": {
        "type": "object",
        "properties": {
          "id": {
            "type": [
              "integer"
            ],
            "minimum": -2147483648,
            "maximum": 2147483647
          },
          "username": {
            "type": [
              "null",
              "string"
            ]
          },
          "userappname": {
            "type": [
              "null",
              "string"
            ]
          }
        },
        "definitions": {
          "sdc_recursive_integer_array": {
            "type": [
              "null",
              "integer",
              "array"
            ],
            "items": {
              "$ref": "#/definitions/sdc_recursive_integer_array"
            }
          },
          "sdc_recursive_number_array": {
            "type": [
              "null",
              "number",
              "array"
            ],
            "items": {
              "$ref": "#/definitions/sdc_recursive_number_array"
            }
          },
          "sdc_recursive_string_array": {
            "type": [
              "null",
              "string",
              "array"
            ],
            "items": {
              "$ref": "#/definitions/sdc_recursive_string_array"
            }
          },
          "sdc_recursive_boolean_array": {
            "type": [
              "null",
              "boolean",
              "array"
            ],
            "items": {
              "$ref": "#/definitions/sdc_recursive_boolean_array"
            }
          },
          "sdc_recursive_timestamp_array": {
            "type": [
              "null",
              "string",
              "array"
            ],
            "format": "date-time",
            "items": {
              "$ref": "#/definitions/sdc_recursive_timestamp_array"
            }
          },
          "sdc_recursive_object_array": {
            "type": [
              "null",
              "object",
              "array"
            ],
            "items": {
              "$ref": "#/definitions/sdc_recursive_object_array"
            }
          }
        }
      }
    }
  ]
}
 
 
  • 执行同步
 ./tap-pg/venv/bin/tap-postgres -c tap-pg.json --catalog catalog.json | ./target-pg/venv/bin/target-postgres -c target-pg.json
 

效果

/Users/dalong/mylearning/singer-project/target-pg/venv/lib/python3.7/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel
 package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For det
ails see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
  """)
INFO Selected streams: ['postgres-public-userapps'] 
INFO No currently_syncing found
INFO Beginning sync of stream(postgres-public-userapps) with sync method(full)
INFO Stream postgres-public-userapps is using full_table replication
INFO Current Server Encoding: UTF8
INFO Current Client Encoding: UTF8
INFO hstore is UNavailable
INFO Beginning new Full Table replication 1559717835286
INFO select SELECT "id" , "userappname" , "username" , xmin::text::bigint
                                      FROM "public"."userapps"
                                     ORDER BY xmin::text ASC with itersize 20000
INFO METRIC: {"type": "counter", "metric": "record_count", "value": 2, "tags": {}}
INFO Table 'userapps' does not exist. Creating... CREATE TABLE copy.userapps ("id" bigint, "userappname" character varying, "username" character varying, PRIMARY KEY ("id"))
INFO Loading 2 rows into 'userapps'
INFO COPY userapps_temp ("id", "userappname", "username") FROM STDIN WITH (FORMAT CSV, ESCAPE '\')
INFO UPDATE 0
INFO INSERT 0 2
{"bookmarks": {"postgres-public-userapps": {"last_replication_method": "FULL_TABLE", "version": 1559717835286, "xmin": null}}, "currently_syncing": null}
 
  • 界面效果

说明

以上只是一个简单的演示,实际上我们可选的工具很多,比如dbt,pgloader,数据导出导入,其他类似etl 工具,或者使用pg 的fdw,dblink。。。

参考资料

https://github.com/rongfengliang/singer-pg2pg
https://github.com/singer-io/tap-postgres
https://www.getdbt.com/
https://github.com/dimitri/pgloader
https://www.postgresql.org/docs/10/contrib-dblink-function.html

使用singer tap-postgres 同步数据到pg的更多相关文章

  1. pipelinewise 基于singer 指南的的数据pipeline 工具

    pipelinewise 是基于开源singer 指南开发的数据pipeline工具,与singer tap 以及target 兼容 支持的特性 内置的elt 特性 轻量级 支持多种复制方法,cdc( ...

  2. 实现从Oracle增量同步数据到GreenPlum

    简介: GreenPlum是一个基于PostgreSQL数据库开发的MPP架构的数据库仓库,适用于OLAP系统,支持50PB(1PB=1000TB)级海量数据的存储和处理. 背景: 目前有一个业务是需 ...

  3. sql笨办法同步数据

    Helpers.SqlHelper sqlHelper = new Helpers.SqlHelper("server=***;database=Cms;user id=sa;passwor ...

  4. 对Big Table进行全表更新,导致 Replication 同步数据的过程十分缓慢

    在Publisher database中更新一个big table,数据行数是3.4亿多.由于没有更新 clustered Index key,因此,只产生了3.4亿多个Update Commands ...

  5. 【转】CentOS5.6下配置rsync内网同步数据到外网

    [转]CentOS5.6下配置rsync内网同步数据到外网 本文转自:http://www.linuxidc.com/Linux/2012-06/64070.htm 一.需求 卫士那边有一个需求,就是 ...

  6. zookeeper源码分析三LEADER与FOLLOWER同步数据流程

    根据二)中的分析,如果一台zookeeper服务器成为集群中的leader,那么一定是当前所有服务器中保存数据最多的服务器,所以在这台服务器成为leader之后,首先要做的事情就是与集群中的其它服务器 ...

  7. Dynamo涉及的算法和协议——p2p架构,一致性hash容错+gossip协议获取集群状态+向量时钟同步数据

    转自:http://www.letiantian.me/2014-06-16-dynamo-algorithm-protocol/ Dynamo是Amazon的一个分布式的键值系统,P2P架构,没有主 ...

  8. Windows 之间用rsync同步数据(cwRsyncServer配置)

    rsync是一款优秀的数据同步软件,在跨服务器,跨机房,跨国备份服务器的首选工具,下面就来介绍下如何配置安装cwRsyncServer很大多数软件一样是B/C架构,cwRsyncServer是rsyn ...

  9. linux和windows同步数据 cwrsync client to rsync server

    linux和windows同步数据,rsync server  cwrsync client linux server一般系统都自带rsync,如果没有就挂载系统盘自己安装一下,安装挺简单的不用我再多 ...

随机推荐

  1. MySQl数据库面试题

    1. MySQL中索引什么作用? 索引的定义和创建的目的 1) 索引是对数据库表中一列或者多列的值进行排序的一种结构,使用索引可快速访问数据库表中的特定信息 2) 索引的分类:主键索引,唯一索引,常规 ...

  2. 应用中有多个Spring Property PlaceHolder导致@Value只能获取到默认值

    背景 工作中负责的一套计费系统需要开发一个新通知功能,在扣费等事件触发后发送MQ,然后消费MQ发送邮件或短信通知给客户.因为有多套环境,测试时需要知道是从哪套环境发出的邮件,又不想维护多套通知模板,因 ...

  3. OpenResty部署nginx及nginx+lua

    因为用nginx+lua去开发,所以会选择用最流行的开源方案,就是用OpenResty nginx+lua打包在一起,而且提供了包括redis客户端,mysql客户端,http客户端在内的大量的组件 ...

  4. c#利用定时器自动备份数据库(mysql)

    1:引用dll MySql.Data.dll,   MySqlbackup.dll 2:建一个数据连接静态类 public static class mysql{public static strin ...

  5. C# vb .net实现棕褐色效果特效滤镜

    在.net中,如何简单快捷地实现Photoshop滤镜组中的棕褐色效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第一 ...

  6. MongoDB和Java(1):Linux下的MongoDB安装

    最近花了一些时间学习了下MongoDB数据库,感觉还是比较全面系统的,涉及了软件安装.客户端操作.安全认证.副本集和分布式集群搭建,以及使用Spring Data连接MongoDB进行数据操作,收获很 ...

  7. js正则表达式【续】(相关字符的解释含义)

    1.字符类[直接量] . (点号,小数点) 匹配任意单个字符,但是行结束符除外\d 匹配一个0-9之间的阿拉伯数字.等价于[0-9]\D    匹配任意一个不是0-9之间阿拉伯数字的字符.等价于[^0 ...

  8. Code Clean读书笔记

    代码整洁之道读书笔记 by fangpc 序言部分 "神在细节之中" - 建筑师路德维希 5S哲学(精益) 整理(Seiri):搞清楚事物之所在--通过恰当地命名之类的手段--至关 ...

  9. Spring+Velocity+Mybatis入门(step by step)

    一.开发工具 开发过程中使用的操作系统是OS X,关于软件安装的问题请大家移步高效的Mac环境设置. 本文是我对自己学习过程的一个回顾,应该还有不少问题待改进,例如目录的设置.编码习惯和配置文件的处理 ...

  10. Java开发环境之Eclipse

    查看更多Java开发环境配置,请点击<Java开发环境配置大全> 拾壹章:Eclipse安装教程 1)去官网下载安装包 http://www.eclipse.org/downloads/ ...