[英]How to configure flink jobs at runtime?
是否可以在运行时配置flink应用程序? 例如,我有一个流应用程序,该应用程序读取输入,进行一些转换,然后过滤掉低于某个阈值的所有元素。 但是,我希望此阈值在运行时是可配置的,这意味着我可以更改此阈值而不必重新启动flink作业。 示例代码:
DataStream<MyModel> myModelDataStream = // get input ...
// do some stuff ...
.filter(new RichFilterFunction<MyModel>() {
@Override
public boolean filter(MyModel value) throws Exception {
return value.someValue() > someGlobalState.getThreshold();
}
})
// write to some sink ...
DataStream<MyConfig> myConfigDataStream = // get input ...
// ...
.process(new RichProcessFunction<MyConfig>() {
someGlobalState.setThreshold(MyConfig.getThreshold());
})
// ...
是否有可能实现这一目标? 例如,可以通过配置流更改的全局状态。
是的,您可以使用BroadcastProcessFunction
做到这一点。 大概是这样的:
MapStateDescriptor<Void, Threshold> bcStateDescriptor = new MapStateDescriptor<>(
"thresholds", Types.VOID, Threshold.class);
DataStream<MyModel> myModelDataStream = // get input ...
DataStream<Threshold> thresholds = // get input...
BroadcastStream<Threshold> controlStream = thresholds.broadcast(bcStateDescriptor);
DataStream<MyModel> result = myModelDataStream
.connect(controlStream)
.process(new MyFunction());
public class MyFunction extends BroadcastProcessFunction<MyModel, Long, MyModel> {
@Override
public void processBroadcastElement(Threshold newthreshold, Context ctx, Collector<MyModel> out) {
BroadcastState<Void, Threshold> bcState = ctx.getBroadcastState(new MapStateDescriptor<>("thresholds", Types.VOID, Threshold.class));
bcState.put(null, newthreshold);
}
@Override
public void processElement(MyModel model, Collector<MyModel> out) {
Threshold threshold = ctx.getBroadcastState(new MapStateDescriptor<>("threshold", Types.VOID, Threshold.class)).get(null);
if (threshold.value() == null || model.getData() > threshold.value()) {
out.collect(model);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.