flink-cdc同步mysql數據到hbase

本文首發於我的個人博客網站 等待下一個秋-Flink

什麼是CDC?

CDC是(Change Data Capture 變更數據獲取)的簡稱。核心思想是,監測並捕獲數據庫的變動(包括數據 或 數據表的插入INSERT、更新UPDATE、刪除DELETE等),將這些變更按發生的順序完整記錄下來,寫入到消息中間件中以供其他服務進行訂閱及消費。

Flink_CDC

1. 環境準備

  • mysql

  • hbase

  • flink 1.13.5 on yarn

說明:如果沒有安裝hadoop,那麼可以不用yarn,直接用flink standalone環境吧。

2. 下載下列依賴包

下面兩個地址下載flink的依賴包,放在lib目錄下面。

  1. flink-sql-connector-hbase-1.4_2.11-1.13.5.jar
  2. flink-sql-connector-mysql-cdc-1.4.0.jar

如果你的Flink是其它版本,可以來這裏下載。

我是flink1.13,這裏flink-sql-connector-mysql-cdc,需要1.4.0以上版本。

image-20220913170030754

如果你是更高版本的flink,可以自行https://github.com/ververica/flink-cdc-connectors下載新版mvn clean install -DskipTests 自己編譯。

img

這是我編譯的最新版2.2,傳上去發現太新了,如果重新換個版本,我得去gitee下載源碼,不然github速度太慢了,然後用IDEA編譯打包,又得下載一堆依賴。我投降,我直接去網上下載了個1.4的直接用了。

我下載的jar包,放在flink的lib目錄下面:

image-20220915145142496

flink-sql-connector-hbase-1.4_2.11-1.13.5.jar
flink-sql-connector-mysql-cdc-1.4.0.jar
  1. 先在yarn上面啓動一個application,進入flink13.5目錄,執行:
bin/yarn-session.sh -d -s 2 -jm 1024 -tm 2048 -qu root.sparkstreaming -nm flink-cdc-hbase
  1. 進入flink sql命令行
bin/sql-client.sh embedded -s flink-cdc-hbase

img

4. 同步數據

這裏有一張mysql表:

