org.apache.flink.api.common.state.MapState Java Examples

The following examples show how to use org.apache.flink.api.common.state.MapState. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PerWindowStateDataViewStore.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public <N, UK, UV> StateMapView<N, UK, UV> getStateMapView(String stateName, MapViewTypeInfo<UK, UV> mapViewTypeInfo) throws Exception {
	MapStateDescriptor<UK, UV> mapStateDescriptor = new MapStateDescriptor<>(
		stateName,
		mapViewTypeInfo.getKeyType(),
		mapViewTypeInfo.getValueType());

	MapState<UK, UV> mapState = keyedStateBackend.getOrCreateKeyedState(windowSerializer, mapStateDescriptor);
	// explict cast to internal state
	InternalMapState<?, N, UK, UV> internalMapState = (InternalMapState<?, N, UK, UV>) mapState;

	if (mapViewTypeInfo.isNullAware()) {
		ValueStateDescriptor<UV> nullStateDescriptor = new ValueStateDescriptor<>(
			stateName + NULL_STATE_POSTFIX,
			mapViewTypeInfo.getValueType());
		ValueState<UV> nullState = keyedStateBackend.getOrCreateKeyedState(windowSerializer, nullStateDescriptor);
		// explict cast to internal state
		InternalValueState<?, N, UV> internalNullState = (InternalValueState<?, N, UV>) nullState;
		return new StateMapView.NamespacedStateMapViewWithKeysNullable<>(internalMapState, internalNullState);
	} else {
		return new StateMapView.NamespacedStateMapViewWithKeysNotNull<>(internalMapState);
	}
}
 
Example #2
Source File: AsyncOperationFailureNotifier.java    From stateful-functions with Apache License 2.0 6 votes vote down vote up
static void fireExpiredAsyncOperations(
    MapStateDescriptor<Long, Message> asyncOperationStateDescriptor,
    Reductions reductions,
    MapState<Long, Message> asyncOperationState,
    KeyedStateBackend<String> keyedStateBackend)
    throws Exception {

  AsyncOperationFailureNotifier asyncOperationFailureNotifier =
      new AsyncOperationFailureNotifier(reductions, asyncOperationState);

  keyedStateBackend.applyToAllKeys(
      VoidNamespace.get(),
      VoidNamespaceSerializer.INSTANCE,
      asyncOperationStateDescriptor,
      asyncOperationFailureNotifier);

  if (asyncOperationFailureNotifier.enqueued()) {
    reductions.processEnvelopes();
  }
}
 
Example #3
Source File: PerKeyStateDataViewStore.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public <N, UK, UV> StateMapView<N, UK, UV> getStateMapView(String stateName, MapViewTypeInfo<UK, UV> mapViewTypeInfo) throws Exception {
	MapStateDescriptor<UK, UV> mapStateDescriptor = new MapStateDescriptor<>(
		stateName,
		mapViewTypeInfo.getKeyType(),
		mapViewTypeInfo.getValueType());

	MapState<UK, UV> mapState = ctx.getMapState(mapStateDescriptor);

	if (mapViewTypeInfo.isNullAware()) {
		ValueStateDescriptor<UV> nullStateDescriptor = new ValueStateDescriptor<>(
			stateName + NULL_STATE_POSTFIX,
			mapViewTypeInfo.getValueType());
		ValueState<UV> nullState = ctx.getState(nullStateDescriptor);
		return new StateMapView.KeyedStateMapViewWithKeysNullable<>(mapState, nullState);
	} else {
		return new StateMapView.KeyedStateMapViewWithKeysNotNull<>(mapState);
	}
}
 
Example #4
Source File: StateBackendTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Verify that an empty {@code MapState} yields {@code null}.
 */
