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

The following examples show how to use org.apache.flink.queryablestate.client.state.serialization.KvStateSerializer#deserializeKeyAndNamespace() . 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: AbstractHeapState.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] getSerializedValue(
		final byte[] serializedKeyAndNamespace,
		final TypeSerializer<K> safeKeySerializer,
		final TypeSerializer<N> safeNamespaceSerializer,
		final TypeSerializer<SV> safeValueSerializer) throws Exception {

	Preconditions.checkNotNull(serializedKeyAndNamespace);
	Preconditions.checkNotNull(safeKeySerializer);
	Preconditions.checkNotNull(safeNamespaceSerializer);
	Preconditions.checkNotNull(safeValueSerializer);

	Tuple2<K, N> keyAndNamespace = KvStateSerializer.deserializeKeyAndNamespace(
			serializedKeyAndNamespace, safeKeySerializer, safeNamespaceSerializer);

	SV result = stateTable.get(keyAndNamespace.f0, keyAndNamespace.f1);

	if (result == null) {
		return null;
	}
	return KvStateSerializer.serializeValue(result, safeValueSerializer);
}
 
Example 2
Source File: AbstractRocksDBState.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] getSerializedValue(
		final byte[] serializedKeyAndNamespace,
		final TypeSerializer<K> safeKeySerializer,
		final TypeSerializer<N> safeNamespaceSerializer,
		final TypeSerializer<V> safeValueSerializer) throws Exception {

	//TODO make KvStateSerializer key-group aware to save this round trip and key-group computation
	Tuple2<K, N> keyAndNamespace = KvStateSerializer.deserializeKeyAndNamespace(
			serializedKeyAndNamespace, safeKeySerializer, safeNamespaceSerializer);

	int keyGroup = KeyGroupRangeAssignment.assignToKeyGroup(keyAndNamespace.f0, backend.getNumberOfKeyGroups());

	RocksDBSerializedCompositeKeyBuilder<K> keyBuilder =
					new RocksDBSerializedCompositeKeyBuilder<>(
						safeKeySerializer,
						backend.getKeyGroupPrefixBytes(),
						32
					);
	keyBuilder.setKeyAndKeyGroup(keyAndNamespace.f0, keyGroup);
	byte[] key = keyBuilder.buildCompositeKeyNamespace(keyAndNamespace.f1, namespaceSerializer);
	return backend.db.get(columnFamily, key);
}
 
Example 3
Source File: KvStateRequestSerializerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests key and namespace serialization utils.
 */
@Test
public void testKeyAndNamespaceSerialization() throws Exception {
	TypeSerializer<Long> keySerializer = LongSerializer.INSTANCE;
	TypeSerializer<String> namespaceSerializer = StringSerializer.INSTANCE;

	long expectedKey = Integer.MAX_VALUE + 12323L;
	String expectedNamespace = "knilf";

	byte[] serializedKeyAndNamespace = KvStateSerializer.serializeKeyAndNamespace(
			expectedKey, keySerializer, expectedNamespace, namespaceSerializer);

	Tuple2<Long, String> actual = KvStateSerializer.deserializeKeyAndNamespace(
			serializedKeyAndNamespace, keySerializer, namespaceSerializer);

	assertEquals(expectedKey, actual.f0.longValue());
	assertEquals(expectedNamespace, actual.f1);
}
 
Example 4
Source File: KvStateRequestSerializerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests key and namespace serialization utils.
 */
@Test
public void testKeyAndNamespaceSerialization() throws Exception {
	TypeSerializer<Long> keySerializer = LongSerializer.INSTANCE;
	TypeSerializer<String> namespaceSerializer = StringSerializer.INSTANCE;

	long expectedKey = Integer.MAX_VALUE + 12323L;
	String expectedNamespace = "knilf";

	byte[] serializedKeyAndNamespace = KvStateSerializer.serializeKeyAndNamespace(
			expectedKey, keySerializer, expectedNamespace, namespaceSerializer);

	Tuple2<Long, String> actual = KvStateSerializer.deserializeKeyAndNamespace(
			serializedKeyAndNamespace, keySerializer, namespaceSerializer);

	assertEquals(expectedKey, actual.f0.longValue());
	assertEquals(expectedNamespace, actual.f1);
}
 
