当前位置:   article > 正文

FlinkCDC实时监控Mysql_com.alibaba.ververica.cdc.connectors.mysql.mysqlso

com.alibaba.ververica.cdc.connectors.mysql.mysqlsource;

maven依赖

import com.alibaba.ververica.cdc.connectors.mysql.MySQLSource;
import com.alibaba.ververica.cdc.debezium.DebeziumSourceFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
  • 1
  • 2
  • 3
  • 4

调用MysqlSource方法

//创建flinkcdc监控Mysqlblinlog日志
        DebeziumSourceFunction<String> sourceFunction = MySQLSource.<String>builder()
               	//数据库地址
                .hostname("localhost")
                //端口号
                .port(3306)
                //用户名
                .username("zhang")
                //密码
                .password("1234")
                //监控的数据库
                .databaseList("school")
                //监控的表名,格式数据库.表名
                .tableList("school.person")
                //虚拟化方式
                .deserializer(new PersonSchema())
                //时区
                .serverTimeZone("UTC")
                .build();
        //创建flink流处理执行环境
        StreamExecutionEnvironment environment = StreamExecutionEnvironment.getExecutionEnvironment();
        //设置并行度
        environment.setParallelism(1);
        //加载source
        DataStream<String> source = environment.addSource(sourceFunction);
        //打印结果
        source.print();
        //执行任务
        environment.execute();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

目前最高支持8.0的mysql数据库,8.0以上可能会因为无法获取密钥报错。

blinlog日志序列化成对象字符串

import com.alibaba.ververica.cdc.debezium.DebeziumDeserializationSchema;

import io.debezium.data.Envelope;
import org.apache.flink.api.common.typeinfo.BasicTypeInfo;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.util.Collector;
import org.apache.kafka.connect.data.Struct;
import org.apache.kafka.connect.source.SourceRecord;
//实现接口DebeziumDeserializationSchema把日志进行解析
public class PersonSchema implements DebeziumDeserializationSchema<String> {
    @Override
    public void deserialize(SourceRecord source, Collector<String> out) throws Exception {
        String topic = source.topic();
        String[] split = topic.split("[.]");
        String database = split[1];
        String table = split[2];
        //获取操作类型
        Envelope.Operation operation = Envelope.operationFor(source);
        //获取数据本身
        Struct struct = (Struct) source.value();
        //根据每个字段名获取相应的字段类型
        Struct after = struct.getStruct("after");
        Object pid = after.get("pid");
        Object pname = after.get("pname");
        Object psex = after.get("psex");
        Object page = after.get("page");
        //拼接成字符串(添加数据)
        String file=pid.toString()+"|"+pname.toString()+"|"+psex.toString()+"|"+page.toString();
        Struct before = struct.getStruct("before");
        Object upid = before.get("pid");
        Object upname = before.get("pname");
        Object upsex = before.get("psex");
        Object upage = before.get("page");
        //修改数据的拼接
        String ufile=pid.toString()+"|"+pname.toString()+"|"+psex.toString()+"|"+page.toString();
        if (after!=null&&before==null){
            String datam="insert"+"|"+file;
            out.collect(datam);
        }else {
            String datan="update"+"|"+ufile;
            out.collect(datan);
        }

}

    @Override
    public TypeInformation<String> getProducedType() {
    //返回字符串类型
        return BasicTypeInfo.STRING_TYPE_INFO;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/811566
推荐阅读
相关标签
  

闽ICP备14008679号