简体   繁体   English

访问动态变化的 Apache Flink 中的每个键状态存储

[英]Accessing per-key state store in Apache Flink that changes dynamically

I have a stream of messages with different keys.我有一个带有不同键的消息流。 For each key, I want to create an event time session window and do some processing on it only if:对于每个键,我想创建一个事件时间会话窗口并仅在以下情况下对其进行一些处理:

  • MIN_EVENTS number of events has been accumulated in the window (essentially a keyed state) MIN_EVENTS窗口中已累积的事件数(本质上是键控状态)

For each key, MIN_EVENTS is different and might change during runtime .对于每个键, MIN_EVENTS是不同的,并且可能会在运行时发生变化 I am having difficulty implementing this.我很难实现这一点。 In particular, I am implementing this logic like so:特别是,我正在像这样实现这个逻辑:

        inputStream.keyBy(key).
        window(EventTimeSessionWindow(INACTIVITY_PERIOD).
        trigger(new MyCustomCountTrigger()).
        apply(new MyProcessFn())

I am trying to create a custom MyCustomCountTrigger() that should be capable of reading from a state store such as MapState<String, Integer> stateStore that maps key to it's MIN_EVENTS parameter.我正在尝试创建一个自定义MyCustomCountTrigger() ,它应该能够从状态存储中读取,例如MapState<String, Integer> stateStore MIN_EVENTSkey映射到它的MIN_EVENTS参数。 I am aware that I can access a state store using the TriggerContext ctx object that is available to all Triggers.我知道我可以使用可用于所有触发器的TriggerContext ctx对象访问状态存储。

How do I initialize this state store from outside the CountTrigger() class?如何从 CountTrigger() 类外部初始化此状态存储? I haven't been able to find examples to do so.我一直无法找到这样做的例子。

You can initialize the state based on parameters sent to the constructor of your Trigger class.您可以根据发送到 Trigger 类的构造函数的参数来初始化状态。 But you can't access the state from outside that class.但是您不能从该类之外访问该状态。

If you need more flexibility, I suggest you use a process function instead of a window.如果您需要更大的灵活性,我建议您使用进程函数而不是窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM