此问题摘录自Flink中文社区邮件,仅仅作为记录用。

Q:
我有个问题想请教下,关于flinksql与mysql维表关联 关于mysql更新的问题 有没有好的方法?我现在使用的广播mysql但是有个问题,广播mysql时我的流数据已经到了但是mysql的数据还没有到导致我现在数据会再启动的时候丢失一部分数据。

A:
如果是想达到延迟JOIN的目的,可以考虑利用WaterMarkmaxoutoforderness
job取消时做savepoint重启时应该不会有这个问题

A:
您的意思是open 全量预加载吗?我目前的逻辑是自己自定义的source 广播出去
这是我的source:

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.source.RichSourceFunction;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;

public class GetMysqlDvcId extends RichSourceFunction<Map<String, Integer>> {

    private Connection connection = null;
    private PreparedStatement ps = null;
    private volatile boolean isRunning = true;

    @Override
    public void open(Configuration parameters) throws Exception {
        super.open(parameters);
          String database=“db_nssa”;
          String host=212.21.12.12;
          String password=“saa!;
          String port=3306;
          String username=“root”;

        String driver = “com.mysql.jdbc.Driver”;
        String url = "jdbc:mysql://" + host + ":" + port + "/" + database + "?useUnicode=true&characterEncoding=UTF-8";
          connection = MySQLUtil.getConnection(driver, url, username, password);
        if (this.connection != null) {
            String sql = "select ip,device_id from sys_device";
            ps =connection.prepareStatement(sql);
        }
    }

    @Override
    public void run(SourceContext<Map<String, Integer>> ctx) throws Exception {
        Map<String, Integer> map = new HashMap<>();
        while (isRunning) {
            ResultSet resultSet = ps.executeQuery();
            while (resultSet.next()) {
                    map.put(resultSet.getString(“ip”),resultSet.getInt(“device_id”));
            }
            System.out.println("=======select alarm notify from mysql, size = {}, map = {}"+ map.size()+ map);
            ctx.collect(map);
            map.clear();
            Thread.sleep(2000 * 60);
        }

    }

    @Override
    public void cancel() {
        try {
            super.close();
            if (connection != null) {
                connection.close();
            }
            if (ps != null) {
                ps.close();
            }
        } catch (Exception e) {
            System.out.println(“runException:{}+e);
        }
        isRunning = false;
    }
}

A:

hi 可以考虑使用 temporal table join :

[1] https://ci.apache.org/projects/flink/flink-docs-stable/dev/table/streaming/joins.html#join-with-a-temporal-table

A:
延迟维表关联这个特性我觉得还是一个比较通用的特性,目前我们考虑借助 Timer 来实现的,社区如果有这个功能的话,我觉得对于 Flink使用方会有很大帮助的。
我看社区有这样的一个 JIRA 再跟踪了[1],我会持续关注。

[1] https://issues.apache.org/jira/browse/FLINK-19063