@Test
public void testMapStateDefaultValue() throws Exception {
	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);

	MapStateDescriptor<String, String> kvId = new MapStateDescriptor<>("id", String.class, String.class);

	MapState<String, String> state = backend.getPartitionedState(
			VoidNamespace.INSTANCE,
			VoidNamespaceSerializer.INSTANCE, kvId);

	backend.setCurrentKey(1);
	assertNull(state.entries());

	state.put("Ciao", "Hello");
	state.put("Bello", "Nice");

	assertNotNull(state.entries());
	assertEquals(state.get("Ciao"), "Hello");
	assertEquals(state.get("Bello"), "Nice");

	state.clear();
	assertNull(state.entries());

	backend.dispose();
}
 
Example #5
Source File: MergingWindowSetTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Test merging of a large new window that covers one existing windows.
 */
@Test
public void testMergeLargeWindowCoveringSingleWindow() throws Exception {
	MapState<TimeWindow, TimeWindow> mockState = new HeapMapState<>();

	MergingWindowSet<TimeWindow> windowSet =
			new MergingWindowSet<>(SessionWindowAssigner.withGap(Duration.ofMillis(3)), mockState);
	windowSet.initializeCache("key1");

	TestingMergeFunction mergeFunction = new TestingMergeFunction();

	// add an initial small window

	mergeFunction.reset();
	assertEquals(new TimeWindow(1, 2), windowSet.addWindow(new TimeWindow(1, 2), mergeFunction));
	assertFalse(mergeFunction.hasMerged());
	assertEquals(new TimeWindow(1, 2), windowSet.getStateWindow(new TimeWindow(1, 2)));

	// add a new window that completely covers the existing window

	mergeFunction.reset();
	assertEquals(new TimeWindow(0, 3), windowSet.addWindow(new TimeWindow(0, 3), mergeFunction));
	assertTrue(mergeFunction.hasMerged());
	assertEquals(new TimeWindow(1, 2), windowSet.getStateWindow(new TimeWindow(0, 3)));
}
 
Example #6
Source File: PerWindowStateDataViewStore.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public <N, UK, UV> StateMapView<N, UK, UV> getStateMapView(String stateName, MapViewTypeInfo<UK, UV> mapViewTypeInfo) throws Exception {
	MapStateDescriptor<UK, UV> mapStateDescriptor = new MapStateDescriptor<>(
		stateName,
		mapViewTypeInfo.getKeyType(),
		mapViewTypeInfo.getValueType());

	MapState<UK, UV> mapState = keyedStateBackend.getOrCreateKeyedState(windowSerializer, mapStateDescriptor);
	// explict cast to internal state
	InternalMapState<?, N, UK, UV> internalMapState = (InternalMapState<?, N, UK, UV>) mapState;

	if (mapViewTypeInfo.isNullAware()) {
		ValueStateDescriptor<UV> nullStateDescriptor = new ValueStateDescriptor<>(
			stateName + NULL_STATE_POSTFIX,
			mapViewTypeInfo.getValueType());
		ValueState<UV> nullState = keyedStateBackend.getOrCreateKeyedState(windowSerializer, nullStateDescriptor);
		// explict cast to internal state
		InternalValueState<?, N, UV> internalNullState = (InternalValueState<?, N, UV>) nullState;
		return new StateMapView.NamespacedStateMapViewWithKeysNullable<>(internalMapState, internalNullState);
	} else {
		return new StateMapView.NamespacedStateMapViewWithKeysNotNull<>(internalMapState);
	}
}
 
Example #7
Source File: PerKeyStateDataViewStore.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public <N, UK, UV> StateMapView<N, UK, UV> getStateMapView(String stateName, MapViewTypeInfo<UK, UV> mapViewTypeInfo) throws Exception {
	MapStateDescriptor<UK, UV> mapStateDescriptor = new MapStateDescriptor<>(
		stateName,
		mapViewTypeInfo.getKeyType(),
		mapViewTypeInfo.getValueType());

	MapState<UK, UV> mapState = ctx.getMapState(mapStateDescriptor);

	if (mapViewTypeInfo.isNullAware()) {
		ValueStateDescriptor<UV> nullStateDescriptor = new ValueStateDescriptor<>(
			stateName + NULL_STATE_POSTFIX,
			mapViewTypeInfo.getValueType());
		ValueState<UV> nullState = ctx.getState(nullStateDescriptor);
		return new StateMapView.KeyedStateMapViewWithKeysNullable<>(mapState, nullState);
	} else {
		return new StateMapView.KeyedStateMapViewWithKeysNotNull<>(mapState);
	}
}
 
