当前位置:网站首页>Tableapi & SQL and MySQL insert data of Flink
Tableapi & SQL and MySQL insert data of Flink
2022-07-01 15:01:00 【Dragon man who can't fly】
Use Tbale&SQL And Flink JDBC Connector inserts data into MYSQL Database table
Sample environment
java.version: 1.8.x
flink.version: 1.11.1
kafka:2.11Sample data source ( Project code cloud download )
Flink System examples And Build development environment and data
Sample module (pom.xml)
Flink System examples And TableAPI & SQL And Sample module
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 Use Tbale&SQL And Flink JDBC Connector inserts data into MYSQL Database table
*/
public class InsertToMysql {
/**
Official reference :https://ci.apache.org/projects/flink/flink-docs-release-1.11/dev/table/connectors/jdbc.html
*/
//flink-jdbc-1.11.1 How to write it , All attribute names are in JdbcTableSourceSinkFactory The factory class defines
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', -- The database needs to be established \n" +
" 'connector.driver' = 'com.mysql.jdbc.Driver', \n" +
" 'connector.table' = 'users', -- Known tables \n" +
" 'connector.username' = 'root',\n" +
" 'connector.password' = 'password' \n" +
")";
public static void main(String[] args) throws Exception {
// structure StreamExecutionEnvironment
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
// structure EnvironmentSettings And designate Blink Planner
EnvironmentSettings bsSettings = EnvironmentSettings.newInstance().useBlinkPlanner().inStreamingMode().build();
// structure StreamTableEnvironment
StreamTableEnvironment tEnv = StreamTableEnvironment.create(env, bsSettings);
// register mysql Data dimension table
tEnv.executeSql(table_sql);
// perform SQL,id=0 It's because of id Fields are self incrementing primary keys , by 0 be mysql Recognition will default to self increment instead of
String sql = "insert into my_users (id,name,age,status) values(0,'tom',31,0)";
// The first way : Direct execution sql
// TableResult tableResult = tEnv.executeSql(sql);
// The second way : Declare a collection of operations to perform sql
StatementSet stmtSet = tEnv.createStatementSet();
stmtSet.addInsertSql(sql);
TableResult tableResult = stmtSet.execute();
tableResult.print();
}
}
Print the results
+-------------------------------------------+
| default_catalog.default_database.my_users |
+-------------------------------------------+
| -1 |
+-------------------------------------------+
1 row in set边栏推荐
- 写在Doris毕业后的第一天
- Hidden rules of the workplace that must be understood before 30
- 微服务追踪SQL(支持Isto管控下的gorm查询追踪)
- 关于重载运算符的再整理
- Build your own website (14)
- 首届技术播客月开播在即
- Solid smart contract development - easy to get started
- Fix the failure of idea global search shortcut (ctrl+shift+f)
- Music player development example (can be set up)
- 微信公众号订阅消息 wx-open-subscribe 的实现及闭坑指南
猜你喜欢

opencv学习笔记五--文件扫描+OCR文字识别

openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题

Word2vec yyds dry goods inventory

skywalking 6.4 分布式链路跟踪 使用笔记
SQL常用的四个排序函数梳理
![opencv学习笔记六--图像特征[harris+SIFT]+特征匹配](/img/50/5c8adacea78e470c255070c8621ddd.png)
opencv学习笔记六--图像特征[harris+SIFT]+特征匹配

idea中新建的XML文件变成普通文件的解决方法.

手把手带你入门 API 开发

Official announcement: Apache Doris graduated successfully and became the top project of ASF!

The solution to turn the newly created XML file into a common file in idea
随机推荐
Written on the first day after Doris graduated
Flink 系例 之 TableAPI & SQL 与 MYSQL 分组统计
JVM第二话 -- JVM内存模型以及垃圾回收
建立自己的网站(14)
JVM performance tuning and practical basic theory part II
Build MySQL master-slave server under Ubuntu 14.04
Markdown编辑器使用基本语法
竣达技术丨室内空气环境监测终端 pm2.5、温湿度TVOC等多参数监测
[leetcode] 16. The sum of the nearest three numbers
智能运维实战:银行业务流程及单笔交易追踪
Build your own website (14)
写在Doris毕业后的第一天
这3款在线PS工具,得试试
ArrayList 扩容详解,扩容原理[通俗易懂]
对于编程思想和能力有重大提升的书有哪些?
In hot summer, please put away this safe gas use guide!
【14. 区间和(离散化)】
互联网医院系统源码 医院小程序源码 智慧医院源码 在线问诊系统源码
TypeScript: let
Digital transformation: data visualization enables sales management