附录 A:Flink DataStream API 速查

Flink DataStream API 常用操作速查卡:Environment 创建、Source 与 Sink、Transformation、Window、State、Watermark 策略、Checkpoint 配置及 Metrics 注册的代码模板与参数速查。

Environment

StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setParallelism(4);
env.enableCheckpointing(30_000);          // 默认就是 EXACTLY_ONCE
env.setStateBackend(new EmbeddedRocksDBStateBackend());
env.setRestartStrategy(RestartStrategies.fixedDelayRestart(3, Duration.ofSeconds(10)));
// 时间语义不在 Environment 上设置:由每个 Source 的 WatermarkStrategy 决定
方法默认作用
getExecutionEnvironment()-自动检测环境
createLocalEnvironment(n)-本地 n 并行度
setParallelism(n)本地 = CPU 核数;集群 = parallelism.default(默认 1)全局并行度
enableCheckpointing(ms)无开启 Checkpoint
setStateBackend(backend)HashMap状态后端
WatermarkStrategy(Source 侧)无事件时间与 Watermark 策略;没有全局时间语义开关

速查卡里的 Event 以 9.1 的 v3 record 为准,访问器是 amount()/eventTime()/userId()。第 4~8 章用的是同名 POJO,对应写法是 getAmount()/getEventTime()/getUserId(),两种不要混用。

Source

// Kafka Source
KafkaSource<Event> kafkaSource = KafkaSource.<Event>builder()
    .setBootstrapServers("kafka:9092")
    .setTopics("topic")
    .setGroupId("group")
    .setStartingOffsets(OffsetsInitializer.earliest())
    .setValueOnlyDeserializer(new EventDeserializationSchema())
    .build();

DataStream<Event> stream = env.fromSource(kafkaSource, watermarkStrategy, "kafka");

// Collection Source
DataStream<Event> stream = env.fromCollection(list);

// Socket Source
DataStream<String> stream = env.socketTextStream("localhost", 9999);

Transformation

stream
    .map(e -> transform(e))              // 1:1 转换
    .filter(e -> e.amount() > 0)         // 过滤
    .flatMap((FlatMapFunction<T, R>) ...) // 1:N
    .keyBy(Event::userId)                 // 分区
    .window(TumblingEventTimeWindows.of(Duration.ofMinutes(1)))  // 窗口
    .aggregate(new MyAggregate())         // 聚合
    .addSink(sink);                        // 输出
操作输入输出状态
mapTR无
filterTT无
flatMapTR无
keyByTKeyedStream无
reduceKeyedStreamT有
aggregateKeyedStreamR有
processKeyedStreamR有(自定义)
windowKeyedStreamWindowedStream有

Window

// 滚动窗口
stream.keyBy(...).window(TumblingEventTimeWindows.of(Duration.ofMinutes(1)));

// 滑动窗口
stream.keyBy(...).window(SlidingEventTimeWindows.of(Duration.ofMinutes(5), Duration.ofMinutes(1)));

// 会话窗口
stream.keyBy(...).window(EventTimeSessionWindows.withGap(Duration.ofMinutes(30)));

// 全局窗口 + 触发器
stream.keyBy(...).window(GlobalWindows.create()).trigger(CountTrigger.of(100));
窗口API适用
滚动TumblingEventTimeWindows固定周期统计
滑动SlidingEventTimeWindows移动平均
会话EventTimeSessionWindows用户行为
全局GlobalWindows自定义触发

Watermark

// 有界乱序
WatermarkStrategy.<Event>forBoundedOutOfOrderness(Duration.ofSeconds(10))
    .withIdleness(Duration.ofMinutes(5))
    .withTimestampAssigner((event, ts) -> event.eventTime());

// 单调递增
WatermarkStrategy.<Event>forMonotonousTimestamps()
    .withTimestampAssigner((event, ts) -> event.eventTime());
策略适用
forBoundedOutOfOrderness有乱序
forMonotonousTimestamps严格递增
noWatermarksProcessing Time

State

// ValueState
ValueStateDescriptor<Long> descriptor = new ValueStateDescriptor<>("count", Types.LONG);
ValueState<Long> state = getRuntimeContext().getState(descriptor);

// ListState
ListStateDescriptor<Event> descriptor = new ListStateDescriptor<>("events", Event.class);
ListState<Event> state = getRuntimeContext().getListState(descriptor);

// MapState
MapStateDescriptor<String, Rule> descriptor = new MapStateDescriptor<>("rules", String.class, Rule.class);
MapState<String, Rule> state = getRuntimeContext().getMapState(descriptor);
状态类型结构接口
ValueStatekey → valuevalue(), update(v)
ListStatekey → Listadd(v), get(), update(list)
MapStatekey → Mapput(k,v), get(k), entries()
ReducingStatekey → reduce 结果add(v)

Checkpoint

env.enableCheckpointing(30000);
env.getCheckpointConfig().setCheckpointingConsistencyMode(
    org.apache.flink.core.execution.CheckpointingMode.EXACTLY_ONCE);
env.getCheckpointConfig().setCheckpointTimeout(60000);
env.getCheckpointConfig().setMinPauseBetweenCheckpoints(10000);
env.getCheckpointConfig().enableUnalignedCheckpoints();
env.getCheckpointConfig().setExternalizedCheckpointCleanup(
    CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);
配置参数作用
enableCheckpointingms开启 + 间隔(默认 EXACTLY_ONCE)
setCheckpointingConsistencyModeEXACTLY_ONCE / AT_LEAST_ONCE语义;旧的 setCheckpointingMode(streaming.api.CheckpointingMode) 已废弃
setCheckpointTimeoutms单次超时,超时即取消并记一次失败
enableUnalignedCheckpoints-降低反压影响;1.20 默认 aligned-checkpoint-timeout=0,开启后每次都直接走非对齐

Metrics

Counter counter = getRuntimeContext().getMetricGroup().counter("name");
Gauge<Double> gauge = getRuntimeContext().getMetricGroup().gauge("name", () -> compute());

Sink

// Kafka Sink
KafkaSink<Event> sink = KafkaSink.<Event>builder()
    .setBootstrapServers("kafka:9092")
    .setRecordSerializer(...)
    .setDeliveryGuarantee(DeliveryGuarantee.EXACTLY_ONCE)
    .setTransactionalIdPrefix("eventd-analytics")  // EXACTLY_ONCE 必填
    .build();
stream.sinkTo(sink);

// 打印
stream.print();

// 自定义 Sink(SinkFunction 在 1.20 已废弃,新代码优先 Sink V2)
stream.addSink(new RichSinkFunction<Event>() {
    @Override
    public void open(OpenContext ctx) { /* 初始化连接 */ }

    @Override
    public void invoke(Event value, Context ctx) { /* 写入 */ }
});

执行

env.execute("job name");        // 阻塞
env.executeAsync("job name");   // 非阻塞

返回 目录 。

继续阅读

探索更多技术文章

浏览归档,发现更多关于系统设计、工具链和工程实践的内容。

全部文章 返回首页

「java」更多文章

  1. 15.3 全书总结与学习路径回顾
  2. 15.2 响应式流处理的演进趋势
  3. 15.1 引擎选型指南