Example #8
Source File: MergingWindowSetTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Test adding a new window that is identical to an existing window. This should not cause
 * a merge.
 */
@Test
public void testAddingIdenticalWindows() throws Exception {
	MapState<TimeWindow, TimeWindow> mockState = new HeapMapState<>();

	MergingWindowSet<TimeWindow> windowSet =
			new MergingWindowSet<>(SessionWindowAssigner.withGap(Duration.ofMillis(3)), mockState);
	windowSet.initializeCache("key1");

	TestingMergeFunction mergeFunction = new TestingMergeFunction();

	mergeFunction.reset();
	assertEquals(new TimeWindow(1, 2), windowSet.addWindow(new TimeWindow(1, 2), mergeFunction));
	assertFalse(mergeFunction.hasMerged());
	assertEquals(new TimeWindow(1, 2), windowSet.getStateWindow(new TimeWindow(1, 2)));

	mergeFunction.reset();
	assertEquals(new TimeWindow(1, 2), windowSet.addWindow(new TimeWindow(1, 2), mergeFunction));
	assertFalse(mergeFunction.hasMerged());
	assertEquals(new TimeWindow(1, 2), windowSet.getStateWindow(new TimeWindow(1, 2)));
}
 
Example #9
Source File: StateBackendTestBase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testMapStateIsEmpty() throws Exception {
	MapStateDescriptor<Integer, Long> kvId = new MapStateDescriptor<>("id", Integer.class, Long.class);

	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);

	try {
		MapState<Integer, Long> state = backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);
		backend.setCurrentKey(1);
		assertTrue(state.isEmpty());

		int stateSize = 1024;
		for (int i = 0; i < stateSize; i++) {
			state.put(i, i * 2L);
			assertFalse(state.isEmpty());
		}

		for (int i = 0; i < stateSize; i++) {
			assertFalse(state.isEmpty());
			state.remove(i);
		}
		assertTrue(state.isEmpty());

	} finally {
		backend.dispose();
	}
}
 
Example #10
Source File: StreamingRuntimeContextTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static AbstractStreamOperator<?> createMapPlainMockOp() throws Exception {

	AbstractStreamOperator<?> operatorMock = mock(AbstractStreamOperator.class);
	ExecutionConfig config = new ExecutionConfig();

	KeyedStateBackend keyedStateBackend = mock(KeyedStateBackend.class);

	DefaultKeyedStateStore keyedStateStore = new DefaultKeyedStateStore(keyedStateBackend, config);

	when(operatorMock.getExecutionConfig()).thenReturn(config);

	doAnswer(new Answer<MapState<Integer, String>>() {

		@Override
		public MapState<Integer, String> answer(InvocationOnMock invocationOnMock) throws Throwable {
			MapStateDescriptor<Integer, String> descr =
					(MapStateDescriptor<Integer, String>) invocationOnMock.getArguments()[2];

			AbstractKeyedStateBackend<Integer> backend = new MemoryStateBackend().createKeyedStateBackend(
				new DummyEnvironment("test_task", 1, 0),
				new JobID(),
				"test_op",
				IntSerializer.INSTANCE,
				1,
				new KeyGroupRange(0, 0),
				new KvStateRegistry().createTaskRegistry(new JobID(), new JobVertexID()),
				TtlTimeProvider.DEFAULT,
				new UnregisteredMetricsGroup(),
				Collections.emptyList(),
				new CloseableRegistry());
			backend.setCurrentKey(0);
			return backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, descr);
		}
	}).when(keyedStateBackend).getPartitionedState(Matchers.any(), any(TypeSerializer.class), any(MapStateDescriptor.class));

	when(operatorMock.getKeyedStateStore()).thenReturn(keyedStateStore);
	when(operatorMock.getOperatorID()).thenReturn(new OperatorID());
	return operatorMock;
}
 
