最近看了大佬的博客,突然想起Async I/O方式是Blink 推给社区的一大重要功能,可以使用异步的方式获取外部数据,想着自己实现以下,项目上用的时候,可以不用现去找了。
最开始想用scala 实现一个读取 hbase数据的demo,参照官网demo:
/*** An implementation of the ‘AsyncFunction‘ that sends requests and sets the callback.*/
class AsyncDatabaseRequest extendsAsyncFunction[String, (String, String)] {/**The database specific client that can issue concurrent requests with callbacks*/lazy val client: DatabaseClient= newDatabaseClient(host, post, credentials)/**The context used for the future callbacks*/implicit lazy val executor: ExecutionContext=ExecutionContext.fromExecutor(Executors.directExecutor())
override def asyncInvoke(str: String, resultFuture: ResultFuture[(String, String)]): Unit={//issue the asynchronous request, receive a future for the result
val resultFutureRequested: Future[String] = client.query(str)//set the callback to be executed once the request by the client is complete//the callback simply forwards the result to the result future
resultFutureRequested.onSuccess {case result: String =>resultFuture.complete(Iterable((str, result)))
}
}
}//create the original stream
val stream: DataStream[String] =...//apply the async I/O transformation
val resultStream: DataStream[(String, String)] =AsyncDataStream.unorderedWait(stream, new AsyncDatabaseRequest(), 1000, TimeUnit.MILLISECONDS, 100)
失败了,上图标红的部分实现不了
1、Future 找不到可以用的实现类
2、unorderedWait 一直报错
源码example 里面也有Scala 的案例
def main(args: Array[String]) {

本文介绍了如何使用Flink的异步IO功能来访问HBase和MySQL数据库。首先尝试使用Scala实现,但由于找不到合适的Future实现和unorderedWait报错而失败。然后转向Java版本,提供了一个通过RichAsyncFunction连接MySQL的完整示例,实现了从Kafka读取数据,查询MySQL并返回结果的功能。测试表明该方法能够正确处理数据并返回结果。
最低0.47元/天 解锁文章
765

被折叠的 条评论
为什么被折叠?



