Flink 自定义Source 读取Mysql

吃可爱长大的小学妹 提交于 2019-12-20 16:50:48
object CustomSource {

  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
    val stringDataStream: DataStream[Provience] = env.addSource(new MySQLSource())
    stringDataStream.print("Provience Record")
    env.execute
  }

}

class MySQLSource() extends RichSourceFunction[Provience] {
  var ps: PreparedStatement = _
  var connection: Connection = _
  var resultSet: ResultSet = _

  override def open(parameters: Configuration): Unit = {
    super.open(parameters)
    val driver = "com.mysql.jdbc.Driver"
    val url = "jdbc:mysql://hadoop100:3306/test?useSSL=true"
    val username = "root"
    val password = "123456"
    Class.forName(driver)
    try {
      connection = DriverManager.getConnection(url, username, password)
      val sql = "select * from DMPProvince;"
      ps = connection.prepareStatement(sql)
    } catch {
      case ex: Exception => {

      }
    }
  }

  override def run(sourceContext: SourceFunction.SourceContext[Provience]): Unit = {
    try {
      resultSet = ps.executeQuery()
      while (resultSet.next()) {
        var provience = new Provience()
        provience.setProvienceName(resultSet.getString(1).trim)
        provience.setCityName(resultSet.getString(2).trim)
        provience.setCt(resultSet.getInt(3))
        sourceContext.collect(provience)
      }
    } catch {
      case ex: SQLException => {
        ex.printStackTrace()
      }
    }
  }

  override def cancel(): Unit = {

  }

  override def close(): Unit = {
    super.close()
    if (connection != null) {
      connection.close()
    }
    if (ps != null) {
      ps.close()
    }
  }
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!