天天看点

Flink实例(十八):FLINK 异步IO (三)实例 (一)

生成6条数据,从0开始递增的6个数字。模拟异步查询之后,加上时间戳输出

public class AsyncIODemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        final int maxCount = 6;
        final int taskNum = 1;
        final long timeout = 40000;

        DataStream<Integer> inputStream = env.addSource(new SimpleSource(maxCount));
        AsyncFunction<Integer, String> function = new SampleAsyncFunction();

        DataStream<String> result = AsyncDataStream.unorderedWait(
                    inputStream,
                    function,
                    timeout,
                    TimeUnit.MILLISECONDS,
                    10).setParallelism(taskNum);

        result.map(new MapFunction<String, String>() {
            @Override
            public String map(String value) throws Exception {
                return value + "," + System.currentTimeMillis();
            }
        }).print();

        env.execute("Async IO Demo");
    }

    private static class SimpleSource implements SourceFunction<Integer> {
        private volatile boolean isRunning = true;
        private int counter = 0;
        private int start = 0;

        public SimpleSource(int maxNum) {
            this.counter = maxNum;
        }

        @Override
        public void run(SourceContext<Integer> ctx) throws Exception {
            while ((start < counter || counter == -1) && isRunning) {
                synchronized (ctx.getCheckpointLock()) {
                    System.out.println("send data:" + start);
                    ctx.collect(start);
                    ++start;
                }
                Thread.sleep(10L);
            }
        }

        @Override
        public void cancel() {
            isRunning = false;
        }
    }
}      

异步方法

public class SampleAsyncFunction extends RichAsyncFunction<Integer, String> {
    private long[] sleep = {100L, 1000L, 5000L, 2000L, 6000L, 100L};

    @Override
    public void open(Configuration parameters) throws Exception {
        super.open(parameters);
    }

    @Override
    public void close() throws Exception {
        super.close();
    }
    //逻辑处理的主体函数 
    @Override
    public void asyncInvoke(final Integer input, final ResultFuture<String> resultFuture) {
        System.out.println(System.currentTimeMillis() + "-input:" + input + " will sleep " + sleep[input] + " ms");

        query(input, resultFuture);
    }
      
//通过ResultFuture的对象 调用complete达到发送数据到下游的效果 complete方法可以简单类似成collector的collect方法      
private void query(final Integer input, final ResultFuture<String> resultFuture) {
        try {
            Thread.sleep(sleep[input]);
            resultFuture.complete(Collections.singletonList(String.valueOf(input)));
        } catch (InterruptedException e) {
            resultFuture.complete(new ArrayList<>(0));
        }
    }

    private void asyncQuery(final Integer input, final ResultFuture<String> resultFuture) {
        CompletableFuture.supplyAsync(new Supplier<Integer>() {

            @Override
            public Integer get() {
                try {
                    Thread.sleep(sleep[input]);
                    return input;
                } catch (Exception e) {
                    return null;
                }
            }
        }).thenAccept((Integer dbResult) -> {
            resultFuture.complete(Collections.singleton(String.valueOf(dbResult)));
        });
    }
}      

  上面的代码中有两个方法

query()

asyncQuery()

,其中

Thread.sleep(sleep[input]);

用来模拟查询需要等待的时间,每条数据等待的时间分别为

100L, 1000L, 5000L, 2000L, 6000L, 100L

毫秒。

结果分析

运行

query()

的结果为

send data:0
send data:1
send data:2
send data:3
send data:4
send data:5
1577801193230-input:0 will sleep 100 ms
1577801193331-input:1 will sleep 1000 ms
0,1577801194336
1,1577801194336
1577801194336-input:2 will sleep 5000 ms
1577801199339-input:3 will sleep 2000 ms
2,1577801201341
1577801201342-input:4 will sleep 6000 ms
3,1577801207345
4,1577801207345
1577801207346-input:5 will sleep 100 ms
5,1577801207451      

可以看到第一条数据进入到

map

算子的时间与最后一条相差了13115毫秒,执行的顺序与source中数据的顺序一致,并且是串行的。

asyncQuery()

send data:0
send data:1
send data:2
send data:3
1577802161755-input:0 will sleep 100 ms
1577802161756-input:1 will sleep 1000 ms
1577802161757-input:2 will sleep 5000 ms
send data:4
send data:5
1577802161783-input:3 will sleep 2000 ms
1577802161784-input:4 will sleep 6000 ms
1577802161785-input:5 will sleep 100 ms
0,1577802161859
1,1577802162759
3,1577802163862
5,1577802163962
2,1577802166760
4,1577802168762      

同样第一条数据进入map算子的时间与最后一条仅相差了6903毫秒,而且输出结果的顺序并不是source中的顺序,而是按照查询时间递增的顺序输出,并且查询请求几乎是同一时间发出的。

通过上面的例子可以看出,flink所谓的异步IO,并不是只要实现了

asyncInvoke

方法就是异步了,这个方法并不是异步的,而是要依靠这个方法里面所写的查询是异步的才可以。否则像是上面

query()

方法那样,同样会阻塞查询相当于同步IO。在实现flink异步IO的时候一定要注意。官方文档也给出了相关的说明。

For example, the following patterns result in a blocking asyncInvoke(...) functions and thus void the asynchronous 
behavior:Using a database client whose lookup/query method call blocks until the result has been received back      

注意:

  使用Async I/O,需要外部存储有支持异步请求的客户端

  使用Async I/O,继承

RichAsyncFunction(接口AsyncFunction<IN, OUT>的抽象类)

,重写或实现

open(建立连接)

close(关闭连接)

asyncInvoke(异步调用)

3个方法即可。

  使用Async I/O, 最好结合缓存一起使用,可减少请求外部存储的次数,提高效率。

  Async I/O 提供了

Timeout

参数来控制请求最长等待时间。默认,异步I/O请求超时时,会引发异常并重启或停止作业。 如果要处理超时,可以重写

AsyncFunction#timeout

方法。

作者:Woople

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。