Example 5
Source File: AbstractRocksDBState.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] getSerializedValue(
		final byte[] serializedKeyAndNamespace,
		final TypeSerializer<K> safeKeySerializer,
		final TypeSerializer<N> safeNamespaceSerializer,
		final TypeSerializer<V> safeValueSerializer) throws Exception {

	//TODO make KvStateSerializer key-group aware to save this round trip and key-group computation
	Tuple2<K, N> keyAndNamespace = KvStateSerializer.deserializeKeyAndNamespace(
			serializedKeyAndNamespace, safeKeySerializer, safeNamespaceSerializer);

	int keyGroup = KeyGroupRangeAssignment.assignToKeyGroup(keyAndNamespace.f0, backend.getNumberOfKeyGroups());

	RocksDBSerializedCompositeKeyBuilder<K> keyBuilder =
					new RocksDBSerializedCompositeKeyBuilder<>(
						safeKeySerializer,
						backend.getKeyGroupPrefixBytes(),
						32
					);
	keyBuilder.setKeyAndKeyGroup(keyAndNamespace.f0, keyGroup);
	byte[] key = keyBuilder.buildCompositeKeyNamespace(keyAndNamespace.f1, namespaceSerializer);
	return backend.db.get(columnFamily, key);
}
 
Example 6
Source File: HeapMapState.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] getSerializedValue(
		final byte[] serializedKeyAndNamespace,
		final TypeSerializer<K> safeKeySerializer,
		final TypeSerializer<N> safeNamespaceSerializer,
		final TypeSerializer<Map<UK, UV>> safeValueSerializer) throws Exception {

	Preconditions.checkNotNull(serializedKeyAndNamespace);
	Preconditions.checkNotNull(safeKeySerializer);
	Preconditions.checkNotNull(safeNamespaceSerializer);
	Preconditions.checkNotNull(safeValueSerializer);

	Tuple2<K, N> keyAndNamespace = KvStateSerializer.deserializeKeyAndNamespace(
			serializedKeyAndNamespace, safeKeySerializer, safeNamespaceSerializer);

	Map<UK, UV> result = stateTable.get(keyAndNamespace.f0, keyAndNamespace.f1);

	if (result == null) {
		return null;
	}

	final MapSerializer<UK, UV> serializer = (MapSerializer<UK, UV>) safeValueSerializer;

	final TypeSerializer<UK> dupUserKeySerializer = serializer.getKeySerializer();
	final TypeSerializer<UV> dupUserValueSerializer = serializer.getValueSerializer();

	return KvStateSerializer.serializeMap(result.entrySet(), dupUserKeySerializer, dupUserValueSerializer);
}
 
Example 7
Source File: KvStateRequestSerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests key and namespace deserialization utils with too many bytes.
 */
@Test(expected = IOException.class)
public void testKeyAndNamespaceDeserializationTooMany2() throws Exception {
	// Long + null String + 2 bytes
	KvStateSerializer.deserializeKeyAndNamespace(
		new byte[] {1, 1, 1, 1, 1, 1, 1, 1, 42, 0, 2, 2}, LongSerializer.INSTANCE,
		StringSerializer.INSTANCE);
}
 
Example 8
Source File: KvStateRequestSerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests key and namespace deserialization utils with too many bytes.
 */
@Test(expected = IOException.class)
public void testKeyAndNamespaceDeserializationTooMany1() throws Exception {
	// Long + null String + 1 byte
	KvStateSerializer.deserializeKeyAndNamespace(
		new byte[] {1, 1, 1, 1, 1, 1, 1, 1, 42, 0, 2}, LongSerializer.INSTANCE,
		StringSerializer.INSTANCE);
}
 
Example 9
Source File: KvStateRequestSerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests key and namespace deserialization utils with too many bytes.
 */
@Test(expected = IOException.class)
public void testKeyAndNamespaceDeserializationTooMany1() throws Exception {
	// Long + null String + 1 byte
	KvStateSerializer.deserializeKeyAndNamespace(
		new byte[] {1, 1, 1, 1, 1, 1, 1, 1, 42, 0, 2}, LongSerializer.INSTANCE,
		StringSerializer.INSTANCE);
}
 
Example 10
Source File: KvStateRequestSerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests key and namespace deserialization utils with too many bytes.
 */
