Get Started Free
Wade Waldron

Wade Waldron

Staff Software Practice Lead

Working with Keyed State in Flink

Overview

One of the powerful features of Flink is its ability to maintain state in a datastream. This state can be kept local to the operation being performed which can improve performance by eliminating network hops. In this video, we'll introduce keyed state in Flink and show you how you can use it to maintain state across messages and even windows.

Topics:

  • Keyed State
  • Value/List/Map State
  • Reducing State/Aggregating State
  • Descriptors
  • Loading/Updating State

Code

Descriptors

private ValueStateDescriptor<MyClass> descriptor;

@Override
public void open(Configuration parameters) {
	descriptor = new ValueStateDescriptor<>(
		"Identifier", 
		MyClass.class
	);
	descriptor.enableTimeToLive(ttlConfig);
	...
}	

Accessing/Updating State

public void process(...) {
	ValueState<MyClass> state = context
		.globalState()
		.getState(descriptor);
	MyClass value = state.value();

	if(value == null) {
		...
	} else {
		...
	}

	state.update(value);
}

Resources

Use the promo code FLINKJAVA101 to get $25 of free Confluent Cloud usage

Be the first to get updates and new content

We will only share developer content and updates, including notifications when new content is added. We will never send you sales emails. 🙂 By subscribing, you understand we will process your personal information in accordance with our Privacy Statement.