在做報(bào)表數(shù)據(jù)統(tǒng)計(jì)時(shí),我們用的是mysql + kafka + Spark Streaming方案,
kafka監(jiān)聽mysql訂單表中訂單狀態(tài),然后發(fā)送到spark streaming中進(jìn)行分析統(tǒng)計(jì)。
這里記錄一下kafka監(jiān)聽mysql中數(shù)據(jù)變更方案
1、Kafka connect
-
1.簡介
kafka connect 是一個可擴(kuò)展的、可靠的在kafka和其他系統(tǒng)之間流傳輸?shù)臄?shù)據(jù)工具。簡而言之就是他可以通過Connector(連接器)簡單、快速的將大集合數(shù)據(jù)導(dǎo)入和導(dǎo)出kafka。可以接收整個數(shù)據(jù)庫或收集來自所有的應(yīng)用程序的消息到kafka的topic中Kafka connect是Confluent公司(當(dāng)時(shí)開發(fā)出Apache Kafka的核心團(tuán)隊(duì)成員出來創(chuàng)立的新公司)開發(fā)的confluent platform的核心功能.大家都知道現(xiàn)在數(shù)據(jù)的ETL過程經(jīng)常會選擇kafka作為消息中間件應(yīng)用在離線和實(shí)時(shí)的使用場景中,而kafka的數(shù)據(jù)上游和下游一直沒有一個無縫銜接的pipeline來實(shí)現(xiàn)統(tǒng)一,比如會選擇flume或者logstash采集數(shù)據(jù)到kafka,然后kafka又通過其他方式pull或者push數(shù)據(jù)到目標(biāo)存儲.
而kafka connect旨在圍繞kafka構(gòu)建一個可伸縮的,可靠的數(shù)據(jù)流通道,通過kafka connect可以快速實(shí)現(xiàn)大量數(shù)據(jù)進(jìn)出kafka從而和其他源數(shù)據(jù)源或者目標(biāo)數(shù)據(jù)源進(jìn)行交互構(gòu)造一個低延遲的數(shù)據(jù)pipeline。
具體官網(wǎng)文檔www.confluent.io/.
https://docs.confluent.io/2.0.0/connect/connect-jdbc/docs/index.html#examples
-
2.安裝
- kafka安裝
-
下載kafka-connect-jdbc
下載成功后,從confluentinc-kafka-connect-jdbc-4.1.2.zip libs中獲取到kafka-connect-jdbc-4.1.2.jar,并把其放到kafka安裝目錄下libs文件夾中 - 下載mysql-connector-java-5.1.47.jar,并把其放到kafka安裝目錄下libs文件夾中
-
3.使用
- 1.啟動kafka sh kafkaStart.sh
- 2.創(chuàng)建kafka topic
./bin/kafka-run-class.sh kafka.admin.TopicCommand --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic mysql-kafka-comments
- 3.新建source/sink配置文件,并放置在kafka config目錄下
vim quickstart-mysql.properties name=mysql-b-source-comments connector.class=io.confluent.connect.jdbc.JdbcSourceConnector tasks.max=1 connection.url=jdbc:mysql://127.0.0.1:3306/android_service?user=xxx&password=xxxx # timestamp+incrementing 時(shí)間戳自增混合模式 mode=timestamp+incrementing # 時(shí)間戳 commenttime timestamp.column.name=commenttime # 自增字段 id incrementing.column.name=id # 白名單表 comments table.whitelist=comments # topic前綴 mysql-kafka- topic.prefix=mysql-kafka- vim quickstart-mysql-sink.properties name=mysql-b-sink-comments connector.class=io.confluent.connect.jdbc.JdbcSinkConnector tasks.max=1 #kafka的topic名稱 topics=mysql-kafka-comments # 配置JDBC鏈接 connection.url=jdbc:mysql://127.0.0.1:3306/android_service?user=xxx&password=xxxx # 不自動創(chuàng)建表,如果為true,會自動創(chuàng)建表,表名為topic名稱 auto.create=false # upsert model更新和插入 insert.mode=upsert # 下面兩個參數(shù)配置了以pid為主鍵更新 pk.mode = record_value pk.fields = id #表名為kafkatable table.name.format=kafkacomments
- 4.啟動kafka connect
./bin/connect-standalone.sh ./config/connect-standalone.properties ./config/quickstart-mysql.properties ./config/quickstart-mysql-sink.properties
啟動過程中有報(bào)8083端口已經(jīng)被占用,在config目錄下,修改connect-standalone文件,在最后一樣添加,用于修改監(jiān)聽REST API的默認(rèn)端口
#用于監(jiān)聽REST API的端口 rest.port=8003
- 插入數(shù)據(jù)
在comments表中插入數(shù)據(jù)后,可以看到在kafkacomments表中也同步插入了數(shù)據(jù)
- 插入數(shù)據(jù)
image.pngimage.png在comments表中更新數(shù)據(jù)后,可以看到在kafkacomments表中也同步更新了數(shù)據(jù)
image.pngimage.png- 6.查看kafka topic中數(shù)據(jù)
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic mysql-kafka-comments --from-beginning
image.png7.自定義開發(fā)connect
https://github.com/confluentinc/kafka-connect-jdbc
如果有需求要自定義開發(fā)connect的話,可以直接在這個源碼中開發(fā),然后打成jar包。開發(fā)一個連接器只需要實(shí)現(xiàn)兩個接口,Connector和Task8.參考文章
http://www.lxweimin.com/p/46b6fa53cae4
https://www.orchome.com/345
2、canal
canal是阿里開源的中間件,純Java開發(fā),基于數(shù)據(jù)庫增量日志解析,提供增量數(shù)據(jù)訂閱&消費(fèi),目前主要支持了MySQL(也支持mariaDB),主要用于同步mysql數(shù)據(jù)庫變更,是一個非常成熟的數(shù)據(jù)庫同步方案。
https://github.com/alibaba/canal/
canal是通過模擬成為mysql 的slave的方式,監(jiān)聽mysql 的binlog日志來獲取數(shù)據(jù),binlog設(shè)置為row模式以后,不僅能獲取到執(zhí)行的每一個增刪改的腳本,同時(shí)還能獲取到修改前和修改后的數(shù)據(jù),基于這個特性,canal就能高性能的獲取到mysql數(shù)據(jù)數(shù)據(jù)的變更。
canal的部署主要分為server端和client端。
server端部署好以后,可以直接監(jiān)聽mysql binlog,因?yàn)閟erver端是把自己模擬成了mysql slave,所以,只能接受數(shù)據(jù),沒有進(jìn)行任何邏輯的處理,具體的邏輯處理,需要client端進(jìn)行處理。
client端一般是需要大家進(jìn)行簡單的開發(fā)。https://github.com/alibaba/canal/wiki/ClientAPI 有一個簡單的示例,很容易理解。
canal 1.1.1版本之后, 默認(rèn)支持將canal server接收到的binlog數(shù)據(jù)直接投遞到MQ(kafka,RocketMQ)
https://github.com/alibaba/canal/wiki/Canal-Kafka-RocketMQ-QuickStart