Example #11
Source File: MultiplexedMapStateAccessor.java    From stateful-functions with Apache License 2.0 5 votes vote down vote up
MultiplexedMapStateAccessor(
    MapState<byte[], byte[]> handle,
    byte[] accessorMapKey,
    TypeSerializer<T> subValueSerializer) {
  this.mapStateHandle = Objects.requireNonNull(handle);
  this.accessorMapKey = Objects.requireNonNull(accessorMapKey);
  this.serializer = new RawSerializer<>(subValueSerializer);
}
 
Example #12
Source File: StreamingRuntimeContextTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testMapStateReturnsEmptyMapByDefault() throws Exception {

	StreamingRuntimeContext context = createMapOperatorRuntimeContext();

	MapStateDescriptor<Integer, String> descr = new MapStateDescriptor<>("name", Integer.class, String.class);
	MapState<Integer, String> state = context.getMapState(descr);

	Iterable<Map.Entry<Integer, String>> value = state.entries();
	assertNotNull(value);
	assertFalse(value.iterator().hasNext());
}
 
Example #13
Source File: AsyncSink.java    From stateful-functions with Apache License 2.0 5 votes vote down vote up
@Inject
AsyncSink(
    @Label("async-operations") MapState<Long, Message> pendingAsyncOperations,
    @Label("checkpoint-lock-executor") Executor asOperator,
    @Label("mailbox-executor") Executor operatorMailbox,
    @Label("reductions") Lazy<Reductions> reductions) {
  this.pendingAsyncOperations = Objects.requireNonNull(pendingAsyncOperations);
  this.asOperator = Objects.requireNonNull(asOperator);
  this.reductions = Objects.requireNonNull(reductions);
  this.operatorMailbox = Objects.requireNonNull(operatorMailbox);
}
 
Example #14
Source File: QsStateClient.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private static MapState<EmailId, EmailInformation> getMapState(
		String jobId,
		QueryableStateClient client,
		MapStateDescriptor<EmailId, EmailInformation> stateDescriptor) throws InterruptedException, ExecutionException {

	CompletableFuture<MapState<EmailId, EmailInformation>> resultFuture =
			client.getKvState(
					JobID.fromHexString(jobId),
					QsConstants.QUERY_NAME,
					QsConstants.KEY, // which key of the keyed state to access
					BasicTypeInfo.STRING_TYPE_INFO,
					stateDescriptor);

	return resultFuture.get();
}
 
Example #15
Source File: AsyncOperationFailureNotifier.java    From stateful-functions with Apache License 2.0 5 votes vote down vote up
@Override
public void process(String key, MapState<Long, Message> state) throws Exception {
  for (Entry<Long, Message> entry : state.entries()) {
    Long futureId = entry.getKey();
    Message metadataMessage = entry.getValue();
    Message adaptor = new AsyncMessageDecorator(asyncOperationState, futureId, metadataMessage);
    reductions.enqueue(adaptor);
    enqueued = true;
  }
}
 
Example #16
Source File: AsyncOperationFailureNotifier.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
@Override
public void process(String key, MapState<Long, Message> state) throws Exception {
  for (Entry<Long, Message> entry : state.entries()) {
    Long futureId = entry.getKey();
    Message metadataMessage = entry.getValue();
    reductions.enqueueAsyncOperationAfterRestore(futureId, metadataMessage);
    enqueued = true;
  }
}
 