@Test(expected = IOException.class)
public void testKeyAndNamespaceDeserializationTooMany2() throws Exception {
	// Long + null String + 2 bytes
	KvStateSerializer.deserializeKeyAndNamespace(
		new byte[] {1, 1, 1, 1, 1, 1, 1, 1, 42, 0, 2, 2}, LongSerializer.INSTANCE,
		StringSerializer.INSTANCE);
}
 
Example 11
Source File: HeapListState.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] getSerializedValue(
		final byte[] serializedKeyAndNamespace,
		final TypeSerializer<K> safeKeySerializer,
		final TypeSerializer<N> safeNamespaceSerializer,
		final TypeSerializer<List<V>> safeValueSerializer) throws Exception {

	Preconditions.checkNotNull(serializedKeyAndNamespace);
	Preconditions.checkNotNull(safeKeySerializer);
	Preconditions.checkNotNull(safeNamespaceSerializer);
	Preconditions.checkNotNull(safeValueSerializer);

	Tuple2<K, N> keyAndNamespace = KvStateSerializer.deserializeKeyAndNamespace(
			serializedKeyAndNamespace, safeKeySerializer, safeNamespaceSerializer);

	List<V> result = stateTable.get(keyAndNamespace.f0, keyAndNamespace.f1);

	if (result == null) {
		return null;
	}

	final TypeSerializer<V> dupSerializer = ((ListSerializer<V>) safeValueSerializer).getElementSerializer();

	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	DataOutputViewStreamWrapper view = new DataOutputViewStreamWrapper(baos);

	// write the same as RocksDB writes lists, with one ',' separator
	for (int i = 0; i < result.size(); i++) {
		dupSerializer.serialize(result.get(i), view);
		if (i < result.size() -1) {
			view.writeByte(',');
		}
	}
	view.flush();

	return baos.toByteArray();
}
 
Example 12
Source File: KvStateRequestSerializerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Tests key and namespace deserialization utils with too many bytes.
 */
@Test(expected = IOException.class)
public void testKeyAndNamespaceDeserializationTooMany2() throws Exception {
	// Long + null String + 2 bytes
	KvStateSerializer.deserializeKeyAndNamespace(
		new byte[] {1, 1, 1, 1, 1, 1, 1, 1, 42, 0, 2, 2}, LongSerializer.INSTANCE,
		StringSerializer.INSTANCE);
}
 
Example 13
Source File: KvStateRequestSerializerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Tests key and namespace deserialization utils with too many bytes.
 */
@Test(expected = IOException.class)
public void testKeyAndNamespaceDeserializationTooMany1() throws Exception {
	// Long + null String + 1 byte
	KvStateSerializer.deserializeKeyAndNamespace(
		new byte[] {1, 1, 1, 1, 1, 1, 1, 1, 42, 0, 2}, LongSerializer.INSTANCE,
		StringSerializer.INSTANCE);
}
 
Example 14
Source File: HeapMapState.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] getSerializedValue(
		final byte[] serializedKeyAndNamespace,
		final TypeSerializer<K> safeKeySerializer,
		final TypeSerializer<N> safeNamespaceSerializer,
		final TypeSerializer<Map<UK, UV>> safeValueSerializer) throws Exception {

	Preconditions.checkNotNull(serializedKeyAndNamespace);
	Preconditions.checkNotNull(safeKeySerializer);
	Preconditions.checkNotNull(safeNamespaceSerializer);
	Preconditions.checkNotNull(safeValueSerializer);

	Tuple2<K, N> keyAndNamespace = KvStateSerializer.deserializeKeyAndNamespace(
			serializedKeyAndNamespace, safeKeySerializer, safeNamespaceSerializer);

	Map<UK, UV> result = stateTable.get(keyAndNamespace.f0, keyAndNamespace.f1);

	if (result == null) {
		return null;
	}

	final MapSerializer<UK, UV> serializer = (MapSerializer<UK, UV>) safeValueSerializer;

	final TypeSerializer<UK> dupUserKeySerializer = serializer.getKeySerializer();
	final TypeSerializer<UV> dupUserValueSerializer = serializer.getValueSerializer();

	return KvStateSerializer.serializeMap(result.entrySet(), dupUserKeySerializer, dupUserValueSerializer);
}
 
