flink读取mysql的数据

flink读取mysql的数据
1.导入需要的依赖

 <dependency>
            <groupId>org.apache.flinkgroupId>
            <artifactId>flink-scala_2.11artifactId>
            <version>1.7.2version>


        dependency>
        
        <dependency>
            <groupId>org.apache.flinkgroupId>
            <artifactId>flink-streaming-scala_2.11artifactId>
            <version>1.7.2version>

        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.44version>
        dependency>

        <dependency>
            <groupId>org.apache.flinkgroupId>
            <artifactId>flink-jdbc_2.11artifactId>
            <version>1.7.0version>
        dependency>
package org.zsw.flink

import org.apache.flink.api.common.typeinfo.BasicTypeInfo
import org.apache.flink.api.java.io.jdbc.JDBCInputFormat
import org.apache.flink.api.java.typeutils.RowTypeInfo
import org.apache.flink.api.scala._
import org.apache.flink.types.Row

/**
 * @description:
 * @author: zsw
 * @date: Created in 2020/4/13 10:37
 * @modified By:
 */
object Flink_MysqlSource {


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

    val env: ExecutionEnvironment = ExecutionEnvironment.getExecutionEnvironment
    val inputMysql: DataSet[Row] = jdbcRead(env)

    inputMysql.print()

  }

  def jdbcRead(env: ExecutionEnvironment) ={
    val inputMysql: DataSet[Row] = env.createInput(JDBCInputFormat.buildJDBCInputFormat()
      //    .指定驱动名称
      .setDrivername("com.mysql.jdbc.Driver")
      //      url
      .setDBUrl("jdbc:mysql://spark01:3306/test")
      .setUsername("root")
      .setPassword("root")
      .setQuery("select sensor,temp from temperature")
      .setRowTypeInfo(new RowTypeInfo(BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.DOUBLE_TYPE_INFO))
      .finish()
    )
    inputMysql
  }

}



你可能感兴趣的:(flink读取mysql的数据)