Flink寫入mysql其實也很簡單,只用繼承RichSinkFunction這個類,重寫裏面的方法就行了.具體的實現如下:
/**
* 把結果保存到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) values(?,?)"
for (i <- value){
ps = connection.prepareStatement(sql)
ps.