Java Code Examples for org.apache.flink.queryablestate.client.state.serialization.KvStateSerializer#deserializeList()

The following examples show how to use org.apache.flink.queryablestate.client.state.serialization.KvStateSerializer#deserializeList() . 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: StateBackendTestBase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the value by getting the serialized value and deserializing it
 * if it is not null.
 */
private static <V, K, N> List<V> getSerializedList(
		InternalKvState<K, N, V> kvState,
		K key,
		TypeSerializer<K> keySerializer,
		N namespace,
		TypeSerializer<N> namespaceSerializer,
		TypeSerializer<V> valueSerializer) throws Exception {

	byte[] serializedKeyAndNamespace = KvStateSerializer.serializeKeyAndNamespace(
			key, keySerializer, namespace, namespaceSerializer);

	byte[] serializedValue = kvState.getSerializedValue(
			serializedKeyAndNamespace,
			kvState.getKeySerializer(),
			kvState.getNamespaceSerializer(),
			kvState.getValueSerializer()
	);

	if (serializedValue == null) {
		return null;
	} else {
		return KvStateSerializer.deserializeList(serializedValue, valueSerializer);
	}
}
 
Example 2
Source File: StateBackendTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the value by getting the serialized value and deserializing it
 * if it is not null.
 */
private static <V, K, N> List<V> getSerializedList(
		InternalKvState<K, N, V> kvState,
		K key,
		TypeSerializer<K> keySerializer,
		N namespace,
		TypeSerializer<N> namespaceSerializer,
		TypeSerializer<V> valueSerializer) throws Exception {

	byte[] serializedKeyAndNamespace = KvStateSerializer.serializeKeyAndNamespace(
			key, keySerializer, namespace, namespaceSerializer);

	byte[] serializedValue = kvState.getSerializedValue(
			serializedKeyAndNamespace,
			kvState.getKeySerializer(),
			kvState.getNamespaceSerializer(),
			kvState.getValueSerializer()
	);

	if (serializedValue == null) {
		return null;
	} else {
		return KvStateSerializer.deserializeList(serializedValue, valueSerializer);
	}
}
 
Example 3
Source File: StateBackendTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the value by getting the serialized value and deserializing it
 * if it is not null.
 */
private static <V, K, N> List<V> getSerializedList(
		InternalKvState<K, N, V> kvState,
		K key,
		TypeSerializer<K> keySerializer,
		N namespace,
		TypeSerializer<N> namespaceSerializer,
		TypeSerializer<V> valueSerializer) throws Exception {

	byte[] serializedKeyAndNamespace = KvStateSerializer.serializeKeyAndNamespace(
			key, keySerializer, namespace, namespaceSerializer);

	byte[] serializedValue = kvState.getSerializedValue(
			serializedKeyAndNamespace,
			kvState.getKeySerializer(),
			kvState.getNamespaceSerializer(),
			kvState.getValueSerializer()
	);

	if (serializedValue == null) {
		return null;
	} else {
		return KvStateSerializer.deserializeList(serializedValue, valueSerializer);
	}
}
 
Example 4
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 5
Source File: ImmutableListState.java    From flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <V, T, S extends State> S createState(
	StateDescriptor<S, T> stateDescriptor,
	byte[] serializedState) throws IOException {
	final List<V> state = KvStateSerializer.deserializeList(
		serializedState,
		((ListStateDescriptor<V>) stateDescriptor).getElementSerializer());
	return (S) new ImmutableListState<>(state);
}
 
Example 6
Source File: KvStateRequestSerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests list deserialization with too few bytes.
 */
@Test(expected = IOException.class)
public void testDeserializeListTooShort2() throws Exception {
	// Long + 1 byte (separator) + 1 byte (incomplete Long)
	KvStateSerializer.deserializeList(new byte[] {1, 1, 1, 1, 1, 1, 1, 1, 2, 3},
		LongSerializer.INSTANCE);
}
 
Example 7
Source File: KvStateRequestSerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests list deserialization with too few bytes.
 */
@Test
public void testDeserializeListEmpty() throws Exception {
	List<Long> actualValue = KvStateSerializer
		.deserializeList(new byte[] {}, LongSerializer.INSTANCE);
	assertEquals(0, actualValue.size());
}
 
Example 8
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 9
Source File: ImmutableListState.java    From flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <V, T, S extends State> S createState(
	StateDescriptor<S, T> stateDescriptor,
	byte[] serializedState) throws IOException {
	final List<V> state = KvStateSerializer.deserializeList(
		serializedState,
		((ListStateDescriptor<V>) stateDescriptor).getElementSerializer());
	return (S) new ImmutableListState<>(state);
}
 
Example 10
Source File: KvStateRequestSerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests list deserialization with too few bytes.
 */
@Test(expected = IOException.class)
public void testDeserializeListTooShort2() throws Exception {
	// Long + 1 byte (separator) + 1 byte (incomplete Long)
	KvStateSerializer.deserializeList(new byte[] {1, 1, 1, 1, 1, 1, 1, 1, 2, 3},
		LongSerializer.INSTANCE);
}
 
Example 11
Source File: KvStateRequestSerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests list deserialization with too few bytes.
 */
@Test
public void testDeserializeListEmpty() throws Exception {
	List<Long> actualValue = KvStateSerializer
		.deserializeList(new byte[] {}, LongSerializer.INSTANCE);
	assertEquals(0, actualValue.size());
}
 
Example 12
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 13
Source File: ImmutableListState.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <V, T, S extends State> S createState(
	StateDescriptor<S, T> stateDescriptor,
	byte[] serializedState) throws IOException {
	final List<V> state = KvStateSerializer.deserializeList(
		serializedState,
		((ListStateDescriptor<V>) stateDescriptor).getElementSerializer());
	return (S) new ImmutableListState<>(state);
}
 
Example 14
Source File: KvStateRequestSerializerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Tests list deserialization with too few bytes.
 */
@Test(expected = IOException.class)
public void testDeserializeListTooShort2() throws Exception {
	// Long + 1 byte (separator) + 1 byte (incomplete Long)
	KvStateSerializer.deserializeList(new byte[] {1, 1, 1, 1, 1, 1, 1, 1, 2, 3},
		LongSerializer.INSTANCE);
}
 
Example 15
Source File: KvStateRequestSerializerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Tests list deserialization with too few bytes.
 */
@Test
public void testDeserializeListEmpty() throws Exception {
	List<Long> actualValue = KvStateSerializer
		.deserializeList(new byte[] {}, LongSerializer.INSTANCE);
	assertEquals(0, actualValue.size());
}
 
Example 16
Source File: KvStateRequestSerializerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests list deserialization with too few bytes.
 */
@Test(expected = IOException.class)
public void testDeserializeListTooShort1() throws Exception {
	// 1 byte (incomplete Long)
	KvStateSerializer.deserializeList(new byte[] {1}, LongSerializer.INSTANCE);
}
 
Example 17
Source File: KvStateRequestSerializerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests list deserialization with too few bytes.
 */
@Test(expected = IOException.class)
public void testDeserializeListTooShort1() throws Exception {
	// 1 byte (incomplete Long)
	KvStateSerializer.deserializeList(new byte[] {1}, LongSerializer.INSTANCE);
}
 
Example 18
Source File: KvStateRequestSerializerTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Tests list deserialization with too few bytes.
 */
@Test(expected = IOException.class)
public void testDeserializeListTooShort1() throws Exception {
	// 1 byte (incomplete Long)
	KvStateSerializer.deserializeList(new byte[] {1}, LongSerializer.INSTANCE);
}