flink Periodic Watermarks 自定义周期性水印

1、BoundedOutOfOrdernessGenerator

/**
 * This generator generates watermarks assuming that elements arrive out of order,
 * but only to a certain degree. The latest elements for a certain timestamp t will arrive
 * at most n milliseconds after the earliest elements for timestamp t.
 */
public class BoundedOutOfOrdernessGenerator implements AssignerWithPeriodicWatermarks<MyEvent> {

    private final long maxOutOfOrderness = 3000; // 3.0 seconds

    private long currentMaxTimestamp;

    @Override
    public long extractTimestamp(MyEvent element, long previousElementTimestamp) {
        long timestamp = element.getCreationTime();
        currentMaxTimestamp = Math.max(timestamp, currentMaxTimestamp);
        return timestamp;
    }

    @Override
    public Watermark getCurrentWatermark() {
        // return the watermark as current highest timestamp minus the out-of-orderness bound
        return new Watermark(currentMaxTimestamp - maxOutOfOrderness);
    }
}

效果解析:

 

2、TimeLagWatermarkGenerator

/**
 * This generator generates watermarks that are lagging behind processing time by a fixed amount.
 * It assumes that elements arrive in Flink after a bounded delay.
 */
public class TimeLagWatermarkGenerator implements AssignerWithPeriodicWatermarks<MyEvent> {

    private final long maxTimeLag = 3000; // 3 seconds

    @Override
    public long extractTimestamp(MyEvent element, long previousElementTimestamp) {
        return element.getCreationTime();
    }

    @Override
    public Watermark getCurrentWatermark() {
        // return the watermark as current time minus the maximum time lag
        return new Watermark(System.currentTimeMillis() - maxTimeLag);
    }
}

效果解析:

 

 

 

转载于:https://www.cnblogs.com/asker009/p/11318290.html

Flink 中,可以将 MySQL 自定义为检查点存储。以下是实现方法: 1. 首先,需要将 Flink 依赖库中的 flink-connector-jdbc 和 mysql-connector-java 添加到项目中。 2. 接着,创建一个实现 CheckpointStorage 接口的 MySQLCheckpointStorage 类,用于将检查点状态存储到 MySQL 数据库中。具体实现可以参考下面的代码示例: ``` public class MySQLCheckpointStorage implements CheckpointStorage { private final String jdbcUrl; private final String username; private final String password; public MySQLCheckpointStorage(String jdbcUrl, String username, String password) { this.jdbcUrl = jdbcUrl; this.username = username; this.password = password; } @Override public CheckpointStorageAccess createCheckpointStorage(JobID jobID) throws IOException { return new MySQLCheckpointStorageAccess(jdbcUrl, username, password, jobID); } @Override public boolean supportsHighlyAvailableStorage() { return false; } private static class MySQLCheckpointStorageAccess implements CheckpointStorageAccess { private final String jdbcUrl; private final String username; private final String password; private final JobID jobID; public MySQLCheckpointStorageAccess(String jdbcUrl, String username, String password, JobID jobID) { this.jdbcUrl = jdbcUrl; this.username = username; this.password = password; this.jobID = jobID; } @Override public CheckpointMetadata createCheckpointMetadata(long checkpointID, CheckpointOptions checkpointOptions) throws IOException { // TODO: 实现创建检查点元数据的逻辑 return null; } @Override public void addCheckpointMetadata(CheckpointMetadata checkpointMetadata) throws IOException { // TODO: 实现添加检查点元数据的逻辑 } @Override public CheckpointMetadata getCheckpointMetadata(long checkpointID) throws IOException { // TODO: 实现获取检查点元数据的逻辑 return null; } @Override public void shutdown(JobStatus jobStatus) throws IOException { // TODO: 实现关闭存储的逻辑 } } } ``` 3. 在 Flink 的配置文件中,设置 checkpoint.storage 属性为自定义的 MySQLCheckpointStorage 类。例如: ``` # Flink 的配置文件 checkpoint.storage: com.example.MySQLCheckpointStorage checkpoint.storage.jdbc.url: jdbc:mysql://localhost:3306/checkpoint checkpoint.storage.jdbc.username: root checkpoint.storage.jdbc.password: password ``` 其中,checkpoint.storage.jdbc.url、checkpoint.storage.jdbc.username 和 checkpoint.storage.jdbc.password 分别是 MySQL 数据库的连接 URL、用户名和密码。 4. 最后,在 Flink 应用程序中,调用 ExecutionEnvironment.getCheckpointConfig().enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION),启用外部化的检查点,并设置清理策略为取消任务时保留检查点。这样,Flink 在执行检查点时,就会将状态存储到 MySQL 数据库中。 以上就是在 Flink 中将 MySQL 自定义为检查点存储的实现方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值