当前位置:网站首页>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
边栏推荐
- TypeScript:const
- 241. Design priorities for operational expressions
- Chapter 4 of getting started with MySQL: creation, modification and deletion of data tables
- [dynamic programming] interval dp:p1005 matrix retrieval
- The first word of JVM -- detailed introduction to JVM and analysis of runtime data area
- DirectX修复工具V4.1公测![通俗易懂]
- Hidden rules of the workplace that must be understood before 30
- C learning notes (5) class and inheritance
- Opencv mat class
- Ubuntu 14.04下搭建MySQL主从服务器
猜你喜欢
对于编程思想和能力有重大提升的书有哪些?
Vnctf2022 open web gocalc0
Opencv interpolation mode
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
JVM second conversation -- JVM memory model and garbage collection
idea中新建的XML文件变成普通文件的解决方法.
[dynamic programming] interval dp:p1005 matrix retrieval
Salesforce、约翰霍普金斯、哥大 | ProGen2: 探索蛋白语言模型的边界
一波三折,终于找到src漏洞挖掘的方法了【建议收藏】
Internet hospital system source code hospital applet source code smart hospital source code online consultation system source code
随机推荐
Opencv interpolation mode
【15. 区间合并】
[stage life summary] I gave up the postgraduate entrance examination and participated in the work. I have successfully graduated and just received my graduation certificate yesterday
IDEA全局搜索快捷键(ctrl+shift+F)失效修复
solidty-基础篇-基础语法和定义函数
Build MySQL master-slave server under Ubuntu 14.04
[leetcode 324] swing sorting II thinking + sorting
MongoDB第二话 -- MongoDB高可用集群实现
leetcode:329. Longest increasing path in matrix
竣达技术丨多台精密空调微信云监控方案
Markdown编辑器使用基本语法
MIT team used graph neural network to accelerate the screening of amorphous polymer electrolytes and promote the development of next-generation lithium battery technology
[Verilog quick start of Niuke series] ~ multi function data processor, calculate the difference between two numbers, use generate... For statement to simplify the code, and use sub modules to realize
首届技术播客月开播在即
[dynamic programming] p1004 grid access (four-dimensional DP template question)
【锁】Redis锁 处理并发 原子性
Minimum spanning tree and bipartite graph in graph theory (acwing template)
Written on the first day after Doris graduated
音乐播放器开发实例(可毕设)
2022-2-15 learning xiangniuke project - Section 4 business management