Search code examples
apache-flinkflink-streaming

coProcessFunction vs keyedCoProcessFunction on connected keyed streams


In referring to Flink code samples, I observed that when connecting two streams using the connect operator, both coProcessFunction and keyedCoProcessFunction are being extended almost interchangeably and identically when operating on keyed streams (overriding processElement1, processElement2, and onTimer), So when operating on keyed streams what is the difference between extending CoProcessFunction as compared to keyedCoProcessFunction to implementent the business logic of the keyed connected stream?

Thank you.


Solution

  • KeyedCoProcessFunction and KeyedProcessFunction were added somewhat recently. The difference compared to the non-keyed flavors is that the current key is available in the Context that gets passed to the various processElement and onTimer methods.

    If you try to use keyed state or timers in a ProcessFunction or CoProcessFunction, it will work if you are actually in a keyed context, and will throw an exception if you are not.