Example #17
Source File: IntervalJoinOperator.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private static <T> void addToBuffer(
		final MapState<Long, List<IntervalJoinOperator.BufferEntry<T>>> buffer,
		final T value,
		final long timestamp) throws Exception {
	List<BufferEntry<T>> elemsInBucket = buffer.get(timestamp);
	if (elemsInBucket == null) {
		elemsInBucket = new ArrayList<>();
	}
	elemsInBucket.add(new BufferEntry<>(value, false));
	buffer.put(timestamp, elemsInBucket);
}
 
Example #18
Source File: SerialStateAccess.java    From da-streamingledger with Apache License 2.0 5 votes vote down vote up
SerialStateAccess(StateAccessSpec<InT, K, V> spec, MapState<K, V> state) {
    this.spec = requireNonNull(spec);
    this.state = requireNonNull(state);
    this.keySelector = requireNonNull(spec.keyAccess);
    this.writeOnly = spec.accessType == AccessType.WRITE;
    this.readOnly = spec.accessType == AccessType.READ;
}
 
Example #19
Source File: AsyncMessageDecorator.java    From stateful-functions with Apache License 2.0 5 votes vote down vote up
AsyncMessageDecorator(
    MapState<Long, Message> asyncOperationState, Long futureId, Message metadataMessage) {
  this.futureId = futureId;
  this.pendingAsyncOperations = asyncOperationState;
  this.message = metadataMessage;
  this.throwable = null;
  this.result = null;
  this.restored = true;
}
 
Example #20
Source File: StreamingRuntimeContextTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static AbstractStreamOperator<?> createMapPlainMockOp() throws Exception {

	AbstractStreamOperator<?> operatorMock = mock(AbstractStreamOperator.class);
	ExecutionConfig config = new ExecutionConfig();

	KeyedStateBackend keyedStateBackend = mock(KeyedStateBackend.class);

	DefaultKeyedStateStore keyedStateStore = new DefaultKeyedStateStore(keyedStateBackend, config);

	when(operatorMock.getExecutionConfig()).thenReturn(config);

	doAnswer(new Answer<MapState<Integer, String>>() {

		@Override
		public MapState<Integer, String> answer(InvocationOnMock invocationOnMock) throws Throwable {
			MapStateDescriptor<Integer, String> descr =
					(MapStateDescriptor<Integer, String>) invocationOnMock.getArguments()[2];

			AbstractKeyedStateBackend<Integer> backend = new MemoryStateBackend().createKeyedStateBackend(
				new DummyEnvironment("test_task", 1, 0),
				new JobID(),
				"test_op",
				IntSerializer.INSTANCE,
				1,
				new KeyGroupRange(0, 0),
				new KvStateRegistry().createTaskRegistry(new JobID(), new JobVertexID()),
				TtlTimeProvider.DEFAULT,
				new UnregisteredMetricsGroup(),
				Collections.emptyList(),
				new CloseableRegistry());
			backend.setCurrentKey(0);
			return backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, descr);
		}
	}).when(keyedStateBackend).getPartitionedState(Matchers.any(), any(TypeSerializer.class), any(MapStateDescriptor.class));

	when(operatorMock.getKeyedStateStore()).thenReturn(keyedStateStore);
	when(operatorMock.getOperatorID()).thenReturn(new OperatorID());
	return operatorMock;
}
 
Example #21
Source File: MergingWindowSetTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Test merging of a large new window that covers multiple existing windows.
 */
