Flink DataStream使用JdbcInputFormat方式从Mysql读取数据

1. 背景

官网Jdbc DataStream Connector只有Sink,没有Source,所以需要用其它方式从Mysql查询数据

2. 查看Mysql表数据

mysql> select * from person;
+------+------+
| id   | name |
+------+------+
|    1 | yi   |
|    2 | er   |
+------+------+
2 rows in set (0.00 sec)

mysql>

3. Flink DataStream查询数据程序

import org.apache.flink.api.common.typeinfo.BasicTypeInfo
import org.apache.flink.api.java.typeutils.RowTypeInfo
import org.apache.flink.connector.jdbc.JdbcInputFormat
import org.apache.flink.streaming.api.scala.{DataStream, StreamExecutionEnvironment, createTypeInformation}
import org.apache.flink.types.Row

object FlinkTest {

  def main(args: Array[String]): Unit = {

    val senv:StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
    senv.setParallelism(1)

    val mysqlPersonDataInput:DataStream[Row] = senv.createInput(JdbcInputFormat.buildJdbcInputFormat()
      .setDrivername("com.mysql.cj.jdbc.Driver")
      .setDBUrl("jdbc:mysql://192.168.8.115:3306/test?serverTimezone=GMT%2B8&useSSL=false")
      .setUsername("root")
      .setPassword("Root_123")
      .setQuery("select id, name from person")
      .setRowTypeInfo(new RowTypeInfo(
        BasicTypeInfo.LONG_TYPE_INFO,
        BasicTypeInfo.STRING_TYPE_INFO))
      .finish())

    mysqlPersonDataInput.executeAndCollect().foreach(println)
  }

}

运行程序,结果如下:

+I[1, yi]
+I[2, er]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用FlinkJDBC连接器来读取MySQL数据,并使用Flink SQL对数据进行分析和转换,然后再使用JDBC连接器将结果写入另一个MySQL表中。 以下是一个简单的示例代码: ```java //创建Flink环境 StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env); //创建MySQL连接器 JdbcConnectionOptions connectionOptions = JdbcConnectionOptions.builder() .withUrl("jdbc:mysql://localhost:3306/mydatabase") .withDriverName("com.mysql.jdbc.Driver") .withUsername("myuser") .withPassword("mypassword") .build(); JdbcCatalog catalog = new JdbcCatalog("mycatalog", connectionOptions); tableEnv.registerCatalog("mycatalog", catalog); //读取MySQL数据 String sourceTable = "mytable"; String sql = "SELECT * FROM " + sourceTable; Table source = tableEnv.sqlQuery(sql); //SQL分析并转换 String resultTable = "result"; sql = "SELECT col1, col2, col3 FROM " + sourceTable + " WHERE col1 > 10"; Table result = tableEnv.sqlQuery(sql); //写入MySQLJdbcOutputFormat jdbcOutputFormat = JdbcOutputFormat.buildJdbcOutputFormat() .setDBUrl("jdbc:mysql://localhost:3306/mydatabase") .setDrivername("com.mysql.jdbc.Driver") .setUsername("myuser") .setPassword("mypassword") .setQuery("INSERT INTO myresulttable (col1, col2, col3) VALUES (?, ?, ?)") .finish(); DataStream<Row> dataStream = tableEnv.toAppendStream(result, Row.class); dataStream.addSink(new JdbcOutputSinkFunction(jdbcOutputFormat)); //执行任务 env.execute("MySQL to MySQL"); ``` 需要注意的是,在写入MySQL表时,需要使用JdbcOutputFormatJdbcOutputSinkFunction来实现。其中,JdbcOutputFormat需要设置数据库连接信息和写入语句,JdbcOutputSinkFunction将Table转换为DataStream后,再使用JdbcOutputFormat数据写入MySQL表中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值