返回顶部
首页 > 资讯 > 数据库 >使用Flink CDC将Mysql中的数据实时同步到ES
  • 301
分享到

使用Flink CDC将Mysql中的数据实时同步到ES

mysqlflinkelasticsearch 2023-09-01 12:09:02 301人浏览 安东尼
摘要

前言 最近公司要搞搜索,需要把Mysql中的数据同步到es中来进行搜索,由于公司已经搭建了flink集群,就打算用flink来做这个同步。本来以为很简单,跟着官网文档走就好了,结果没想到折腾了将近一周

前言

最近公司要搞搜索,需要把Mysql中的数据同步到es中来进行搜索,由于公司已经搭建了flink集群,就打算用flink来做这个同步。本来以为很简单,跟着官网文档走就好了,结果没想到折腾了将近一周的时间……

我也是没想到,这玩意网上资源竟然这么少,找到的全部都是通过flink sql-client实现的,但这有个问题,当fink集群重启,JOB就没有了,没有办法通过savePointing来恢复。所以还是记录下。

代码

直接上代码:

public static void main(String[] args) throws Exception {        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();        env.enableCheckpointing(3000);    env.getCheckpointConfig().setCheckpointStorage("file:///tmp/flink/savepointings");        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);        tableEnv.executeSql("CREATE TABLE orders (\n" +                "   order_id INT,\n" +                "   order_date TIMESTAMP(0),\n" +                "   customer_name STRING,\n" +                "   price DECIMAL(10, 5),\n" +                "   product_id INT,\n" +                "   order_status BOOLEAN,\n" +                "   PRIMARY KEY (order_id) NOT ENFORCED\n" +                " ) WITH (\n" +                "   'connector' = 'mysql-cdc',\n" +                "   'hostname' = 'localhost',\n" +                "   'port' = '3306',\n" +                "   'username' = 'root',\n" +                "   'passWord' = '123456',\n" +                "   'database-name' = 'mydb',\n" +                "   'table-name' = 'orders'\n" +                " );").await();        tableEnv.executeSql("CREATE TABLE products (\n" +                "    id INT,\n" +                "    name STRING,\n" +                "    description STRING,\n" +                "    PRIMARY KEY (id) NOT ENFORCED\n" +                "  ) WITH (\n" +                "    'connector' = 'mysql-cdc',\n" +                "    'hostname' = 'localhost',\n" +                "    'port' = '3306',\n" +                "    'username' = 'root',\n" +                "    'password' = '123456',\n" +                "    'database-name' = 'mydb',\n" +                "    'table-name' = 'products'\n" +                "  );").await();        tableEnv.executeSql("CREATE TABLE enriched_orders (\n" +                "   order_id INT,\n" +                "   order_date TIMESTAMP(0),\n" +                "   customer_name STRING,\n" +                "   price DECIMAL(10, 5),\n" +                "   product_id INT,\n" +                "   order_status BOOLEAN,\n" +                "   product_name STRING,\n" +                "   product_description STRING,\n" +                "   PRIMARY KEY (order_id) NOT ENFORCED\n" +                " ) WITH (\n" +                "     'connector' = 'elasticsearch-7',\n" +                "     'hosts' = 'Http://localhost:9200',\n" +                "     'index' = 'enriched_orders_lhc'\n" +                " );");        tableEnv.executeSql("INSERT INTO enriched_orders\n" +                " SELECT o.*, p.name, p.description\n" +                " FROM orders AS o\n" +                " LEFT JOIN products AS p ON o.product_id = p.id");        env.execute("Mysql to ES");    }

来源地址:https://blog.csdn.net/lhcnicholas/article/details/129854091

您可能感兴趣的文档:

--结束END--

本文标题: 使用Flink CDC将Mysql中的数据实时同步到ES

本文链接: https://lsjlt.com/news/388456.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作