Debezium SQL Server Source Connector+Kafka+Spark+MySQL 实时数据处理

写在前面

前段时间在实时获取SQLServer数据库变化时候,整个过程可谓是坎坷。然后就想在这里记录一下。
本文的技术栈: Debezium SQL Server Source Connector+Kafka+Spark+MySQL
ps:后面应该会将数据放到Kudu上。
然后主要记录一下,整个组件使用和组件对接过程中一些注意点和坑。

注意如果使用confluent端口9092,8083

开始吧

在处理实时数据时,需要即时地获得数据库表中数据的变化,然后将数据变化发送到Kafka中。不同的数据库有不同的组件进行处理。
常见的MySQL数据库,就有比较多的支持 canalmaxwell等,他们都是类似 MySQL binlog 增量订阅&消费组件这种模式 。那么关于微软的SQLServer数据库,好像整个开源社区 支持就没有那么好了。

1.选择Connector

Debezium的SQL Server连接器是一种源连接器,可以获取SQL Server数据库中现有数据的快照,然后监视和记录对该数据的所有后续行级更改。每个表的所有事件都记录在单独的Kafka Topic中,应用程序和服务可以轻松使用它们。然后本连接器也是基于MSSQL的change data capture实现。

2.安装Connector

我参照官方文档安装是没有问题的。

2.1 Installing Confluent Hub Client

Confluent Hub客户端本地安装为Confluent Platform的一部分,位于/ bin目录中。
Linux
Download and unzip the Confluent Hub tarball.

  1. [root@hadoop001 softs]# ll confluent-hub-client-latest.tar
  2. -rw-r--r--. 1 root root 6909785 9 24 10:02 confluent-hub-client-latest.tar
  3. [root@hadoop001 softs]# tar confluent-hub-client-latest.tar -C ../app/conn/
  4. [root@hadoop001 softs]# ll ../app/conn/
  5. 总用量 6748
  6. drwxr-xr-x. 2 root root 27 9 24 10:43 bin
  7. -rw-r--r--. 1 root root 6909785 9 24 10:02 confluent-hub-client-latest.tar
  8. drwxr-xr-x. 3 root root 34 9 24 10:05 etc
  9. drwxr-xr-x. 2 root root 6 9 24 10:08 kafka-mssql
  10. drwxr-xr-x. 4 root root 29 9 24 10:05 share
  11. [root@hadoop001 softs]#

配置bin目录到系统环境变量中

  1. export CONN_HOME=/root/app/conn
  2. export PATH=$CONN_HOME/bin:$PATH

确认是否安装成功

  1. [root@hadoop001 ~]# source /etc/profile
  2. [root@hadoop001 ~]# confluent-hub
  3. usage: confluent-hub <command> [ <args> ]
  4. Commands are:
  5. help Display help information
  6. install install a component from either Confluent Hub or from a local file
  7. See 'confluent-hub help <command>' for more information on a specific command.
  8. [root@hadoop001 ~]#

2.2 Install the SQL Server Connector 使用命令confluent-hub

  1. [root@hadoop001 ~]# confluent-hub install debezium/debezium-connector-sqlserver:0.9.4
  2. The component can be installed in any of the following Confluent Platform installations:
  3. 1. / (installed rpm/deb package)
  4. 2. /root/app/conn (where this tool is installed)
  5. Choose one of these to continue the installation (1-2): 2
  6. Do you want to install this into /root/app/conn/share/confluent-hub-components? (yN) n
  7. Specify installation directory: /root/app/conn/share/java/confluent-hub-client
  8. Component's license:
  9. Apache 2.0
  10. https://github.com/debezium/debezium/blob/master/LICENSE.txt
  11. I agree to the software license agreement (yN) y
  12. You are about to install 'debezium-connector-sqlserver' from Debezium Community, as published on Confluent Hub.
  13. Do you want to continue? (yN) y

