当前位置:网站首页>Flink 系例 之 TableAPI & SQL 与 MYSQL 插入数据
Flink 系例 之 TableAPI & SQL 与 MYSQL 插入数据
2022-07-01 14:54:00 【不会飞的小龙人】
使用 Tbale&SQL 与 Flink JDBC 连接器将数据插入 MYSQL 数据库表
示例环境
java.version: 1.8.x
flink.version: 1.11.1
kafka:2.11
示例数据源 (项目码云下载)
示例模块 (pom.xml)
Flink 系例 之 TableAPI & SQL 与 示例模块
InsertToMysql.java
package com.flink.examples.mysql;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.EnvironmentSettings;
import org.apache.flink.table.api.StatementSet;
import org.apache.flink.table.api.TableResult;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
/**
* @Description 使用Tbale&SQL与Flink JDBC连接器将数据插入MYSQL数据库表
*/
public class InsertToMysql {
/**
官方参考:https://ci.apache.org/projects/flink/flink-docs-release-1.11/dev/table/connectors/jdbc.html
*/
//flink-jdbc-1.11.1写法,所有属性名在JdbcTableSourceSinkFactory工厂类中定义
static String table_sql =
"CREATE TABLE my_users (\n" +
" id BIGINT,\n" +
" name STRING,\n" +
" age INT,\n" +
" status INT,\n" +
" PRIMARY KEY (id) NOT ENFORCED\n" +
") WITH (\n" +
" 'connector.type' = 'jdbc',\n" +
" 'connector.url' = 'jdbc:mysql://192.168.110.35:3306/flink?useUnicode=true&characterEncoding=utf-8', -- 数据库需建立 \n" +
" 'connector.driver' = 'com.mysql.jdbc.Driver', \n" +
" 'connector.table' = 'users', -- 已知的表 \n" +
" 'connector.username' = 'root',\n" +
" 'connector.password' = 'password' \n" +
")";
public static void main(String[] args) throws Exception {
//构建StreamExecutionEnvironment
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//构建EnvironmentSettings 并指定Blink Planner
EnvironmentSettings bsSettings = EnvironmentSettings.newInstance().useBlinkPlanner().inStreamingMode().build();
//构建StreamTableEnvironment
StreamTableEnvironment tEnv = StreamTableEnvironment.create(env, bsSettings);
//注册mysql数据维表
tEnv.executeSql(table_sql);
//执行SQL,id=0是因id字段为自增主键,为0则mysql识别会默认自增值代替
String sql = "insert into my_users (id,name,age,status) values(0,'tom',31,0)";
// 第一种方式:直接执行sql
// TableResult tableResult = tEnv.executeSql(sql);
//第二种方式:声明一个操作集合来执行sql
StatementSet stmtSet = tEnv.createStatementSet();
stmtSet.addInsertSql(sql);
TableResult tableResult = stmtSet.execute();
tableResult.print();
}
}
打印结果
+-------------------------------------------+
| default_catalog.default_database.my_users |
+-------------------------------------------+
| -1 |
+-------------------------------------------+
1 row in set
边栏推荐
- The data in the database table recursively forms a closed-loop data. How can we get these data
- 深度分析数据在内存中的存储形式
- 竣达技术丨多台精密空调微信云监控方案
- Tensorflow 2. X realizes iris classification
- 音乐播放器开发实例(可毕设)
- One of the data Lake series | you must love to read the history of minimalist data platforms, from data warehouse, data lake to Lake warehouse
- [零基础学IoT Pwn] 复现Netgear WNAP320 RCE
- JVM第一话 -- JVM入门详解以及运行时数据区分析
- Problem note - Oracle 11g uninstall
- Don't want to knock the code? Here comes the chance
猜你喜欢
JVM performance tuning and practical basic theory part II
Official announcement: Apache Doris graduated successfully and became the top project of ASF!
In hot summer, please put away this safe gas use guide!
opencv学习笔记四--银行卡号识别
After twists and turns, I finally found the method of SRC vulnerability mining [recommended collection]
Problem note - Oracle 11g uninstall
Rearrangement of overloaded operators
Word2vec yyds dry goods inventory
[Verilog quick start of Niuke question series] ~ use functions to realize data size conversion
C learning notes (5) class and inheritance
随机推荐
[dynamic programming] p1004 grid access (four-dimensional DP template question)
ArrayList 扩容详解,扩容原理[通俗易懂]
Chapter 4 of getting started with MySQL: creation, modification and deletion of data tables
These three online PS tools should be tried
关于重载运算符的再整理
【锁】Redis锁 处理并发 原子性
tensorflow2-savedmodel convert to pb(frozen_graph)
Reorganize the trivial knowledge points at the end of the term
Problem note - Oracle 11g uninstall
[dynamic programming] interval dp:p1005 matrix retrieval
Digital transformation: data visualization enables sales management
Markdown编辑器使用基本语法
音乐播放器开发实例(可毕设)
[lock] redis lock handles concurrency atomicity
炎炎夏日,这份安全用气指南请街坊们收好!
Don't want to knock the code? Here comes the chance
[14. Interval sum (discretization)]
【15. 区间合并】
手把手带你入门 API 开发
Opencv interpolation mode