自定义MySQL Source

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
package com.shujia.source

import org.apache.flink.streaming.api.functions.source.{ParallelSourceFunction, RichParallelSourceFunction, RichSourceFunction, SourceFunction}
import org.apache.flink.streaming.api.scala._

import java.sql.{Connection, DriverManager, ResultSet, Statement}

object Demo05MySQLSource {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
    env
      .addSource(new MySQLSource)
      .print()

    env.execute()
  }

}

// 以MySQL作为数据源 构建MySQLSource
class MySQLSource extends SourceFunction[String] {
  override def run(ctx: SourceFunction.SourceContext[String]): Unit = {
    //加载驱动
    Class.forName("com.mysql.jdbc.Driver")

    //建立连接
    val conn: Connection = DriverManager.getConnection("jdbc:mysql://master:3306/student?useSSL=false", "root", "123456")

    // 创建Statement
    val stat: Statement = conn.createStatement()

    // 执行SQL
    val rs: ResultSet = stat.executeQuery("select id,name,age,gender,clazz from student")

    // 循环遍历返回的结果
    while (rs.next()) {
      val id: Int = rs.getInt("id")
      val name: String = rs.getString("name")
      val age: Int = rs.getInt("age")
      val gender: String = rs.getString("gender")
      val clazz: String = rs.getString("clazz")
      // 将获取到的每一条数据都发送到下游
      ctx.collect(s"$id,$name,$age,$gender,$clazz")
    }

    // 关闭连接
    conn.close()

  }

  override def cancel(): Unit = {

  }
}
package com.shujia.source

import org.apache.flink.configuration.Configuration
import org.apache.flink.streaming.api.functions.source.{ParallelSourceFunction, RichParallelSourceFunction, RichSourceFunction, SourceFunction}
import org.apache.flink.streaming.api.scala._

import java.sql.{Connection, DriverManager, ResultSet, Statement}

object Demo05MySQLSource {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
    env.addSource(new MySQLSource).print()

    env.execute()
  }

}

/**
 * SourceFunction :单线程
 * RichSourceFunction:多了open以及cancel两个方法
 * ParallelSourceFunction:并行的source
 * RichParallelSourceFunction:多了open以及cancel两个方法
 */

// 以MySQL作为数据源 构建MySQLSource
class MySQLSource extends RichParallelSourceFunction[String] {
  var conn:Connection = _
  /**
   * 在Flink程序运行时会执行一次,可以进行一些初始化操作
   * 会在run方法之前执行
   */
  override def open(parameters: Configuration): Unit = {
    println("建立连接")
    // 加载驱动
    Class.forName("com.mysql.jdbc.Driver")
    // 建立连接
    conn= DriverManager.getConnection("jdbc:mysql://master:3306/student?useSSL=false", "root", "123456")

  }

  /**
   * 一般在例如map、join等操作执行完成后会调用
   */
  override def close(): Unit = {


  }

  /**
   * 主要用于获取数据并发送到下游
   */
  override def run(ctx: SourceFunction.SourceContext[String]): Unit = {

    // 创建Statement
    val stat: Statement = conn.createStatement()

    // 执行SQL
    val rs: ResultSet = stat.executeQuery("select id,name,age,gender,clazz from student")

    // 循环遍历返回的结果
    while (rs.next()) {
      val id: Int = rs.getInt("id")
      val name: String = rs.getString("name")
      val age: Int = rs.getInt("age")
      val gender: String = rs.getString("gender")
      val clazz: String = rs.getString("clazz")
      // 将获取到的每一条数据都发送到下游
      ctx.collect(s"$id,$name,$age,$gender,$clazz")
    }

    // 关闭连接
    conn.close()

  }

  override def cancel(): Unit = {
    // 关闭连接
    conn.close()

  }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值