注意:Specify installation directory:这个安装目录最好是你刚才的confluent-hub 目录下的 /share/java/confluent-hub-client 这个目录下。其余的基本操作就好。

3.配置Connector

首先需要对Connector进行配置,配置文件位于 $KAFKA_HOME/config/connect-distributed.properties:

  1. # These are defaults. This file just demonstrates how to override some settings.
  2. # kafka集群地址,我这里是单节点多Broker模式
  3. bootstrap.servers=haoop001:9093,hadoop001:9094,hadoop001:9095
  4. # Connector集群的名称,同一集群内的Connector需要保持此group.id一致
  5. group.id=connect-cluster
  6. # The converters specify the format of data in Kafka and how to translate it into Connect data. Every Connect user will
  7. # need to configure these based on the format they want their data in when loaded from or stored into Kafka
  8. # 存储到kafka的数据格式
  9. key.converter=org.apache.kafka.connect.json.JsonConverter
  10. value.converter.schemas.enable=false
  11. # The internal converter used for offsets and config data is configurable and must be specified, but most users will
  12. # 内部转换器的格式,针对offsets、config和status,一般不需要修改
  13. internal.key.converter=org.apache.kafka.connect.json.JsonConverter
  14. internal.value.converter=org.apache.kafka.connect.json.JsonConverter
  15. internal.key.converter.schemas.enable=false
  16. internal.value.converter.schemas.enable=false
  17. # Topic to use for storing offsets. This topic should have many partitions and be replicated.
  18. # 用于保存offsets的topic,应该有多个partitions,并且拥有副本(replication),主要根据你的集群实际情况来
  19. # Kafka Connect会自动创建这个topic,但是你可以根据需要自行创建
  20. offset.storage.topic=connect-offsets-2
  21. offset.storage.replication.factor=3
  22. offset.storage.partitions=1
  23. # 保存connector和task的配置,应该只有1个partition,并且有3个副本
  24. config.storage.topic=connect-configs-2
  25. config.storage.replication.factor=3
  26. # 用于保存状态,可以拥有多个partition和replication
  27. # Topic to use for storing statuses. This topic can have multiple partitions and should be replicated.
  28. status.storage.topic=connect-status-2
  29. status.storage.replication.factor=3
  30. status.storage.partitions=1
  31. offset.storage.file.filename=/root/data/kafka-logs/offset-storage-file
  32. # Flush much faster than normal, which is useful for testing/debugging
  33. offset.flush.interval.ms=10000
  34. # REST端口号
  35. rest.port=18083
  36. # 保存connectors的路径
  37. #plugin.path=/root/app/kafka_2.11-0.10.1.1/connectors
  38. plugin.path=/root/app/conn/share/java/confluent-hub-client

4.创建kafka Topic

我这里是单节点多Broker模式的Kafka,那么创建Topic可以如下:

  1. kafka-topics.sh --zookeeper hadoop001:2181 --create --topic connect-offsets-2 --replication-factor 3 --partitions 1
  2. kafka-topics.sh --zookeeper hadoop001:2181 --create --topic connect-configs-2 --replication-factor 3 --partitions 1
  3. kafka-topics.sh --zookeeper hadoop001:2181 --create --topic connect-status-2 --replication-factor 3 --partitions 1

查看状态 <很重要>

  1. [root@hadoop001 ~]# kafka-topics.sh --describe --zookeeper hadoop001:2181 --topic connect-offsets-2
  2. Topic:connect-offsets-2 PartitionCount:1 ReplicationFactor:3 Configs:
  3. Topic: connect-offsets-2 Partition: 0 Leader: 3 Replicas: 3,1,2 Isr: 3,1,2
  4. [root@hadoop001 ~]# kafka-topics.sh --describe --zookeeper hadoop001:2181 --topic connect-configs-2
  5. Topic:connect-configs-2 PartitionCount:1 ReplicationFactor:3 Configs:
  6. Topic: connect-configs-2 Partition: 0 Leader: 1 Replicas: 1,2,3 Isr: 1,2,3
  7. [root@hadoop001 ~]# kafka-topics.sh --describe --zookeeper hadoop001:2181 --topic connect-status-2
  8. Topic:connect-status-2 PartitionCount:1 ReplicationFactor:3 Configs:
  9. Topic: connect-status-2 Partition: 0 Leader: 3 Replicas: 3,1,2 Isr: 3,1,2
  10. [root@hadoop001 ~]#

