1 pom 依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zxh</groupId>
<artifactId>flink_demo2</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<flink.version>1.9.2</flink.version>
<scala.version>2.11.8</scala.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-streaming-java_2.11</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-streaming-scala_2.11</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.11.8</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
</dependencies>
</project>
2 代码
package com.zxh
import java.sql.{Connection, DriverManager, PreparedStatement}
import org.apache.flink.configuration.Configuration
import org.apache.flink.streaming.api.functions.source.{RichSourceFunction, SourceFunction}
import org.apache.flink.streaming.api.scala.{DataStream, StreamExecutionEnvironment}
/**
* @author zhangxh
* @date 2020/8/15 16:43
* @version 1.0
*/
object MysqlSourceStream {
def main(args: Array[String]): Unit = {
val env = StreamExecutionEnvironment.getExecutionEnvironment
import org.apache.flink.api.scala._
val dStream: DataStream[User] = env.addSource(new MySqlSource).setParallelism(1)
dStream.print()
env.execute("consume mysql source")
}
/**
*
*/
class MySqlSource extends RichSourceFunction[User] {
private var connection: Connection = null
private var ps: PreparedStatement = null
override def open(parameters: Configuration): Unit = {
val driver = "com.mysql.jdbc.Driver"
val url = "jdbc:mysql://node03:3306/test"
val username = "root"
Class.forName(driver)
connection = DriverManager.getConnection(url, username, "123456")
val sql = "select * from user"
ps = connection.prepareStatement(sql)
}
override def close(): Unit = {
if (connection != null) {
connection.close()
}
if (ps != null) {
ps.close()
}
}
override def run(ctx: SourceFunction.SourceContext[User]): Unit = {
val rs = ps.executeQuery()
while (rs.next()) {
val flight = User(rs.getInt("id"), rs.getString("name"), rs.getInt("age"))
ctx.collect(flight)
}
rs.close()
}
override def cancel(): Unit = {}
}
/**
* 定义case类
* @param id
* @param name
* @param age
*/
case class User(id: Int, name: String, age: Int)
}
3 mysql 数据
4 运行结果