我正在实现一个 SourceFunction,它从数据库中读取数据。如果停止或崩溃(即保存点和检查点),并且数据只处理一次,该作业应该能够恢复。
到目前为止我所拥有的:
@SerialVersionUID(1L)
class JDBCSource(private val waitTimeMs: Long) extends
RichParallelSourceFunction[Event] with StoppableFunction with LazyLogging{
@transient var client: PostGreClient = _
@volatile var isRunning: Boolean = true
val DEFAULT_WAIT_TIME_MS = 1000
def this(clientConfig: Serializable) =
this(clientConfig, DEFAULT_WAIT_TIME_MS)
override def stop(): Unit = {
this.isRunning = false
}
override def open(parameters: Configuration): Unit = {
super.open(parameters)
client = new JDBCClient
}
override def run(ctx: SourceFunction.SourceContext[Event]): Unit = {
while (isRunning){
val statement = client.getConnection.createStatement()
val resultSet = statement.executeQuery("SELECT name, timestamp FROM MYTABLE")
while (resultSet.next()) {
val event: String = resultSet.getString("name")
val timestamp: Long = resultSet.getLong("timestamp")
ctx.collectWithTimestamp(new Event(name, timestamp), timestamp)
}
}
}
override def cancel(): Unit = {
isRunning = false
}
}
如何确保只获取尚未处理的数据库行?我假设该ctx
变量将包含有关当前水印的一些信息,以便我可以将查询更改为:
select name, timestamp from myTable where timestamp > ctx.getCurrentWaterMark
但它对我没有任何相关的方法。任何想法如何解决这个问题将不胜感激