5.开启SqlServer Change Data Capture(CDC)更改数据捕获

变更数据捕获用于捕获应用到 SQL Server 表中的插入、更新和删除活动,并以易于使用的关系格式提供这些变更的详细信息。变更数据捕获所使用的更改表中包含镜像所跟踪源表列结构的列,同时还包含了解所发生的变更所需的元数据。变更数据捕获提供有关对表和数据库所做的 DML 更改的信息。通过使用变更数据捕获,您无需使用费用高昂的方法,如用户触发器、时间戳列和联接查询等。
数据变更历史表会随着业务的持续,变得很大,所以默认情况下,变更数据历史会在本地数据库保留3天(可以通过视图msdb.dbo.cdc_jobs的字段retention来查询,当然也可以更改对应的表来修改保留时间),每天会通过SqlServer后台代理任务,每天晚上2点定时删除。所以推荐定期的将变更数据转移到数据仓库中。
以下命令基本就够用了

  1. --查看数据库是否起用CDC
  2. GO
  3. SELECT [name], database_id, is_cdc_enabled
  4. FROM sys.databases
  5. GO
  6. --数据库起用CDC
  7. USE test1
  8. GO
  9. EXEC sys.sp_cdc_enable_db
  10. GO
  11. --关闭数据库CDC
  12. USE test1
  13. go
  14. exec sys.sp_cdc_disable_db
  15. go
  16. --查看表是否启用CDC
  17. USE test1
  18. GO
  19. SELECT [name], is_tracked_by_cdc
  20. FROM sys.tables
  21. GO
  22. --启用表的CDC,前提是数据库启用
  23. USE Demo01
  24. GO
  25. EXEC sys.sp_cdc_enable_table
  26. @source_schema = 'dbo',
  27. @source_name = 'user',
  28. @capture_instance='user',
  29. @role_name = NULL
  30. GO
  31. --关闭表上的CDC功能
  32. USE test1
  33. GO
  34. EXEC sys.sp_cdc_disable_table
  35. @source_schema = 'dbo',
  36. @source_name = 'user',
  37. @capture_instance='user'
  38. GO
  39. --可能不记得或者不知道开启了什么表的捕获,返回所有表的变更捕获配置信息
  40. EXECUTE sys.sp_cdc_help_change_data_capture;
  41. GO
  42. --查看对某个实例(即表)的哪些列做了捕获监控:
  43. EXEC sys.sp_cdc_get_captured_columns
  44. @capture_instance = 'user'
  45. --查找配置信息 -retention 变更数据保留的分钟数
  46. SELECT * FROM test1.dbo.cdc_jobs
  47. --更改数据保留时间为分钟
  48. EXECUTE sys.sp_cdc_change_job
  49. @job_type = N'cleanup',
  50. @retention=1440
  51. GO
  52. --停止捕获作业
  53. exec sys.sp_cdc_stop_job N'capture'
  54. go
  55. --启动捕获作业
  56. exec sys.sp_cdc_start_job N'capture'
  57. go

6.运行Connector

怎么运行呢?参照

  1. [root@hadoop001 bin]# pwd
  2. /root/app/kafka_2.11-1.1.1/bin
  3. [root@hadoop001 bin]# ./connect-distributed.sh
  4. USAGE: ./connect-distributed.sh [-daemon] connect-distributed.properties
  5. [root@hadoop001 bin]#
  6. [root@hadoop001 bin]# ./connect-distributed.sh ../config/connect-distributed.properties
  7. ... 这里就会有大量日志输出