Example 15
Source File: RocksDBMapState.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] getSerializedValue(
		final byte[] serializedKeyAndNamespace,
		final TypeSerializer<K> safeKeySerializer,
		final TypeSerializer<N> safeNamespaceSerializer,
		final TypeSerializer<Map<UK, UV>> safeValueSerializer) throws Exception {

	Preconditions.checkNotNull(serializedKeyAndNamespace);
	Preconditions.checkNotNull(safeKeySerializer);
	Preconditions.checkNotNull(safeNamespaceSerializer);
	Preconditions.checkNotNull(safeValueSerializer);

	//TODO make KvStateSerializer key-group aware to save this round trip and key-group computation
	Tuple2<K, N> keyAndNamespace = KvStateSerializer.deserializeKeyAndNamespace(
			serializedKeyAndNamespace, safeKeySerializer, safeNamespaceSerializer);

	int keyGroup = KeyGroupRangeAssignment.assignToKeyGroup(keyAndNamespace.f0, backend.getNumberOfKeyGroups());

	RocksDBSerializedCompositeKeyBuilder<K> keyBuilder =
		new RocksDBSerializedCompositeKeyBuilder<>(
			safeKeySerializer,
			backend.getKeyGroupPrefixBytes(),
			32);

	keyBuilder.setKeyAndKeyGroup(keyAndNamespace.f0, keyGroup);

	final byte[] keyPrefixBytes = keyBuilder.buildCompositeKeyNamespace(keyAndNamespace.f1, namespaceSerializer);

	final MapSerializer<UK, UV> serializer = (MapSerializer<UK, UV>) safeValueSerializer;

	final TypeSerializer<UK> dupUserKeySerializer = serializer.getKeySerializer();
	final TypeSerializer<UV> dupUserValueSerializer = serializer.getValueSerializer();
	final DataInputDeserializer inputView = new DataInputDeserializer();

	final Iterator<Map.Entry<UK, UV>> iterator = new RocksDBMapIterator<Map.Entry<UK, UV>>(
			backend.db,
			keyPrefixBytes,
			dupUserKeySerializer,
			dupUserValueSerializer,
			inputView
		) {

		@Override
		public Map.Entry<UK, UV> next() {
			return nextEntry();
		}
	};

	// Return null to make the behavior consistent with other backends
	if (!iterator.hasNext()) {
		return null;
	}

	return KvStateSerializer.serializeMap(() -> iterator, dupUserKeySerializer, dupUserValueSerializer);
}
 
Example 16
Source File: RocksDBMapState.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] getSerializedValue(
		final byte[] serializedKeyAndNamespace,
		final TypeSerializer<K> safeKeySerializer,
		final TypeSerializer<N> safeNamespaceSerializer,
		final TypeSerializer<Map<UK, UV>> safeValueSerializer) throws Exception {

	Preconditions.checkNotNull(serializedKeyAndNamespace);
	Preconditions.checkNotNull(safeKeySerializer);
	Preconditions.checkNotNull(safeNamespaceSerializer);
	Preconditions.checkNotNull(safeValueSerializer);

	//TODO make KvStateSerializer key-group aware to save this round trip and key-group computation
	Tuple2<K, N> keyAndNamespace = KvStateSerializer.deserializeKeyAndNamespace(
			serializedKeyAndNamespace, safeKeySerializer, safeNamespaceSerializer);

	int keyGroup = KeyGroupRangeAssignment.assignToKeyGroup(keyAndNamespace.f0, backend.getNumberOfKeyGroups());

	RocksDBSerializedCompositeKeyBuilder<K> keyBuilder =
		new RocksDBSerializedCompositeKeyBuilder<>(
			safeKeySerializer,
			backend.getKeyGroupPrefixBytes(),
			32);

	keyBuilder.setKeyAndKeyGroup(keyAndNamespace.f0, keyGroup);

	final byte[] keyPrefixBytes = keyBuilder.buildCompositeKeyNamespace(keyAndNamespace.f1, namespaceSerializer);

	final MapSerializer<UK, UV> serializer = (MapSerializer<UK, UV>) safeValueSerializer;

	final TypeSerializer<UK> dupUserKeySerializer = serializer.getKeySerializer();
	final TypeSerializer<UV> dupUserValueSerializer = serializer.getValueSerializer();
	final DataInputDeserializer inputView = new DataInputDeserializer();

	final Iterator<Map.Entry<UK, UV>> iterator = new RocksDBMapIterator<Map.Entry<UK, UV>>(
			backend.db,
			keyPrefixBytes,
			dupUserKeySerializer,
			dupUserValueSerializer,
			inputView
		) {

		@Override
		public Map.Entry<UK, UV> next() {
			return nextEntry();
		}
	};

	// Return null to make the behavior consistent with other backends
	if (!iterator.hasNext()) {
		return null;
	}

	return KvStateSerializer.serializeMap(() -> iterator, dupUserKeySerializer, dupUserValueSerializer);
}
 