@Test
public void testMergeLargeWindowCoveringMultipleWindows() throws Exception {
	MapState<TimeWindow, TimeWindow> mockState = new HeapMapState<>();

	MergingWindowSet<TimeWindow> windowSet =
			new MergingWindowSet<>(SessionWindowAssigner.withGap(Duration.ofMillis(3)), mockState);
	windowSet.initializeCache("key1");

	TestingMergeFunction mergeFunction = new TestingMergeFunction();

	// add several non-overlapping initial windoww

	mergeFunction.reset();
	assertEquals(new TimeWindow(1, 3), windowSet.addWindow(new TimeWindow(1, 3), mergeFunction));
	assertFalse(mergeFunction.hasMerged());
	assertEquals(new TimeWindow(1, 3), windowSet.getStateWindow(new TimeWindow(1, 3)));

	mergeFunction.reset();
	assertEquals(new TimeWindow(5, 8), windowSet.addWindow(new TimeWindow(5, 8), mergeFunction));
	assertFalse(mergeFunction.hasMerged());
	assertEquals(new TimeWindow(5, 8), windowSet.getStateWindow(new TimeWindow(5, 8)));

	mergeFunction.reset();
	assertEquals(new TimeWindow(10, 13), windowSet.addWindow(new TimeWindow(10, 13), mergeFunction));
	assertFalse(mergeFunction.hasMerged());
	assertEquals(new TimeWindow(10, 13), windowSet.getStateWindow(new TimeWindow(10, 13)));

	// add a new window that completely covers the existing windows

	mergeFunction.reset();
	assertEquals(new TimeWindow(5, 13), windowSet.addWindow(new TimeWindow(5, 13), mergeFunction));
	assertTrue(mergeFunction.hasMerged());
	assertThat(mergeFunction.mergedStateWindows(), anyOf(
			containsInAnyOrder(new TimeWindow(5, 8)),
			containsInAnyOrder(new TimeWindow(10, 13))));
	assertThat(
			windowSet.getStateWindow(new TimeWindow(5, 13)),
			anyOf(Is.is(new TimeWindow(5, 8)), Is.is(new TimeWindow(10, 13))));
}
 
Example #22
Source File: MergingWindowProcessFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void open(Context<K, W> ctx) throws Exception {
	super.open(ctx);
	MapStateDescriptor<W, W> mappingStateDescriptor = new MapStateDescriptor<>(
		"session-window-mapping",
		windowSerializer,
		windowSerializer);
	MapState<W, W> windowMapping = ctx.getPartitionedState(mappingStateDescriptor);
	this.mergingWindows = new MergingWindowSet<>(windowAssigner, windowMapping);
	this.mergingFunction = new MergingFunctionImpl();
}
 
Example #23
Source File: StateBackendTestBase.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Verify that an empty {@code MapState} yields {@code null}.
 */
@Test
public void testMapStateDefaultValue() throws Exception {
	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);

	MapStateDescriptor<String, String> kvId = new MapStateDescriptor<>("id", String.class, String.class);

	MapState<String, String> state = backend.getPartitionedState(
			VoidNamespace.INSTANCE,
			VoidNamespaceSerializer.INSTANCE, kvId);

	backend.setCurrentKey(1);
	assertNotNull(state.entries());
	assertFalse(state.entries().iterator().hasNext());

	state.put("Ciao", "Hello");
	state.put("Bello", "Nice");

	assertNotNull(state.entries());
	assertEquals(state.get("Ciao"), "Hello");
	assertEquals(state.get("Bello"), "Nice");

	state.clear();
	assertNotNull(state.entries());
	assertFalse(state.entries().iterator().hasNext());

	backend.dispose();
}
 
Example #24
Source File: DefaultKeyedStateStore.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public <UK, UV> MapState<UK, UV> getMapState(MapStateDescriptor<UK, UV> stateProperties) {
	requireNonNull(stateProperties, "The state properties must not be null");
	try {
		stateProperties.initializeSerializerUnlessSet(executionConfig);
		MapState<UK, UV> originalState = getPartitionedState(stateProperties);
		return new UserFacingMapState<>(originalState);
	} catch (Exception e) {
		throw new RuntimeException("Error while getting state", e);
	}
}
 
