Flink writing to mysql is actually very simple, just inherit the class RichSinkFunction and rewrite the methods inside. The specific implementation is as follows:
/**
* 把结果保存到mysql里面
*/
class MySQLSink extends RichSinkFunction[List[ItemViewCount]] with Serializable {
var connection: sql.Connection = _
var ps: sql.PreparedStatement = _
var statement: java.sql.Statement = _
val username = "***"
val password = "***"
val drivername = "com.mysql.jdbc.Driver"
val url = "***"
/**
* 打开mysql的连接
* @param parameters
*/
override def open(parameters: Configuration): Unit = {
Class.forName(drivername)
connection = DriverManager.getConnection(url, username, password)
statement = connection.createStatement
connection.setAutoCommit(false)
}
/**
* 处理数据后写入mysql
* @param value
*/
override def invoke(value: List[ItemViewCount]): Unit = {
val sql = "insert into topn(itemid,viewcount)