Asynchronous flink水槽只支持bio吗?
sink的Asynchronous flink水槽只支持bio吗?,asynchronous,nio,apache-flink,flink-streaming,Asynchronous,Nio,Apache Flink,Flink Streaming,sink的invoke方法似乎无法实现异步io?e、 g.返回未来 例如,redis连接器使用绝地库同步执行redis命令: 然后它会阻止flink的任务线程等待redis服务器的网络响应,每个命令?!其他操作人员是否可能与sink在同一线程中运行?如果是的话,那么它也会阻止他们吗 我知道flink有asyncio api,但它似乎不适用于sink impl 正如@Dexter所提到的,您可以使用richsyncFunction。下面是一个示例代码(可能需要进一步更新才能正常工作;) Asyn
invoke
方法似乎无法实现异步io?e、 g.返回未来
例如,redis连接器使用绝地库同步执行redis命令:
然后它会阻止flink的任务线程等待redis服务器的网络响应,每个命令?!其他操作人员是否可能与sink在同一线程中运行?如果是的话,那么它也会阻止他们吗
我知道flink有asyncio api,但它似乎不适用于sink impl
正如@Dexter所提到的,您可以使用
richsyncFunction
。下面是一个示例代码(可能需要进一步更新才能正常工作;)
AsyncDataStream.orderedWait(ds,新的RichAsyncFunction()){
临时私人再贴现客户机;
临时私有命令;
临时私有执行器服务执行器;
@凌驾
公共void open(配置参数)引发异常{
super.open(参数);
client=RedisClient.create(“redis://localhost");
commands=client.connect().async();
executor=Executors.newFixedThreadPool(10);
}
@凌驾
public void close()引发异常{
//关闭连接和线程池。
client.shutdown();
executor.shutdown();
super.close();
}
公共void asyncInvoke(Tuple2输入,最终AsyncCollector收集器)引发异常{
//例如,以异步方式从redis获取一些东西
final RedisFuture=commands.get(“key”);
future.thenAccept(新消费者(){
@凌驾
公共void接受(字符串值){
collector.collect(Collections.singletonList(future.get());
}
});
}
},1000,时间单位为毫秒);
Jedis没有异步接口您可以编写自己的连接器impl,它使用“RichAsyncFunction”@Dexter您能举一个简单的例子吗?thenept
和addListener
之间有什么区别?addListener
这是番石榴的ListenableFuture
,我也不知道accept是从哪里来的,您是指Java 8的CompletableFuture
,它与ListenableFuture
中的addListener
功能类似。您是否使用莴苣作为redis客户端?返回的future有然后accept
方法绑定完成回调,请参见Ahh,这样更好。我们可以简单地使用accept()。我已经更新了示例代码。
AsyncDataStream.orderedWait(ds, new RichAsyncFunction<Tuple2<String,MyEvent>, String>() {
transient private RedisClient client;
transient private RedisAsyncCommands<String, String> commands;
transient private ExecutorService executor;
@Override
public void open(Configuration parameters) throws Exception {
super.open(parameters);
client = RedisClient.create("redis://localhost");
commands = client.connect().async();
executor = Executors.newFixedThreadPool(10);
}
@Override
public void close() throws Exception {
// shut down the connection and thread pool.
client.shutdown();
executor.shutdown();
super.close();
}
public void asyncInvoke(Tuple2<String, MyEvent> input, final AsyncCollector<String> collector) throws Exception {
// eg.g get something from redis in async
final RedisFuture<String> future = commands.get("key");
future.thenAccept(new Consumer<String>() {
@Override
public void accept(String value) {
collector.collect(Collections.singletonList(future.get()));
}
});
}
}, 1000, TimeUnit.MILLISECONDS);