flink连接hbase的连接池_Flink 异步IO访问外部数据(hbase、mysql篇)

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

最近看了大佬的博客,突然想起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]) {

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值