Example #25
Source File: QsStateClient.java    From flink with Apache License 2.0 5 votes vote down vote up
private static MapState<EmailId, EmailInformation> getMapState(
		String jobId,
		QueryableStateClient client,
		MapStateDescriptor<EmailId, EmailInformation> stateDescriptor) throws InterruptedException, ExecutionException {

	CompletableFuture<MapState<EmailId, EmailInformation>> resultFuture =
			client.getKvState(
					JobID.fromHexString(jobId),
					QsConstants.QUERY_NAME,
					QsConstants.KEY, // which key of the keyed state to access
					BasicTypeInfo.STRING_TYPE_INFO,
					stateDescriptor);

	return resultFuture.get();
}
 
Example #26
Source File: QsStateClient.java    From flink with Apache License 2.0 5 votes vote down vote up
private static MapState<EmailId, EmailInformation> getMapState(
		String jobId,
		QueryableStateClient client,
		MapStateDescriptor<EmailId, EmailInformation> stateDescriptor) throws InterruptedException, ExecutionException {

	CompletableFuture<MapState<EmailId, EmailInformation>> resultFuture =
			client.getKvState(
					JobID.fromHexString(jobId),
					QsConstants.QUERY_NAME,
					QsConstants.KEY, // which key of the keyed state to access
					BasicTypeInfo.STRING_TYPE_INFO,
					stateDescriptor);

	return resultFuture.get();
}
 
Example #27
Source File: MergingWindowSetTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Test merging of a large new window that covers multiple existing windows.
 */
@Test
public void testMergeLargeWindowCoveringMultipleWindows() throws Exception {
	MapState<TimeWindow, TimeWindow> mockState = new HeapMapState<>();

	MergingWindowSet<TimeWindow> windowSet =
			new MergingWindowSet<>(SessionWindowAssigner.withGap(Duration.ofMillis(3)), mockState);
	windowSet.initializeCache("key1");

	TestingMergeFunction mergeFunction = new TestingMergeFunction();

	// add several non-overlapping initial windoww

	mergeFunction.reset();
	assertEquals(new TimeWindow(1, 3), windowSet.addWindow(new TimeWindow(1, 3), mergeFunction));
	assertFalse(mergeFunction.hasMerged());
	assertEquals(new TimeWindow(1, 3), windowSet.getStateWindow(new TimeWindow(1, 3)));

	mergeFunction.reset();
	assertEquals(new TimeWindow(5, 8), windowSet.addWindow(new TimeWindow(5, 8), mergeFunction));
	assertFalse(mergeFunction.hasMerged());
	assertEquals(new TimeWindow(5, 8), windowSet.getStateWindow(new TimeWindow(5, 8)));

	mergeFunction.reset();
	assertEquals(new TimeWindow(10, 13), windowSet.addWindow(new TimeWindow(10, 13), mergeFunction));
	assertFalse(mergeFunction.hasMerged());
	assertEquals(new TimeWindow(10, 13), windowSet.getStateWindow(new TimeWindow(10, 13)));

	// add a new window that completely covers the existing windows

	mergeFunction.reset();
	assertEquals(new TimeWindow(5, 13), windowSet.addWindow(new TimeWindow(5, 13), mergeFunction));
	assertTrue(mergeFunction.hasMerged());
	assertThat(mergeFunction.mergedStateWindows(), anyOf(
			containsInAnyOrder(new TimeWindow(5, 8)),
			containsInAnyOrder(new TimeWindow(10, 13))));
	assertThat(
			windowSet.getStateWindow(new TimeWindow(5, 13)),
			anyOf(Is.is(new TimeWindow(5, 8)), Is.is(new TimeWindow(10, 13))));
}
 
Example #28
Source File: DefaultKeyedStateStore.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public <UK, UV> MapState<UK, UV> getMapState(MapStateDescriptor<UK, UV> stateProperties) {
	requireNonNull(stateProperties, "The state properties must not be null");
	try {
		stateProperties.initializeSerializerUnlessSet(executionConfig);
		MapState<UK, UV> originalState = getPartitionedState(stateProperties);
		return new UserFacingMapState<>(originalState);
	} catch (Exception e) {
		throw new RuntimeException("Error while getting state", e);
	}
}
 
