A, State
In Flink in accordance with the basic types of State made a division of the following two categories:
Keyed State, the type and state of the related Key, it can only be based on the operation of KeyedStream, the method used. We will be appreciated that this state is logically corresponds to a degree of parallelism and one example of the operation of the Key, <parallel-operator-instance, key>.
Operator State (or non-keyed state), a state in which the type and Key is irrelevant. Accordingly we go from logic understand this concept, which corresponds to a degree of parallelism example, data corresponding to a status. Time because there is no Key concepts involved, the changes occurring in it parallelism (expansion / contraction capacity), the process there will be a re-distribution state data. As shown below:
Keyed State Application examples:
Code Example:
public class StateManager extends RichFlatMapFunction<Tuple2<Long, Long>, Tuple2<Long, Long>> { /** * Operating state of the handle * @param longLongTuple2 * @param collector * @throws Exception */ private transient ValueState<Tuple2<Long, Long>> sum; @Override public void flatMap(Tuple2<Long, Long> value, Collector<Tuple2<Long, Long>> out) throws Exception { // Get the value of state Tuple2 <Long, Long> currentSum = sum.value (); currentSum.f0 = currentSum.f0 + 1; currentSum.f1 = currentSum.f1 + value.f1; // operating state update sum.update (currentSum); // output flatMap operator results IF (currentSum.f0> = 2 ) { out.collect(new Tuple2<Long, Long>(value.f0, currentSum.f1/currentSum.f0)); } } @Override public void open(Configuration parameters) throws Exception { ValueStateDescriptor <Tuple2 <Long, Long >> descriptor = new new ValueStateDescriptor <Tuple2 <Long, Long >> ( " Average " , // name of the state TypeInformation.of ( new new TypeHint <Tuple2 <Long, Long >> () {}) , // type state Tuple2.of ( 0L , 0L ) // initial default state ); sum = getRuntimeContext().getState(descriptor); } }
Operator State Application examples: