Flink CDC 1.18.1 Oracle 数据同步到postgresql

1、下载flink-1.18.1-bin-scala_2.12.tgz,linux通过:

  wget https://archive.apache.org/dist/flink/flink-1.18.1/flink-1.18.1-bin-scala_2.12.tgz

2、oracle11g客户端安装,下载:

instantclient-basic-linux.x64-11.2.0.4.0.zip
instantclient-sdk-linux.x64-11.2.0.4.0.zip
instantclient-sqlplus-linux.x64-11.2.0.4.0.zip

以上文件,在ORACLE网站下载。

3、配置oracle客户端:

[root@wn1 ~]# ls /usr/local/
[root@wn1 ~]# cp instantclient-* /usr/local
[root@wn1 ~]# cd /usr/local
[root@wn1 ~]# unzip instantclient-basic-linux.x64-11.2.0.4.0.zip
[root@wn1 ~]# unzip instantclient-sdk-linux.x64-11.2.0.4.0.zip
[root@wn1 ~]# unzip instantclient-sqlplus-linux.x64-11.2.0.4.0.zip
[root@wn1 ~]# mv instantclient_11_2 oracle_11
[root@wn1 ~]# rm instantclient-*
[root@wn1 ~]# vi /etc/profile
#增加以下内容
export ORACLE_HOME=/usr/local/oracle_11
export PATH=.:${PATH}:$ORACLE_HOME
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$ORACLE_HOME

#保存退出,执行
[root@wn1 ~]# source /etc/profile

#修改ld配置
[root@wn1 ~]#  vi /etc/ld.so.conf.d/oracle.conf
#写入内容
/usr/local/oracle_11
#保存退出,执行
[root@wn1 ~]#ldconfig
#配置oracle连接参数
[root@wn1 ~]# mkdir -p network/admin/
[root@wn1 ~]# cd network/admin/
#找一个tnsnames.ora文件,直接上传到服务器
[root@wn1 ~]# cp /root/tnsnames.ora ./

#测试连接
[root@wn1 ~]# sqlplus sys/manager@//192.168.56.1/orcl
SQL*Plus: Release 11.2.0.4.0 Production on Sun Mar 24 18:04:15 2024

Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL>


4、配置oracle数据库,启用归档日志,这步需要参考:https://nightlies.apache.org/flink/flink-cdc-docs-release-3.0/zh/docs/connectors/legacy-flink-cdc-sources/oracle-cdc/

5、下载oracle cdc 连接器

wget https://maven.aliyun.com/repository/public/com/ververica/flink-sql-connector-oracle-cdc/3.0.1/flink-sql-connector-oracle-cdc-3.0.1.jar

解压:

tar zxvf flink-1.18.1-bin-scala_2.12.tgz

将flink-sql-connector-oracle-cdc-3.0.1.jar复制到flink-1.18.1/lib目录中

6、下载 flink-connector-jdbc-3.1.1-1.17.jar,postgresql-42.7.3.jar

https://repo1.maven.org/maven2/org/apache/flink/flink-connector-jdbc/3.1.1-1.17/flink-connector-jdbc-3.1.1-1.17.jar

https://jdbc.postgresql.org/download/postgresql-42.7.3.jar

将jar包复制到flink-1.18.1/lib目录中

7、安装postgresql就不说了,相信你已经有了数据库了

8、修改Flink的配置文件 /home/flink/flink-1.18.1/conf/flink-conf.yaml ,主要是各种服务的绑定地址,默认为localhost,统统改为0.0.0.0,如:rest.address: 0.0.0.0 #localhost
9、启动

[flink@cn1 bin]$ ./start-cluster.sh
[flink@cn1 bin]$ ./sql-client.sh
Flink SQL>
#创建ORACLE源表

SET execution.checkpointing.interval = 3s;

create table SYS_DIC_DEPT
(
  DEPT_CODE       STRING,
  DEPT_NAME       STRING,
  DEPT_ADDR       STRING,
  DEPT_MEMO       STRING,
  DEPT_FLAG       STRING,
  DEPT_GZYZLTJFLAG STRING,
  DEPT_UPPER       STRING,
  PRIMARY KEY (DEPT_CODE) NOT ENFORCED
)
 WITH (
   'connector' = 'oracle-cdc',
   'hostname' = '192.168.56.1',
   'port' = '1521',
   'username' = 'username',
   'password' = '123456',
   'database-name' = 'ORCL',
   'schema-name' = 'schema-name',
    'table-name' = 'table-name',
    'debezium.log.mining.strategy'='online_catalog',
	'debezium.log.mining.continuos.mine'='true',
	'debezium.snapshot.mode' = 'initial',
	'debezium.database.tablename.case.insensitive'='true'
 );

 Flink SQL> select * from SYS_DIC_DEPT;

