Flink 使用介紹相關(guān)文檔目錄
CDC 簡介
CDC即Change Data Capture 變更數(shù)據(jù)捕獲,為Flink 1.11中一個(gè)新增功能。我們可以通過CDC得知數(shù)據(jù)源表的更新內(nèi)容(包含Insert Update和Delete),并將這些更新內(nèi)容作為數(shù)據(jù)流發(fā)送到下游系統(tǒng)。捕獲到的數(shù)據(jù)操作具有一個(gè)標(biāo)識符,分別對應(yīng)數(shù)據(jù)的增加,修改和刪除。
- +I:新增數(shù)據(jù)。
- -U:一條數(shù)據(jù)的修改會產(chǎn)生兩個(gè)
U
標(biāo)識符數(shù)據(jù)。其中-U
含義為修改前數(shù)據(jù)。 - +U:修改之后的數(shù)據(jù)。
- -D:刪除的數(shù)據(jù)。
MySQL 啟用binlog
接下來以MySQL CDC為例,和大家一起配置Flink MySQL CDC。
在使用CDC之前務(wù)必要開啟MySQL的binlog。下面以MySQL 5.7版本為例說明。
修改my.cnf
文件,在[mysqld]
一節(jié)增加:
server_id=1
log_bin=mysql-bin
binlog_format=ROW
expire_logs_days=30
binlog_do_db=db_a
binlog_do_db=db_b
配置項(xiàng)的解釋如下:
- server_id:MySQL5.7及以上版本開啟binlog必須要配置這個(gè)選項(xiàng)。對于MySQL集群,不同節(jié)點(diǎn)的server_id必須不同。對于單實(shí)例部署則沒有要求。
- log_bin:指定binlog文件名和儲存位置。如果不指定路徑,默認(rèn)位置為
/var/lib/mysql/
。 - binlog_format:binlog格式。有3個(gè)值可以選擇:ROW:記錄哪條數(shù)據(jù)被修改和修改之后的數(shù)據(jù),會產(chǎn)生大量日志。STATEMENT:記錄修改數(shù)據(jù)的SQL,日志量較小。MIXED:混合使用上述兩個(gè)模式。CDC要求必須配置為ROW。
- expire_logs_days:bin_log過期時(shí)間,超過該時(shí)間的log會自動(dòng)刪除。
- binlog_do_db:binlog記錄哪些數(shù)據(jù)庫。如果需要配置多個(gè)庫,如例子中配置多項(xiàng)。切勿使用逗號分隔。
配置文件修改完畢后保存并重啟MySQL。然后進(jìn)入MySQL命令行,驗(yàn)證是否已啟用binlog:
mysql> show variables like '%bin%';
+--------------------------------------------+--------------------------------+
| Variable_name | Value |
+--------------------------------------------+--------------------------------+
| bind_address | * |
| binlog_cache_size | 32768 |
| binlog_checksum | CRC32 |
| binlog_direct_non_transactional_updates | OFF |
| binlog_error_action | ABORT_SERVER |
| binlog_format | ROW |
| binlog_group_commit_sync_delay | 0 |
| binlog_group_commit_sync_no_delay_count | 0 |
| binlog_gtid_simple_recovery | ON |
| binlog_max_flush_queue_time | 0 |
| binlog_order_commits | ON |
| binlog_row_image | FULL |
| binlog_rows_query_log_events | OFF |
| binlog_stmt_cache_size | 32768 |
| binlog_transaction_dependency_history_size | 25000 |
| binlog_transaction_dependency_tracking | COMMIT_ORDER |
| innodb_api_enable_binlog | OFF |
| innodb_locks_unsafe_for_binlog | OFF |
| log_bin | ON |
| log_bin_basename | /var/lib/mysql/mysql-bin |
| log_bin_index | /var/lib/mysql/mysql-bin.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| log_statements_unsafe_for_binlog | ON |
| max_binlog_cache_size | 18446744073709547520 |
| max_binlog_size | 1073741824 |
| max_binlog_stmt_cache_size | 18446744073709547520 |
| sql_log_bin | ON |
| sync_binlog | 1 |
+--------------------------------------------+--------------------------------+
29 rows in set (0.00 sec)
發(fā)現(xiàn)log_bin
的值為ON
。binlog配置已生效。
初始化MySQL 源數(shù)據(jù)表
到這里MySQL環(huán)境已經(jīng)配置完畢。接下來開始準(zhǔn)備測試表和數(shù)據(jù)。
create database demo character set utf8mb4;
use demo;
create table student(`id` int primary key, `name` varchar(128), `age` int);
這里創(chuàng)建了演示數(shù)據(jù)庫demo和一張student表。
使用Java代碼讀取CDC數(shù)據(jù)流
到這一步我們開始使用Flink程序來獲取CDC數(shù)據(jù)流。
使用傳統(tǒng)MySQL 數(shù)據(jù)源方式
首先需要引入Flink Connector MySQL CDC依賴。
<dependency>
<groupId>com.alibaba.ververica</groupId>
<artifactId>flink-connector-mysql-cdc</artifactId>
<version>1.3.0</version>
</dependency>
然后使用Table API編寫程序。這里我們僅僅將CDC數(shù)據(jù)流配置為數(shù)據(jù)源,然后將CDC數(shù)據(jù)流的內(nèi)容打印出來。
val env = StreamExecutionEnvironment.getExecutionEnvironment
// 使用MySQLSource創(chuàng)建數(shù)據(jù)源
// 同時(shí)指定StringDebeziumDeserializationSchema,將CDC轉(zhuǎn)換為String類型輸出
val sourceFunction = MySQLSource.builder().hostname("your-ip").port(3306)
.databaseList("demo").username("root").password("123456")
.deserializer(new StringDebeziumDeserializationSchema).build();
// 單并行度打印,避免輸出亂序
env.addSource(sourceFunction).print.setParallelism(1)
env.execute()
此時(shí)我們插入一條數(shù)據(jù):
insert into student values(2, 'kate', 28);
可以看到程序有如下輸出:
SourceRecord{sourcePartition={server=mysql_binlog_source}, sourceOffset={ts_sec=1618390979, file=mysql-bin.000003, pos=885, row=1, server_id=1, event=2}} ConnectRecord{topic='mysql_binlog_source.demo.student', kafkaPartition=null, key=Struct{id=2}, keySchema=Schema{mysql_binlog_source.demo.student.Key:STRUCT}, value=Struct{after=Struct{id=2,name=kate,age=28},source=Struct{version=1.4.1.Final,connector=mysql,name=mysql_binlog_source,ts_ms=1618390979000,db=demo,table=student,server_id=1,file=mysql-bin.000003,pos=1011,row=0,thread=2},op=c,ts_ms=1618391175254}, valueSchema=Schema{mysql_binlog_source.demo.student.Envelope:STRUCT}, timestamp=null, headers=ConnectHeaders(headers=)}
使用SQL
接下來我們使用更為簡潔的SQL方式。
首先引入Flink SQL必須的依賴。需要注意的是,這里使用blink planner。本例子中使用Scala語言編寫,所以引入了Scala相關(guān)依賴。
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-api-scala-bridge_${scala.binary.version}</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-planner-blink_${scala.binary.version}</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-streaming-scala_${scala.binary.version}</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.ververica</groupId>
<artifactId>flink-connector-mysql-cdc</artifactId>
<version>1.3.0</version>
</dependency>
編寫如下所示的程序代碼:
// 創(chuàng)建Blink Streaming的TableEnvironment
val bsSettings = EnvironmentSettings.newInstance().useBlinkPlanner().inStreamingMode().build()
val tableEnvironment = TableEnvironment.create(bsSettings)
// 創(chuàng)建表,connector使用mysql-cdc
tableEnvironment.executeSql("CREATE TABLE mysql_binlog (id INT NOT NULL, name STRING, age INT) WITH ('connector' = 'mysql-cdc', 'hostname' = '10.180.210.135', 'port' = '3306', 'username' = 'root', 'password' = '123456', 'database-name' = 'demo', 'table-name' = 'student')")
// 創(chuàng)建下游數(shù)據(jù)表,這里使用print類型的connector,將數(shù)據(jù)直接打印出來
tableEnvironment.executeSql("CREATE TABLE sink_table (id INT NOT NULL, name STRING, age INT) WITH ('connector' = 'print')")
// 將CDC數(shù)據(jù)源和下游數(shù)據(jù)表對接起來
tableEnvironment.executeSql("INSERT INTO sink_table SELECT id, name, age FROM mysql_binlog")
接下來可以執(zhí)行insert
語句插入數(shù)據(jù),控制臺會打印出數(shù)據(jù)的變化。
例如我們依次執(zhí)行:
insert into student values(1,'paul',20);
update student set age=30 where id=1;
delete from student where id=1;
在控制臺可以得到如下輸出:
+I(1,paul,20)
-U(1,paul,20)
+U(1,paul,30)
-D(1,paul,30)
使用SQL Client讀取CDC
相比較創(chuàng)建一個(gè)Java項(xiàng)目以jar包的方式創(chuàng)建作業(yè),F(xiàn)llink提供了一個(gè)更為簡單的方式:使用 SQL Client。接下來我們開始配置SQL Client環(huán)境。
配置Flink環(huán)境
在Flink SQL Client使用CDC功能之前,我們需要將相關(guān)依賴放入Flink目錄。
訪問https://mvnrepository.com/artifact/com.alibaba.ververica/flink-connector-mysql-cdc/
,下載flink-connector-mysql-cdc
jar包,復(fù)制到flink安裝位置的lib
目錄中。
啟動(dòng)Flink SQL Client
這里SQL Client在standalone集群上運(yùn)行。
官網(wǎng)配置方式鏈接:https://ci.apache.org/projects/flink/flink-docs-master/docs/dev/table/sqlclient/#getting-started,簡單來說是執(zhí)行Flink安裝目錄如下兩個(gè)命令:
./bin/start-cluster.sh
./bin/sql-client.sh embedded
如果沒有問題,此時(shí)可以進(jìn)入SQL Client。
執(zhí)行如下SQL(和上一章"使用SQL"使用的語句相同):
CREATE TABLE mysql_binlog (
id INT NOT NULL,
name STRING,
age INT
) WITH (
'connector' = 'mysql-cdc',
'hostname' = 'localhost',
'port' = '3306',
'username' = 'root',
'password' = '123456',
'database-name' = 'demo',
'table-name' = 'student'
);
CREATE TABLE sink_table (
id INT NOT NULL,
name STRING,
age INT
) WITH (
'connector' = 'print'
);
INSERT INTO sink_table SELECT id, name, age FROM mysql_binlog;
然后在MySQL命令行執(zhí)行些insert語句插入數(shù)據(jù)。需要注意的是sink_table
的輸出是無法在SQL client上面查看的。需要打開Flink Web UI的Task Managers頁面的stdout標(biāo)簽。可以找到類似如下輸出:
+I(1,paul,20)
-U(1,paul,20)
+U(1,paul,30)
-D(1,paul,30)
Flink 已經(jīng)成功捕獲到MySQL的數(shù)據(jù)變更。