Flink 自定义Source 读取Mysql

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()
    }
  }
}

你可能感兴趣的:(大数据)