如果看不到数据,请检查ORACLE的字段是否全部大写

10、创建PG Sink:

Flink SQL>
create table sys_dic_dept_sink
(
  dept_code       STRING,
  dept_name       STRING,
  dept_addr       STRING,
  dept_memo       STRING,
  dept_flag       STRING,
  dept_gzyzltjflag STRING,
  dept_upper       STRING,
  PRIMARY KEY (dept_code) NOT ENFORCED
)
with(
'connector' = 'jdbc',
'url' = 'jdbc:postgresql://192.168.56.90:5432/postgres?currentSchema=public', 
'username' = 'postgres',
'password' = '123456',  
'table-name' = 'sys_dic_dept'
);

11、抽数据

Flink SQL> insert into sys_dic_dept_sink select * from SYS_DIC_DEPT;

12、查看任务执行 http://192.168.56.90:8081/#/job/running

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Flink CDC(Change Data Capture)是一种数据同步技术,可以从源数据库中捕获变更数据并将其同步到目标数据库中。DorisDB是一款分布式数据仓库,支持海量数据的存储和查询分析。下面以将数据从DorisDB同步到DorisDB为例,介绍如何使用Flink CDC实现数据同步。 1. 准备工作 在开始之前,需要安装好以下工具和环境: - DorisDB - Flink - Flink CDC 2. 创建数据源 首先需要创建一个数据源,用于从DorisDB中读取数据。可以使用Flink的JDBCInputFormat来读取DorisDB中的数据。在Flink中,可以使用以下代码创建一个JDBCInputFormat: ``` JDBCInputFormat jdbcInputFormat = JDBCInputFormat.buildJDBCInputFormat() .setDrivername(driverName) .setDBUrl(dbUrl) .setUsername(username) .setPassword(password) .setQuery("SELECT * FROM table") .finish(); ``` 其中,driverName、dbUrl、username和password是DorisDB的连接信息,"SELECT * FROM table"是要读取的表的SQL语句。 3. 创建数据同步任务 接下来需要创建一个Flink数据流任务,用于将从DorisDB中读取的数据同步到另一个DorisDB中。可以使用Flink的DataStream API来实现数据同步。以下是一个示例代码: ``` StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); DataStream<Row> sourceStream = env.createInput(jdbcInputFormat); DataStream<Row> sinkStream = sourceStream.map(new MapFunction<Row, Row>() { @Override public Row map(Row value) throws Exception { // 对数据进行转换 return value; } }); DorisDBOutputFormat dorisDBOutputFormat = new DorisDBOutputFormat(); dorisDBOutputFormat.setDrivername(driverName); dorisDBOutputFormat.setDBUrl(dbUrl); dorisDBOutputFormat.setUsername(username); dorisDBOutputFormat.setPassword(password); dorisDBOutputFormat.setTable(table); dorisDBOutputFormat.setBatchSize(batchSize); sinkStream.writeUsingOutputFormat(dorisDBOutputFormat); env.execute(); ``` 其中,sourceStream是从DorisDB中读取的数据流,sinkStream是经过转换后要写入到DorisDB的数据流。可以使用map函数对数据进行转换。DorisDBOutputFormat是一个自定义的输出格式,用于将数据写入到DorisDB中。在这个示例代码中,DorisDBOutputFormat的batchSize属性设置为1000,表示每1000条数据进行一次批量写入。 4. 运行数据同步任务 将上述代码保存为一个Java程序,并使用Flink命令行工具提交任务即可开始数据同步。在执行过程中,Flink CDC会自动监控DorisDB中的数据变更,将新增、修改、删除等操作同步到目标数据库中。 总的来说,使用Flink CDC实现DorisDB数据同步是一种高效、可靠的方式。它不仅可以帮助用户快速实现数据同步,还可以提高数据的实时性和准确性,为企业的数据分析和决策提供有力支持。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值