Java Code Examples for org.apache.flink.runtime.state.internal.InternalListState#add()

The following examples show how to use org.apache.flink.runtime.state.internal.InternalListState#add() . 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: KvStateRequestSerializerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that the serialization of a list using the given list state
 * matches the deserialization with {@link KvStateSerializer#deserializeList}.
 *
 * @param key
 * 		key of the list state
 * @param listState
 * 		list state using the {@link VoidNamespace}, must also be a {@link InternalKvState} instance
 *
 * @throws Exception
 */
public static void testListSerialization(
		final long key,
		final InternalListState<Long, VoidNamespace, Long> listState) throws Exception {

	TypeSerializer<Long> valueSerializer = LongSerializer.INSTANCE;
	listState.setCurrentNamespace(VoidNamespace.INSTANCE);

	// List
	final int numElements = 10;

	final List<Long> expectedValues = new ArrayList<>();
	for (int i = 0; i < numElements; i++) {
		final long value = ThreadLocalRandom.current().nextLong();
		expectedValues.add(value);
		listState.add(value);
	}

	final byte[] serializedKey =
		KvStateSerializer.serializeKeyAndNamespace(
			key, LongSerializer.INSTANCE,
			VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE);

	final byte[] serializedValues = listState.getSerializedValue(
			serializedKey,
			listState.getKeySerializer(),
			listState.getNamespaceSerializer(),
			listState.getValueSerializer());

	List<Long> actualValues = KvStateSerializer.deserializeList(serializedValues, valueSerializer);
	assertEquals(expectedValues, actualValues);

	// Single value
	long expectedValue = ThreadLocalRandom.current().nextLong();
	byte[] serializedValue = KvStateSerializer.serializeValue(expectedValue, valueSerializer);
	List<Long> actualValue = KvStateSerializer.deserializeList(serializedValue, valueSerializer);
	assertEquals(1, actualValue.size());
	assertEquals(expectedValue, actualValue.get(0).longValue());
}
 
Example 2
Source File: KvStateRequestSerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that the serialization of a list using the given list state
 * matches the deserialization with {@link KvStateSerializer#deserializeList}.
 *
 * @param key
 * 		key of the list state
 * @param listState
 * 		list state using the {@link VoidNamespace}, must also be a {@link InternalKvState} instance
 *
 * @throws Exception
 */
public static void testListSerialization(
		final long key,
		final InternalListState<Long, VoidNamespace, Long> listState) throws Exception {

	TypeSerializer<Long> valueSerializer = LongSerializer.INSTANCE;
	listState.setCurrentNamespace(VoidNamespace.INSTANCE);

	// List
	final int numElements = 10;

	final List<Long> expectedValues = new ArrayList<>();
	for (int i = 0; i < numElements; i++) {
		final long value = ThreadLocalRandom.current().nextLong();
		expectedValues.add(value);
		listState.add(value);
	}

	final byte[] serializedKey =
		KvStateSerializer.serializeKeyAndNamespace(
			key, LongSerializer.INSTANCE,
			VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE);

	final byte[] serializedValues = listState.getSerializedValue(
			serializedKey,
			listState.getKeySerializer(),
			listState.getNamespaceSerializer(),
			listState.getValueSerializer());

	List<Long> actualValues = KvStateSerializer.deserializeList(serializedValues, valueSerializer);
	assertEquals(expectedValues, actualValues);

	// Single value
	long expectedValue = ThreadLocalRandom.current().nextLong();
	byte[] serializedValue = KvStateSerializer.serializeValue(expectedValue, valueSerializer);
	List<Long> actualValue = KvStateSerializer.deserializeList(serializedValue, valueSerializer);
	assertEquals(1, actualValue.size());
	assertEquals(expectedValue, actualValue.get(0).longValue());
}
 
Example 3
Source File: KvStateRequestSerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that the serialization of a list using the given list state
 * matches the deserialization with {@link KvStateSerializer#deserializeList}.
 *
 * @param key
 * 		key of the list state
 * @param listState
 * 		list state using the {@link VoidNamespace}, must also be a {@link InternalKvState} instance
 *
 * @throws Exception
 */
public static void testListSerialization(
		final long key,
		final InternalListState<Long, VoidNamespace, Long> listState) throws Exception {

	TypeSerializer<Long> valueSerializer = LongSerializer.INSTANCE;
	listState.setCurrentNamespace(VoidNamespace.INSTANCE);

	// List
	final int numElements = 10;

	final List<Long> expectedValues = new ArrayList<>();
	for (int i = 0; i < numElements; i++) {
		final long value = ThreadLocalRandom.current().nextLong();
		expectedValues.add(value);
		listState.add(value);
	}

	final byte[] serializedKey =
		KvStateSerializer.serializeKeyAndNamespace(
			key, LongSerializer.INSTANCE,
			VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE);

	final byte[] serializedValues = listState.getSerializedValue(
			serializedKey,
			listState.getKeySerializer(),
			listState.getNamespaceSerializer(),
			listState.getValueSerializer());

	List<Long> actualValues = KvStateSerializer.deserializeList(serializedValues, valueSerializer);
	assertEquals(expectedValues, actualValues);

	// Single value
	long expectedValue = ThreadLocalRandom.current().nextLong();
	byte[] serializedValue = KvStateSerializer.serializeValue(expectedValue, valueSerializer);
	List<Long> actualValue = KvStateSerializer.deserializeList(serializedValue, valueSerializer);
	assertEquals(1, actualValue.size());
	assertEquals(expectedValue, actualValue.get(0).longValue());
}