验证:

  1. [root@hadoop001 ~]# netstat -tanp |grep 18083
  2. tcp6 0 0 :::18083 :::* LISTEN 29436/java
  3. [root@hadoop001 ~]#

6.1 获取Worker的信息

ps:可能你需要安装jq这个软件: yum -y install jq ,当然可以在浏览器上打开

  1. [root@hadoop001 ~]# curl -s hadoop001:18083 | jq
  2. {
  3. "version": "1.1.1",
  4. "commit": "8e07427ffb493498",
  5. "kafka_cluster_id": "dmUSlNNLQ9OyJiK-bUc6Tw"
  6. }
  7. [root@hadoop001 ~]#

6.2 获取Worker上已经安装的Connector

  1. [root@hadoop001 ~]# curl -s hadoop001:18083/connector-plugins | jq
  2. [
  3. {
  4. "class": "io.debezium.connector.sqlserver.SqlServerConnector",
  5. "type": "source",
  6. "version": "0.9.5.Final"
  7. },
  8. {
  9. "class": "org.apache.kafka.connect.file.FileStreamSinkConnector",
  10. "type": "sink",
  11. "version": "1.1.1"
  12. },
  13. {
  14. "class": "org.apache.kafka.connect.file.FileStreamSourceConnector",
  15. "type": "source",
  16. "version": "1.1.1"
  17. }
  18. ]
  19. [root@hadoop001 ~]#

可以看见io.debezium.connector.sqlserver.SqlServerConnector 这个是我们自己刚才安装的连接器

6.3 列出当前运行的connector(task)

  1. [root@hadoop001 ~]# curl -s hadoop001:18083/connectors | jq
  2. []
  3. [root@hadoop001 ~]#

6.4 提交Connector用户配置 《重点》

当提交用户配置时,就会启动一个Connector Task,
Connector Task执行实际的作业。
用户配置是一个Json文件,同样通过REST API提交:

  1. curl -s -X POST -H "Content-Type: application/json" --data '{
  2. "name": "connector-mssql-online-1",
  3. "config": {
  4. "connector.class" : "io.debezium.connector.sqlserver.SqlServerConnector",
  5. "tasks.max" : "1",
  6. "database.server.name" : "test1",
  7. "database.hostname" : "hadoop001",
  8. "database.port" : "1433",
  9. "database.user" : "sa",
  10. "database.password" : "xxx",
  11. "database.dbname" : "test1",
  12. "database.history.kafka.bootstrap.servers" : "hadoop001:9093",
  13. "database.history.kafka.topic": "test1.t201909262.bak"
  14. }
  15. }' http://hadoop001:18083/connectors

马上查看connector当前状态,确保状态是RUNNING

  1. [root@hadoop001 ~]# curl -s hadoop001:18083/connectors/connector-mssql-online-1/status | jq
  2. {
  3. "name": "connector-mssql-online-1",
  4. "connector": {
  5. "state": "RUNNING",
  6. "worker_id": "xxx:18083"
  7. },
  8. "tasks": [
  9. {
  10. "state": "RUNNING",
  11. "id": 0,
  12. "worker_id": "xxx:18083"
  13. }
  14. ],
  15. "type": "source"
  16. }
  17. [root@hadoop001 ~]#

此时查看Kafka Topic

  1. [root@hadoop001 ~]# kafka-topics.sh --list --zookeeper hadoop001:2181
  2. __consumer_offsets
  3. connect-configs-2
  4. connect-offsets-2
  5. connect-status-2
  6. #自动生成的Topic, 记录表结构的变化,生成规则:你的connect中自定义的
  7. test1.t201909262.bak
  8. [root@hadoop001 ~]#

再次列出运行的connector(task)

  1. [root@hadoop001 ~]# curl -s hadoop001:18083/connectors | jq
  2. [
  3. "connector-mssql-online-1"
  4. ]
  5. [root@hadoop001 ~]#