CREATE TABLE `product_view` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`product_id` int(11) NOT NULL,
`server_id` int(11) NOT NULL,
`duration` int(11) NOT NULL,
`times` varchar(11) NOT NULL,
`time` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `time` (`time`),
KEY `user_product` (`user_id`,`product_id`) USING BTREE,
KEY `times` (`times`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 樣本數據
INSERT INTO `product_view` VALUES ('1', '1', '1', '1', '120', '120', '2020-04-24 13:14:00');
INSERT INTO `product_view` VALUES ('2', '1', '1', '1', '120', '120', '2020-04-24 13:14:00');
INSERT INTO `product_view` VALUES ('3', '1', '1', '3', '120', '120', '2020-04-24 13:14:00');
INSERT INTO `product_view` VALUES ('4', '1', '1', '2', '120', '120', '2020-04-24 13:14:00');
INSERT INTO `product_view` VALUES ('5', '8', '1', '1', '120', '120', '2020-05-14 13:14:00');
INSERT INTO `product_view` VALUES ('6', '8', '1', '2', '120', '120', '2020-05-13 13:14:00');
INSERT INTO `product_view` VALUES ('7', '8', '1', '3', '120', '120', '2020-04-24 13:14:00');
INSERT INTO `product_view` VALUES ('8', '8', '1', '3', '120', '120', '2020-04-23 13:14:00');
INSERT INTO `product_view` VALUES ('9', '8', '1', '2', '120', '120', '2020-05-13 13:14:00');
  1. 創建數據表關聯mysql
CREATE TABLE product_view_source (
`id` int,
`user_id` int,
`product_id` int,
`server_id` int,
`duration` int,
`times` string,
`time` timestamp,
PRIMARY KEY (`id`) NOT ENFORCED
) WITH (
'connector' = 'mysql-cdc',
'hostname' = '192.168.1.2',
'port' = '3306',
'username' = 'bigdata',
'password' = 'bigdata',
'database-name' = 'test',
'table-name' = 'product_view'
);

這樣,我們在flink sql client操作這個表相當於操作mysql裏面的對應表。

  1. 創建數據表關聯hbase
CREATE TABLE product_view_hbase (
 rowkey INT,
 family1 ROW<user_id INT, product_id INT, server_id INT, duration INT>,
 PRIMARY KEY (rowkey) NOT ENFORCED
) WITH (
 'connector' = 'hbase-1.4',
 'table-name' = 'product_view',
 'zookeeper.quorum' = 'cdh-001:2181'
);

這裏,需要提前在hbase裏面創建好product_view這個主題。

  1. 同步數據

flink-cdc-mysql2hbase

建立同步任務,可以使用sql如下:

insert into product_view_hbase select id as rowkey, ROW(user_id, product_id, server_id, duration) from product_view_source;

這個時候是可以退出flink sql-client的,然後進入flink web-ui,可以看到mysql表數據已經同步到hbase中了,對mysql進行插入,hbase都是同步更新的。

進入hbase shell,可以看到數據已經從mysql同步到hbase了:

hbase(main):009:0> scan 'product_view'
ROW                                    COLUMN+CELL                                                                                                   
 \x00\x00\x00\x01                      column=family1:duration, timestamp=1663223736391, value=\x00\x00\x00x                                         
 \x00\x00\x00\x01                      column=family1:product_id, timestamp=1663223736391, value=\x00\x00\x00\x01                                    
 \x00\x00\x00\x01                      column=family1:server_id, timestamp=1663223736391, value=\x00\x00\x00\x01                                     
 \x00\x00\x00\x01                      column=family1:user_id, timestamp=1663223736391, value=\x00\x00\x00\x01                                       
 \x00\x00\x00\x02                      column=family1:duration, timestamp=1663223736391, value=\x00\x00\x00x                                         
 \x00\x00\x00\x02                      column=family1:product_id, timestamp=1663223736391, value=\x00\x00\x00\x01                                    
 \x00\x00\x00\x02                      column=family1:server_id, timestamp=1663223736391, value=\x00\x00\x00\x01                                     
 \x00\x00\x00\x02                      column=family1:user_id, timestamp=1663223736391, value=\x00\x00\x00\x01                                       
 \x00\x00\x00\x03                      column=family1:duration, timestamp=1663223736391, value=\x00\x00\x00x                                         
 \x00\x00\x00\x03                      column=family1:product_id, timestamp=1663223736391, value=\x00\x00\x00\x01                                    
 \x00\x00\x00\x03                      column=family1:server_id, timestamp=1663223736391, value=\x00\x00\x00\x03                                     
 \x00\x00\x00\x03                      column=family1:user_id, timestamp=1663223736391, value=\x00\x00\x00\x01                                       
 \x00\x00\x00\x04                      column=family1:duration, timestamp=1663223736391, value=\x00\x00\x00x                                         
 \x00\x00\x00\x04                      column=family1:product_id, timestamp=1663223736391, value=\x00\x00\x00\x01                                    
 \x00\x00\x00\x04                      column=family1:server_id, timestamp=1663223736391, value=\x00\x00\x00\x02                                     
 \x00\x00\x00\x04                      column=family1:user_id, timestamp=1663223736391, value=\x00\x00\x00\x01                                       
 \x00\x00\x00\x05                      column=family1:duration, timestamp=1663223736391, value=\x00\x00\x00x                                         
 \x00\x00\x00\x05                      column=family1:product_id, timestamp=1663223736391, value=\x00\x00\x00\x01                                    
 \x00\x00\x00\x05                      column=family1:server_id, timestamp=1663223736391, value=\x00\x00\x00\x01                                     
 \x00\x00\x00\x05                      column=family1:user_id, timestamp=1663223736391, value=\x00\x00\x00\x08                                       
 \x00\x00\x00\x06                      column=family1:duration, timestamp=1663223736391, value=\x00\x00\x00x                                         
 \x00\x00\x00\x06                      column=family1:product_id, timestamp=1663223736391, value=\x00\x00\x00\x01                                    
 \x00\x00\x00\x06                      column=family1:server_id, timestamp=1663223736391, value=\x00\x00\x00\x02                                     
 \x00\x00\x00\x06                      column=family1:user_id, timestamp=1663223736391, value=\x00\x00\x00\x08                                       
 \x00\x00\x00\x07                      column=family1:duration, timestamp=1663223736391, value=\x00\x00\x00x                                         
 \x00\x00\x00\x07                      column=family1:product_id, timestamp=1663223736391, value=\x00\x00\x00\x01                                    
 \x00\x00\x00\x07                      column=family1:server_id, timestamp=1663223736391, value=\x00\x00\x00\x03                                     
 \x00\x00\x00\x07                      column=family1:user_id, timestamp=1663223736391, value=\x00\x00\x00\x08                                       
 \x00\x00\x00\x09                      column=family1:duration, timestamp=1663223736391, value=\x00\x00\x00x                                         
 \x00\x00\x00\x09                      column=family1:product_id, timestamp=1663223736391, value=\x00\x00\x00\x01                                    
 \x00\x00\x00\x09                      column=family1:server_id, timestamp=1663223736391, value=\x00\x00\x00\x03                                     
 \x00\x00\x00\x09                      column=family1:user_id, timestamp=1663223736391, value=\x00\x00\x00\x08                                       
 \x00\x00\x00\x0A                      column=family1:duration, timestamp=1663223736391, value=\x00\x00\x00x                                         
 \x00\x00\x00\x0A                      column=family1:product_id, timestamp=1663223736391, value=\x00\x00\x00\x01                                    
 \x00\x00\x00\x0A                      column=family1:server_id, timestamp=1663223736391, value=\x00\x00\x00\x02                                     
 \x00\x00\x00\x0A                      column=family1:user_id, timestamp=1663223736391, value=\x00\x00\x00\x08                                       
9 row(s)
Took 0.1656 seconds                                                                              

直接在flink-sql client裏面查詢hbase數據,也是可以的:

Flink SQL> select * from product_view_hbase ;
2022-09-15 15:38:23,205 INFO  org.apache.flink.yarn.YarnClusterDescriptor                  [] - No path for the flink jar passed. Using the location of class org.apache.flink.yarn.YarnClusterDescriptor to locate the jar
2022-09-15 15:38:23,207 INFO  org.apache.hadoop.yarn.client.ConfiguredRMFailoverProxyProvider [] - Failing over to rm72
2022-09-15 15:38:23,212 INFO  org.apache.flink.yarn.YarnClusterDescriptor                  [] - Found Web Interface cdh-001:35225 of application 'application_1633924491541_7321'.

執行上面查詢sql,就會進入界面,這就是hbase裏面的數據了:

image-20220915153846428

5. 關聯查詢

在這個flink-sql client環境中,這裏有兩張表:product_view_source(mysql的表)和product_view_hbase(hbase的表),後者是有前者查詢導入的,這裏爲了簡單,我沒有再關聯其它第三張表,就用這兩張表,做關聯查詢,達到演示的目的。

select product_view_source.*, product_view_hbase.*  from product_view_source
inner join product_view_hbase
on product_view_source.id = product_view_hbase.rowkey;

這裏做了個簡單的關聯查詢,通過id跟rowkey關聯,然後打開web-ui,通過flink web-ui結果可以看出,這裏是個hash join,兩個source,到join,一共3個task。

image-20220915160523185

看看查出來的結果吧,這是flnk-sql client:

image-20220915160617440

比如我選中這一行,進來後是這條數據的詳細情況,是沒有問題的:

image-20220915160731799

參考資料

https://nightlies.apache.org/flink/flink-docs-release-1.13/zh/docs/connectors/table/hbase/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章