Process Function (Low-level Operations)

The ProcessFunction

The ProcessFunction is a low-level stream processing operation, giving access to the basic building blocks of
all (acyclic) streaming applications:

events (stream elements)

state (fault-tolerant, consistent, only on keyed stream)

timers (event time and processing time, only on keyed stream)

The ProcessFunction can be thought of as a FlatMapFunction with access to keyed state and timers. It handles events
by being invoked for each event received in the input stream(s).

For fault-tolerant state, the ProcessFunction gives access to Flink’s keyed state, accessible via the
RuntimeContext, similar to the way other stateful functions can access keyed state.

The timers allow applications to react to changes in processing time and in event time.
Every call to the function processElement(...) gets a Context object which gives access to the element’s
event time timestamp, and to the TimerService. The TimerService can be used to register callbacks for future
event-/processing-time instants. When a timer’s particular time is reached, the onTimer(...) method is
called. During that call, all states are again scoped to the key with which the timer was created, allowing
timers to manipulate keyed state.

Note If you want to access keyed state and timers you have
to apply the ProcessFunction on a keyed stream:

stream.keyBy(...).process(newMyProcessFunction())

Low-level Joins

To realize low-level operations on two inputs, applications can use CoProcessFunction or KeyedCoProcessFunction. This
function is bound to two different inputs and gets individual calls to processElement1(...) and
processElement2(...) for records from the two different inputs.

Implementing a low level join typically follows this pattern:

Create a state object for one input (or both)

Update the state upon receiving elements from its input

Upon receiving elements from the other input, probe the state and produce the joined result

For example, you might be joining customer data to financial trades,
while keeping state for the customer data. If you care about having
complete and deterministic joins in the face of out-of-order events,
you can use a timer to evaluate and emit the join for a trade when the
watermark for the customer data stream has passed the time of that
trade.

Example

In the following example a KeyedProcessFunction maintains counts per key, and emits a key/count pair whenever a minute passes (in event time) without an update for that key:

The count, key, and last-modification-timestamp are stored in a ValueState, which is implicitly scoped by key.

For each record, the KeyedProcessFunction increments the counter and sets the last-modification timestamp

The function also schedules a callback one minute into the future (in event time)

Upon each callback, it checks the callback’s event time timestamp against the last-modification time of the stored count
and emits the key/count if they match (i.e., no further update occurred during that minute)

Note This simple example could have been implemented with
session windows. We use KeyedProcessFunction here to illustrate the basic pattern it provides.

