【发布时间】:2020-01-08 16:52:26
【问题描述】:
我尝试用不同的窗口大小计算流中的数据(窗口大小在steam数据中),所以我使用自定义的WindowAssigner和AggregateFunction,但状态很大(窗口范围从一小时到30天)
在我看来,聚合状态只是存储中间结果
有什么问题吗?
public class ElementProcessingTime extends WindowAssigner<Element, TimeWindow> {
@Override public Collection<TimeWindow> assignWindows(Element element, long timestamp, WindowAssignerContext context) {
long slide = Time.seconds(10).toMilliseconds();
long size = element.getTime() * 60 * 1000;
timestamp = context.getCurrentProcessingTime();
List<TimeWindow> windows = new ArrayList<>((int) (size / slide));
long lastStart = TimeWindow.getWindowStartWithOffset(timestamp, 0, slide);
for (long start = lastStart; start > timestamp - size; start -= slide) {
windows.add(new TimeWindow(start, start + size));
}
return windows;
}
@Override public Trigger<FactorCalDetail, TimeWindow> getDefaultTrigger(StreamExecutionEnvironment env) {
return ElementTimeTrigger.create();
}
@Override public TypeSerializer<TimeWindow> getWindowSerializer(ExecutionConfig executionConfig) {
return new TimeWindow.Serializer();
}
@Override public boolean isEventTime() {
return false;
}
}
public class CountAggregate implements AggregateFunction<FactorCalDetail, AggregateResult, AggregateResult> {
@Override public AggregateResult createAccumulator() {
AggregateResult result = new AggregateResult();
result.setResult(0.0);
return result;
}
@Override public AggregateResult add(FactorCalDetail value, AggregateResult accumulator) {
accumulator.setKey(value.getGroupKey());
accumulator.addResult();
accumulator.setTimeSpan(value.getTimeSpan());
return accumulator;
}
@Override public AggregateResult getResult(AggregateResult accumulator) {
return accumulator;
}
@Override public AggregateResult merge(AggregateResult a, AggregateResult b) {
if (a.getKey().equals(b.getKey())) {
a.setResult(a.getResult() + b.getResult());
}
return a;
}
}
env.addSource(source)
.keyBy(Element::getKey)
.window(new ElementProcessingTime())
.aggregate(new CountAggregate())
.addSink(new RedisCustomizeSink(redisProperties));
【问题讨论】:
-
如果你使用的是keyed windows,可以使用RocksDB state backend来减少堆压力ci.apache.org/projects/flink/flink-docs-stable/ops/state/…
-
我已经用了RocksDBStateBackend incrementalCheckpointing,但是状态太大了,10000条数据1g左右,受不了(;′⌒`)
-
您看到内存中的状态快速增长吗?您正在运行多少个节点,每个节点上的 CPU / 内存?
-
测试环境k8s pod 1 core 2g
-
Checkpoints 之间的最小暂停为 1m,状态大小快速增长
标签: apache-flink flink-streaming