使用kafka confluent 同步数据库到kafka消息队列中

1. 背景

  近期正在整合集团数据,内部有很多数据需要进行同步,同步方式可以选择接口或者是数据库同步,内部系统我们选择使用数据库同步的方式,外部系统选择使用接口的方式进行同步。数据库同步软件也有很多,我们希望同步的数据被多次消费,最好将同步的数据写入到消息队列中。最终选择了使用kafka Confluent, 下面将对 Confluent进行详细的介绍。

2. 介绍

    Confluent Platform 可以轻松构建实时数据管道和流应用程序。将来自多个源和位置的数据集成到公司的单个中心事件流平台中。Confluent Platform让您专注于如何从数据中获取商业价值,而不是把时间浪费在底层机制建设上,比如数据如何在不同的系统之间传输或处理。具体来说,Confluent Platform简化了将数据源连接到Apache Kafka、使用Kafka构建应用程序以及保护、监视和管理Kafka基础设施的过程。

使用kafka confluent 同步数据库到kafka消息队列中

3. 实操

    1. 下载安装

wget http://packages.confluent.io/archive/5.2/confluent-5.2.1-2.12.tar.gz

       下载完成后进行解压

    2.  修改配置文件

      1. 修改kafka配置文件 1406  vim /opt/confluent-5.2.0/etc/kafka/server.properties

      2. 修改zk配置文件  vim /opt/confluent-5.2.0/etc/kafka/zookeeper.properties

      3. 修改schema-registry配置文件 vim confluent-5.2.0/etc/schema-registry/schema-registry.properties

    3.启动

      进入confluent 软件目录,执行命令  ./bin/confluent start

       (confluent 正常启动,如果没有启动可以执行单个服务查看具体没启动起来的原因,多是端口被其他服务占用)

      

Starting connect
connect is [UP]
Starting ksql-server
ksql-server is [UP]
Starting control-center
control-center is [UP]

    说明启动成功

    4. 配置同步  

      1. 配置kafka同步信息 修改文件 vim /opt/confluent-5.2.0/etc/schema-registry/connect-avro-standalone.properties

      2. 配置数据库同步信息 新增文件  vim /opt/confluent-5.2.0/etc/kafka-connect-jdbc/postgresql.propertie

# tasks to create:
name=postgresql-jdbc-autoincrement
connector.class=io.confluent.connect.jdbc.JdbcSourceConnector
tasks.max=10
# The remaining configs are specific to the JDBC source connector. In this example, we connect to a
# SQLite database stored in the file test.db, use and auto-incrementing column called 'id' to
# detect new rows as they are added, and output to topics prefixed with 'test-sqlite-jdbc-', e.g.
# a table called 'users' will be written to the topic 'test-sqlite-jdbc-users'.
connection.url=jdbc:postgresql://127.0.0.1:端口/database?user=admin&password=123456
table.types=TABLE,VIEW
table.whitelist=table1,table2

numeric.mapping=best_fit

mode=incrementing
incrementing.column.name=id

topic.prefix=cc-

validate.non.null=false
errors.log.enable = true

#对应postgresql中timestamp的时区
db.timezone=PRC

5. 启动同步

   

/opt/confluent-5.2.0/bin/connect-standalone /opt/confluent-5.2.0/etc/schema-registry/connect-avro-standalone.properties  /opt/confluent-5.2.0/etc/kafka-connect-jdbc/postgresql.properties