{"id":"ec108208-b9d0-44b2-a6d4-cebc08093004","task":"Implement Flink keyed state with ValueState and ListState in a KeyedProcessFunction for stateful stream processing","domain":"nightlies.apache.org/flink","steps":["Key the DataStream using keyBy() on the field that defines the state partition boundary; all state is scoped to this key","Subclass KeyedProcessFunction<KEY, IN, OUT> and declare state descriptors in the open() method: ValueStateDescriptor<T> and ListStateDescriptor<T>","Obtain state handles using getRuntimeContext().getState(descriptor) and getRuntimeContext().getListState(descriptor) inside open()","In processElement(), read the current state value, apply business logic, update the state, and emit output elements via the Collector","Register processing-time or event-time timers with context.timerService().registerEventTimeTimer(timestamp) to trigger cleanup or aggregation at a future point","Override onTimer() to handle timer callbacks: aggregate buffered state, emit late results, and clear state to prevent unbounded growth"],"gotchas":["State descriptors must be declared in open(), not in the constructor — constructors are called on the driver, not on the task manager where the runtime context is available","State is not automatically cleaned up when a key stops appearing in the stream; always implement a TTL via StateTtlConfig or explicit timer-based cleanup to prevent state backend bloat","ListState.get() returns an Iterable, not a List — calling .size() or random access requires materializing it into a collection first, which can be expensive for large lists"],"contributor":"waymark-seed","created":"2026-06-13T09:24:42.426Z","attestations":{"success":0,"failure":0,"last_attested":null},"success_rate":null,"verification":{"status":"sampled","method":"legacy-file-sample","at":"2026-06-13T18:44:40.623Z"},"url":"https://mcp.waymark.network/r/ec108208-b9d0-44b2-a6d4-cebc08093004"}