【问题标题】:Scheduled Task with Apache FlinkApache Flink 的计划任务
【发布时间】:2021-10-15 12:33:58
【问题描述】:

我有一个并行度为 5 的 flink 工作(现在!!)。并且richFlatMap 流之一在open(Configuration parameters) 方法中打开一个文件。在flatMap操作中没有任何打开动作,它只是读取文件来搜索一些东西。 (有一个实用程序类具有类似utilityClass.searchText('abc') 的方法)。这是样板代码:

public class MyFlatMap extends RichFlatMapFunction<...> {

    private MyUtilityFile myFile;

    @Override
    public void open(Configuration parameters) throws Exception {
        myFile.Open("fileLocation");
    }

    @Override
    public void flatMap(...) throws Exception {
        String text = myFile.searchText('abc');
        if (text != null) // take an action
        else // another action
    }

}

此文件每天在特定时间由 python 脚本更新。因此,我还应该在 flatMap 流中打开新创建的文件(通过 python 脚本)。

我只是认为这可以由ScheduledExecutorService 完成,只需一个线程池。

我无法在每次 flatMap 调用时都打开这个文件,因为它很大。

这是我正在尝试编写的样板代码:

public class MyFlatMap extends RichFlatMapFunction<...> implements Runnable {
    private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

    private MyUtilityFile myFile;

    @Override
    public void run() {
       myFile.Open("fileLocation");     
    }

    @Override
    public void open(Configuration parameters) throws Exception {
        scheduler.scheduleAtFixedRate(this, 1, 1, TimeUnit.HOURS);

        myFile.Open("fileLocation");
    }

    @Override
    public void flatMap(...) throws Exception {
        String text = myFile.searchText('abc');
        if (text != null) // take an action
        else // another action
    }

}

这个样板文件是否适合 Flink 环境?如果没有,我怎样才能以预定的方式打开文件? (没有“使用kafka更新文件发送事件并通过flink读取事件后”之类的选项)

【问题讨论】:

    标签: apache-flink flink-streaming


    【解决方案1】:

    或许可以直接实现ProcessingTimeCallback接口,支持定时器操作

    public class MyFlatMap extends RichFlatMapFunction<...> implements ProcessingTimeCallback { 
        private MyUtilityFile myFile;
    
     
        @Override
        public void open(Configuration parameters) throws Exception {
            scheduler.scheduleAtFixedRate(this, 1, 1, TimeUnit.HOURS);
    
            final long now = getProcessingTimeService().getCurrentProcessingTime();
            getProcessingTimeService().registerTimer(now + 3600000, this);
    
            myFile.Open("fileLocation");
        }
    
        @Override
        public void flatMap(...) throws Exception {
            String text = myFile.searchText('abc');
            if (text != null) // take an action
            else // another action
        }
    
        @Override
        public void onProcessingTime(long timestamp) throws Exception {
            myFile.Open("fileLocation");
    
            final long now = getProcessingTimeService().getCurrentProcessingTime();
            getProcessingTimeService().registerTimer(now + 3600000, this);
        }
    }
    

    【讨论】:

    • 感谢您的回复,但我找不到 getProcessingTimeService() 方法。我应该导入哪个包?
    • ((StreamingRuntimeContext) getRuntimeContext()).getProcessingTimeService() in open function
    • 谢谢,它可以与 processingTimeService 一起正常工作。我正在寻找这样的解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 2011-02-25
    • 1970-01-01
    • 2011-05-22
    • 2011-01-22
    • 2019-06-06
    相关资源
    最近更新 更多