Example #29
Source File: MapStateUvExample.java    From flink-learning with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.enableCheckpointing(TimeUnit.MINUTES.toMillis(1));
        env.setParallelism(2);

        CheckpointConfig checkpointConf = env.getCheckpointConfig();
        checkpointConf.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);
        checkpointConf.enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);

        Properties props = new Properties();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, UvExampleUtil.broker_list);
        props.put(ConsumerConfig.GROUP_ID_CONFIG, "app-uv-stat");

        FlinkKafkaConsumerBase<String> kafkaConsumer = new FlinkKafkaConsumer011<>(
                UvExampleUtil.topic, new SimpleStringSchema(), props)
                .setStartFromGroupOffsets();

        FlinkJedisPoolConfig conf = new FlinkJedisPoolConfig
                .Builder().setHost("192.168.30.244").build();

        env.addSource(kafkaConsumer)
            .map(string -> GsonUtil.fromJson(string, UserVisitWebEvent.class))  // 反序列化 JSON
            .keyBy("date","pageId") // 按照 日期和页面 进行 keyBy
            .map(new RichMapFunction<UserVisitWebEvent, Tuple2<String, Long>>() {
                // 存储当前 key 对应的 userId 集合
                private MapState<String,Boolean> userIdState;
                // 存储当前 key 对应的 UV 值
                private ValueState<Long> uvState;

                @Override
                public Tuple2<String, Long> map(UserVisitWebEvent userVisitWebEvent) throws Exception {
                    // 初始化 uvState
                    if(null == uvState.value()){
                        uvState.update(0L);
                    }
                    // userIdState 中不包含当前访问的 userId,说明该用户今天还未访问过该页面
                    // 则将该 userId put 到 userIdState 中,并把 UV 值 +1
                    if(!userIdState.contains(userVisitWebEvent.getUserId())){
                        userIdState.put(userVisitWebEvent.getUserId(),null);
                        uvState.update(uvState.value() + 1);
                    }
                    // 生成 Redis key,格式为 日期_pageId,如: 20191026_0
                    String redisKey = userVisitWebEvent.getDate() + "_"
                            + userVisitWebEvent.getPageId();
                    System.out.println(redisKey + "   :::   " + uvState.value());
                    return Tuple2.of(redisKey, uvState.value());
                }

                @Override
                public void open(Configuration parameters) throws Exception {
                    super.open(parameters);
                    // 从状态中恢复 userIdState
                    userIdState = getRuntimeContext().getMapState(
                            new MapStateDescriptor<>("userIdState",
                                    TypeInformation.of(new TypeHint<String>() {}),
                                    TypeInformation.of(new TypeHint<Boolean>() {})));
                    // 从状态中恢复 uvState
                    uvState = getRuntimeContext().getState(
                            new ValueStateDescriptor<>("uvState",
                                    TypeInformation.of(new TypeHint<Long>() {})));
                }
            })
            .addSink(new RedisSink<>(conf, new RedisSetSinkMapper()));

        env.execute("Redis Set UV Stat");
    }
 
Example #30
Source File: StreamingRuntimeContextTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testMapStateReturnsEmptyMapByDefault() throws Exception {

	StreamingRuntimeContext context = new StreamingRuntimeContext(
			createMapPlainMockOp(),
			createMockEnvironment(),
			Collections.<String, Accumulator<?, ?>>emptyMap());

	MapStateDescriptor<Integer, String> descr = new MapStateDescriptor<>("name", Integer.class, String.class);
	MapState<Integer, String> state = context.getMapState(descr);

	Iterable<Map.Entry<Integer, String>> value = state.entries();
	assertNotNull(value);
	assertFalse(value.iterator().hasNext());
}