zoukankan      html  css  js  c++  java
  • 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方法。

      Async I/O 提供了Capacity参数控制请求并发数,一旦Capacity被耗尽,会触发反压机制来抑制上游数据的摄入。

      Async I/O 输出提供乱序和顺序两种模式。

     
    作者:Woople
    链接:https://www.jianshu.com/p/cefc245e51c7
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

    本文来自博客园,作者:秋华,转载请注明原文链接:https://www.cnblogs.com/qiu-hua/p/13778750.html

  • 相关阅读:
    udev 禁止某些驱动自动加载
    AT91SAM9XEK ramdisk 启动笔记
    MTD 工具使用
    kmp问题
    野指针 空指针 通用指针
    manacher算法实现
    manacher算法
    strcpy和strlen
    冒泡排序
    C++的重载(overload)与重写(override
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/13778750.html
Copyright © 2011-2022 走看看