flink使用dataStream Api消费数据保存到mysql

记录一下基础的自定义sink:flink处理流式数据保存到mysql中
话不多说,上代码:

pom.xml


        UTF-8
        1.7.0
        1.8
        2.11
        2.11.12
        ${java.version}
        ${java.version}
    

    
        
            apache.snapshots
            Apache Development Snapshot Repository
            https://repository.apache.org/content/repositories/snapshots/
            
                false
            
            
                true
            
        
    

    
        
            org.apache.flink
            flink-java
            ${flink.version}
        
        
            org.apache.flink
            flink-streaming-java_${scala.binary.version}
            ${flink.version}
        

        
            org.slf4j
            slf4j-log4j12
            1.7.21
        

        
            org.apache.flink
            flink-scala_${scala.binary.version}
            ${flink.version}
        
        
            org.apache.flink
            flink-streaming-scala_${scala.binary.version}
            ${flink.version}
        
        
            org.scala-lang
            scala-library
            2.11.12
        
        
            log4j
            log4j
            1.2.17
        

        
            mysql
            mysql-connector-java
            5.1.47
        
    

DataStreamCustomSink

package com.dataStream

import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.streaming.api.windowing.time.Time

/**
  * @author 
  * @date 2020/5/27 10:27
  * @version 1.0
  */
case class Person(name:String,age:Integer,sex:String)


object DataStreamCustomSink {
  def main(args: Array[String]): Unit = {
    val env=StreamExecutionEnvironment.getExecutionEnvironment
    var lines = env.socketTextStream("localhost", 9999)
    import org.apache.flink.api.scala._
    val lineStream=lines.map(x=>x.split(",")).map(x=>Person(x(0),x(1).toInt,x(2)))
    lineStream.addSink(new MyJdbcSink)
    env.execute("DataStreamCustomSink")
  }
}

MyJdbcSink

package com.dataStream

import java.sql.{Connection, DriverManager, PreparedStatement}

import org.apache.flink.configuration.Configuration
import org.apache.flink.streaming.api.functions.sink.{RichSinkFunction, SinkFunction}

/**
  * @author 
  * @date 2020/5/27 10:38
  * @version 1.0
  */
//因为SinkFunction的功能比较少 所以使用rich的SinkFunction
class MyJdbcSink() extends RichSinkFunction[Person] {
  //定义sql连接、预编译器
  var conn: Connection = _
  var insertStmt: PreparedStatement = _
  var updateStmt: PreparedStatement = _

  //初始化 、 创建连接 、 和 预编译语句
  override def open(parameters: Configuration): Unit = {
    super.open(parameters)
    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456")
    insertStmt = conn.prepareStatement("insert into person2 (name,age, sex) values (?,?,?)")
    updateStmt = conn.prepareStatement("update person2 set sex=?, age  = ? where name = ?")
  }

  // 调用连接 执行sql
  override def invoke(value: Person, context: SinkFunction.Context[_]): Unit = {
    // 执行更新语句
    updateStmt.setString(3, value.name)
    updateStmt.setInt(2,value.age)
    updateStmt.setString(1, value.sex)
    updateStmt.execute()
    //如果update没有更新 即 没有查询到数据 即 没有该id 那么执行插入
    if (updateStmt.getUpdateCount == 0) {
      insertStmt.setString(1, value.name)
      insertStmt.setInt(2,value.age)
      insertStmt.setString(3, value.sex)
      insertStmt.execute()
    }
  }

  //关闭时做清理工作
  override def close(): Unit = {
    insertStmt.close()
    updateStmt.close()
    conn.close()
  }
}

你可能感兴趣的:(大数据,mysql,flink,flink,mysql,jdbc)