6.5 查看connector的信息

  1. [root@hadoop001 ~]# curl -s hadoop001:18083/connectors/connector-mssql-online-1 | jq
  2. {
  3. "name": "connector-mssql-online-1",
  4. "config": {
  5. "connector.class": "io.debezium.connector.sqlserver.SqlServerConnector",
  6. "database.user": "sa",
  7. "database.dbname": "test1",
  8. "tasks.max": "1",
  9. "database.hostname": "hadoop001",
  10. "database.password": "xxx",
  11. "database.history.kafka.bootstrap.servers": "hadoop001:9093",
  12. "database.history.kafka.topic": "test1.t201909262.bak",
  13. "name": "connector-mssql-online-1",
  14. "database.server.name": "test1",
  15. "database.port": "1433"
  16. },
  17. "tasks": [
  18. {
  19. "connector": "connector-mssql-online-1",
  20. "task": 0
  21. }
  22. ],
  23. "type": "source"
  24. }
  25. [root@hadoop001 ~]#

6.6 查看connector下运行的task信息

  1. [root@hadoop001 ~]# curl -s hadoop001:18083/connectors/connector-mssql-online-1/tasks | jq
  2. [
  3. {
  4. "id": {
  5. "connector": "connector-mssql-online-1",
  6. "task": 0
  7. },
  8. "config": {
  9. "connector.class": "io.debezium.connector.sqlserver.SqlServerConnector",
  10. "database.user": "sa",
  11. "database.dbname": "test1",
  12. "task.class": "io.debezium.connector.sqlserver.SqlServerConnectorTask",
  13. "tasks.max": "1",
  14. "database.hostname": "hadoop001",
  15. "database.password": "xxx",
  16. "database.history.kafka.bootstrap.servers": "hadoop001:9093",
  17. "database.history.kafka.topic": "test1.t201909262.bak",
  18. "name": "connector-mssql-online-1",
  19. "database.server.name": "test1",
  20. "database.port": "1433"
  21. }
  22. }
  23. ]
  24. [root@hadoop001 ~]#

task的配置信息继承自connector的配置

6.7 暂停/重启/删除 Connector

  1. # curl -s -X PUT hadoop001:18083/connectors/connector-mssql-online-1/pause
  2. # curl -s -X PUT hadoop001:18083/connectors/connector-mssql-online-1/resume
  3. # curl -s -X DELETE hadoop001:18083/connectors/connector-mssql-online-1

7.从Kafka中读取变动数据

  1. # 记录表结构的变化,生成规则:你的connect中自定义的
  2. kafka-console-consumer.sh --bootstrap-server hadoop001:9093 --topic test1.t201909262.bak --from-beginning
  3. # 记录数据的变化,生成规则:test1.dbo.t201909262
  4. kafka-console-consumer.sh --bootstrap-server hadoop001:9093 --topic test1.dbo.t201909262 --from-beginning

这里就是:

  1. kafka-console-consumer.sh --bootstrap-server hadoop001:9093 --topic test1.dbo.t201909262 --from-beginning
  2. kafka-console-consumer.sh --bootstrap-server hadoop001:9093 --topic test1.dbo.t201909262

8. 对表进行 DML语句 操作

新增数据:
然后kafka控制台也就会马上打出日志
Debezium SQL Server - 图1
spark 对接kafka 10s一个批次
Debezium SQL Server - 图2
然后就会将这个新增的数据插入到MySQL中去
具体的处理逻辑后面再花时间来记录一下
修改和删除也是OK的,就不演示了
有任何问题,欢迎留言一起交流~~
更多好文:https://blog.csdn.net/liuge36
参考文章:
https://docs.confluent.io/current/connect/debezium-connect-sqlserver/index.html#sqlserver-source-connector
https://docs.microsoft.com/en-us/sql/relational-databases/track-changes/track-data-changes-sql-server?view=sql-server-2017
https://blog.csdn.net/qq_19518987/article/details/89329464
http://www.tracefact.net/tech/087.html

分类: 大数据学习