Example 17
Source File: KvStateRequestSerializerTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Tests key and namespace deserialization utils with too few bytes.
 */
@Test(expected = IOException.class)
public void testKeyAndNamespaceDeserializationEmpty() throws Exception {
	KvStateSerializer.deserializeKeyAndNamespace(
		new byte[] {}, LongSerializer.INSTANCE, StringSerializer.INSTANCE);
}
 
Example 18
Source File: KvStateRequestSerializerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests key and namespace deserialization utils with too few bytes.
 */
@Test(expected = IOException.class)
public void testKeyAndNamespaceDeserializationTooShort() throws Exception {
	KvStateSerializer.deserializeKeyAndNamespace(
		new byte[] {1}, LongSerializer.INSTANCE, StringSerializer.INSTANCE);
}
 
Example 19
Source File: KvStateRequestSerializerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests key and namespace deserialization utils with too few bytes.
 */
@Test(expected = IOException.class)
public void testKeyAndNamespaceDeserializationEmpty() throws Exception {
	KvStateSerializer.deserializeKeyAndNamespace(
		new byte[] {}, LongSerializer.INSTANCE, StringSerializer.INSTANCE);
}
 
Example 20
Source File: RocksDBMapState.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] getSerializedValue(
		final byte[] serializedKeyAndNamespace,
		final TypeSerializer<K> safeKeySerializer,
		final TypeSerializer<N> safeNamespaceSerializer,
		final TypeSerializer<Map<UK, UV>> safeValueSerializer) throws Exception {

	Preconditions.checkNotNull(serializedKeyAndNamespace);
	Preconditions.checkNotNull(safeKeySerializer);
	Preconditions.checkNotNull(safeNamespaceSerializer);
	Preconditions.checkNotNull(safeValueSerializer);

	//TODO make KvStateSerializer key-group aware to save this round trip and key-group computation
	Tuple2<K, N> keyAndNamespace = KvStateSerializer.deserializeKeyAndNamespace(
			serializedKeyAndNamespace, safeKeySerializer, safeNamespaceSerializer);

	int keyGroup = KeyGroupRangeAssignment.assignToKeyGroup(keyAndNamespace.f0, backend.getNumberOfKeyGroups());

	RocksDBSerializedCompositeKeyBuilder<K> keyBuilder =
		new RocksDBSerializedCompositeKeyBuilder<>(
			safeKeySerializer,
			backend.getKeyGroupPrefixBytes(),
			32);

	keyBuilder.setKeyAndKeyGroup(keyAndNamespace.f0, keyGroup);

	final byte[] keyPrefixBytes = keyBuilder.buildCompositeKeyNamespace(keyAndNamespace.f1, namespaceSerializer);

	final MapSerializer<UK, UV> serializer = (MapSerializer<UK, UV>) safeValueSerializer;

	final TypeSerializer<UK> dupUserKeySerializer = serializer.getKeySerializer();
	final TypeSerializer<UV> dupUserValueSerializer = serializer.getValueSerializer();
	final DataInputDeserializer inputView = new DataInputDeserializer();

	final Iterator<Map.Entry<UK, UV>> iterator = new RocksDBMapIterator<Map.Entry<UK, UV>>(
			backend.db,
			keyPrefixBytes,
			dupUserKeySerializer,
			dupUserValueSerializer,
			inputView
		) {

		@Override
		public Map.Entry<UK, UV> next() {
			return nextEntry();
		}
	};

	// Return null to make the behavior consistent with other backends
	if (!iterator.hasNext()) {
		return null;
	}

	return KvStateSerializer.serializeMap(() -> iterator, dupUserKeySerializer, dupUserValueSerializer);
}