importorg.apache.flink.api.common.state.ValueState;importorg.apache.flink.api.common.state.ValueStateDescriptor;importorg.apache.flink.api.java.tuple.Tuple;importorg.apache.flink.api.java.tuple.Tuple2;importorg.apache.flink.configuration.Configuration;importorg.apache.flink.streaming.api.functions.KeyedProcessFunction;importorg.apache.flink.streaming.api.functions.KeyedProcessFunction.Context;importorg.apache.flink.streaming.api.functions.KeyedProcessFunction.OnTimerContext;importorg.apache.flink.util.Collector;// the source data streamDataStream<Tuple2<String,String>>stream=...;// apply the process function onto a keyed streamDataStream<Tuple2<String,Long>>result=stream.keyBy(0).process(newCountWithTimeoutFunction());/**
* The data type stored in the state
*/publicclassCountWithTimestamp{publicStringkey;publiclongcount;publiclonglastModified;}/**
* The implementation of the ProcessFunction that maintains the count and timeouts
*/publicclassCountWithTimeoutFunctionextendsKeyedProcessFunction<Tuple,Tuple2<String,String>,Tuple2<String,Long>>{/** The state that is maintained by this process function */privateValueState<CountWithTimestamp>state;@Overridepublicvoidopen(Configurationparameters)throwsException{state=getRuntimeContext().getState(newValueStateDescriptor<>("myState",CountWithTimestamp.class));}@OverridepublicvoidprocessElement(Tuple2<String,String>value,Contextctx,Collector<Tuple2<String,Long>>out)throwsException{// retrieve the current countCountWithTimestampcurrent=state.value();if(current==null){current=newCountWithTimestamp();current.key=value.f0;}// update the state's countcurrent.count++;// set the state's timestamp to the record's assigned event time timestampcurrent.lastModified=ctx.timestamp();// write the state backstate.update(current);// schedule the next timer 60 seconds from the current event timectx.timerService().registerEventTimeTimer(current.lastModified+60000);}@OverridepublicvoidonTimer(longtimestamp,OnTimerContextctx,Collector<Tuple2<String,Long>>out)throwsException{// get the state for the key that scheduled the timerCountWithTimestampresult=state.value();// check if this is an outdated timer or the latest timerif(timestamp==result.lastModified+60000){// emit the state on timeoutout.collect(newTuple2<String,Long>(result.key,result.count));}}}

importorg.apache.flink.api.common.state.ValueStateimportorg.apache.flink.api.common.state.ValueStateDescriptorimportorg.apache.flink.api.java.tuple.Tupleimportorg.apache.flink.streaming.api.functions.KeyedProcessFunctionimportorg.apache.flink.util.Collector// the source data stream
valstream:DataStream[Tuple2[String, String]]=...// apply the process function onto a keyed stream
valresult:DataStream[Tuple2[String, Long]]=stream.keyBy(0).process(newCountWithTimeoutFunction())/**
* The data type stored in the state
*/caseclassCountWithTimestamp(key:String,count:Long,lastModified:Long)/**
* The implementation of the ProcessFunction that maintains the count and timeouts
*/classCountWithTimeoutFunctionextendsKeyedProcessFunction[Tuple, (String, String), (String, Long)]{/** The state that is maintained by this process function */lazyvalstate:ValueState[CountWithTimestamp]=getRuntimeContext.getState(newValueStateDescriptor[CountWithTimestamp]("myState",classOf[CountWithTimestamp]))overridedefprocessElement(value:(String,String),ctx:KeyedProcessFunction[Tuple, (String, String), (String, Long)]#Context,out:Collector[(String, Long)]):Unit={// initialize or retrieve/update the state
valcurrent:CountWithTimestamp=state.valuematch{casenull=>CountWithTimestamp(value._1,1,ctx.timestamp)caseCountWithTimestamp(key,count,lastModified)=>CountWithTimestamp(key,count+1,ctx.timestamp)}// write the state back
state.update(current)// schedule the next timer 60 seconds from the current event time
ctx.timerService.registerEventTimeTimer(current.lastModified+60000)}overridedefonTimer(timestamp:Long,ctx:KeyedProcessFunction[Tuple, (String, String), (String, Long)]#OnTimerContext,out:Collector[(String, Long)]):Unit={state.valuematch{caseCountWithTimestamp(key,count,lastModified)if(timestamp==lastModified+60000)=>out.collect((key,count))case_=>}}}

NOTE: Before Flink 1.4.0, when called from a processing-time timer, the ProcessFunction.onTimer() method sets
the current processing time as event-time timestamp. This behavior is very subtle and might not be noticed by users. Well, it’s
harmful because processing-time timestamps are indeterministic and not aligned with watermarks. Besides, user-implemented logic
depends on this wrong timestamp highly likely is unintendedly faulty. So we’ve decided to fix it. Upon upgrading to 1.4.0, Flink jobs
that are using this incorrect event-time timestamp will fail, and users should adapt their jobs to the correct logic.

The KeyedProcessFunction

KeyedProcessFunction, as an extension of ProcessFunction, gives access to the key of timers in its onTimer(...)
method.

Timers

Both types of timers (processing-time and event-time) are internally maintained by the TimerService and enqueued for execution.

The TimerService deduplicates timers per key and timestamp, i.e., there is at most one timer per key and timestamp. If multiple timers are registered for the same timestamp, the onTimer() method will be called just once.

Note Flink synchronizes invocations of onTimer() and processElement(). Hence, users do not have to worry about concurrent modification of state.

Fault Tolerance

Timers are fault tolerant and checkpointed along with the state of the application.
In case of a failure recovery or when starting an application from a savepoint, the timers are restored.

Note Checkpointed processing-time timers that were supposed to fire before their restoration, will fire immediately.
This might happen when an application recovers from a failure or when it is started from a savepoint.

Note Timers are always asynchronously checkpointed, except for the combination of RocksDB backend / with incremental snapshots / with heap-based timers (will be resolved with FLINK-10026).
Notice that large numbers of timers can increase the checkpointing time because timers are part of the checkpointed state. See the “Timer Coalescing” section for advice on how to reduce the number of timers.

Timer Coalescing

Since Flink maintains only one timer per key and timestamp, you can reduce the number of timers by reducing the timer resolution to coalesce them.

For a timer resolution of 1 second (event or processing time), you
can round down the target time to full seconds. Timers will fire at most 1 second earlier but not later than requested with millisecond accuracy.
As a result, there are at most one timer per key and second.