监控mysql 一个或者多个库中的 一个或者多个表中的
数据变化(增删改)
首先确定使用的flink 版本:
FlinkCDC 2.0 版本对应 Flink 1.13
在pom文件中添加 FIinkCDC的依赖:
<dependency>
<groupId>com.alibaba.ververica</groupId>
<artifactId>flink-connector-mysql-cdc</artifactId>
<version>1.1.0</version>
</dependency>
Demo:
import com.alibaba.ververica.cdc.connectors.mysql.MySQLSource
import com.alibaba.ververica.cdc.debezium.{DebeziumSourceFunction, StringDebeziumDeserializationSchema}
import com.zw.bigdata.qy.logs.config.EnvConfig
import com.zw.bigdata.qy.logs.util.FlinkExecutionEnvUtil
import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.api.scala._
object flinkcdcDemo{
def main(args: Array[String]): Unit = {
//调用程序封装好的创建env对象的类
val checkpointPath=EnvConfig.getConfigValue("flink.checkout.path")
val driverName = EnvConfig.getConfigValue("com.xxx.driverName")
val hostName = EnvConfig.getConfigValue("com.xxx.host")
val userName = EnvConfig.getConfigValue("com.xxx.userName")
val password = EnvConfig.getConfigValue("com.xxx.password")
val dataBase = EnvConfig.getConfigValue("com.xxx.databse")
val orderTableName = EnvConfig.getConfigValue("com.xxx.orderTableName")
//创建流程序入口
val env: StreamExecutionEnvironment = FlinkExecutionEnvUtil.getStreamEnv(checkpointPath)
env.setParallelism(1) //便于测试使用
val sourceFunction: DebeziumSourceFunction[String] = MySQLSource.builder[String]()
.hostname(hostName)
.port(3306)
.username(userName)
.password(password)
.databaseList(dataBase)
.tableList(orderTableName)
.deserializer(new StringDebeziumDeserializationSchema)
.build()
val value = env.addSource(sourceFunction)
value.print()
env.execute("xhna")
}
}