Java Code Examples for org.apache.flink.api.common.typeutils.base.MapSerializer

The following examples show how to use org.apache.flink.api.common.typeutils.base.MapSerializer. These examples are extracted from open source projects. 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 Project: Flink-CEPplus   Source File: RocksDBMapState.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new {@code RocksDBMapState}.
 *
 * @param columnFamily The RocksDB column family that this state is associated to.
 * @param namespaceSerializer The serializer for the namespace.
 * @param valueSerializer The serializer for the state.
 * @param defaultValue The default value for the state.
 * @param backend The backend for which this state is bind to.
 */
private RocksDBMapState(
		ColumnFamilyHandle columnFamily,
		TypeSerializer<N> namespaceSerializer,
		TypeSerializer<Map<UK, UV>> valueSerializer,
		Map<UK, UV> defaultValue,
		RocksDBKeyedStateBackend<K> backend) {

	super(columnFamily, namespaceSerializer, valueSerializer, defaultValue, backend);

	Preconditions.checkState(valueSerializer instanceof MapSerializer, "Unexpected serializer type.");

	MapSerializer<UK, UV> castedMapSerializer = (MapSerializer<UK, UV>) valueSerializer;
	this.userKeySerializer = castedMapSerializer.getKeySerializer();
	this.userValueSerializer = castedMapSerializer.getValueSerializer();
}
 
Example 2
Source Project: flink   Source File: RocksDBMapState.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new {@code RocksDBMapState}.
 *
 * @param columnFamily The RocksDB column family that this state is associated to.
 * @param namespaceSerializer The serializer for the namespace.
 * @param valueSerializer The serializer for the state.
 * @param defaultValue The default value for the state.
 * @param backend The backend for which this state is bind to.
 */
private RocksDBMapState(
		ColumnFamilyHandle columnFamily,
		TypeSerializer<N> namespaceSerializer,
		TypeSerializer<Map<UK, UV>> valueSerializer,
		Map<UK, UV> defaultValue,
		RocksDBKeyedStateBackend<K> backend) {

	super(columnFamily, namespaceSerializer, valueSerializer, defaultValue, backend);

	Preconditions.checkState(valueSerializer instanceof MapSerializer, "Unexpected serializer type.");

	MapSerializer<UK, UV> castedMapSerializer = (MapSerializer<UK, UV>) valueSerializer;
	this.userKeySerializer = castedMapSerializer.getKeySerializer();
	this.userValueSerializer = castedMapSerializer.getValueSerializer();
}
 
Example 3
Source Project: flink   Source File: RocksDBMapState.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void migrateSerializedValue(
	DataInputDeserializer serializedOldValueInput,
	DataOutputSerializer serializedMigratedValueOutput,
	TypeSerializer<Map<UK, UV>> priorSerializer,
	TypeSerializer<Map<UK, UV>> newSerializer) throws StateMigrationException {

	checkArgument(priorSerializer instanceof MapSerializer);
	checkArgument(newSerializer instanceof MapSerializer);

	TypeSerializer<UV> priorMapValueSerializer = ((MapSerializer<UK, UV>) priorSerializer).getValueSerializer();
	TypeSerializer<UV> newMapValueSerializer = ((MapSerializer<UK, UV>) newSerializer).getValueSerializer();

	try {
		boolean isNull = serializedOldValueInput.readBoolean();
		UV mapUserValue = null;
		if (!isNull) {
			mapUserValue = priorMapValueSerializer.deserialize(serializedOldValueInput);
		}
		serializedMigratedValueOutput.writeBoolean(mapUserValue == null);
		newMapValueSerializer.serialize(mapUserValue, serializedMigratedValueOutput);
	} catch (Exception e) {
		throw new StateMigrationException("Error while trying to migrate RocksDB map state.", e);
	}
}
 
Example 4
Source Project: flink   Source File: RocksDBMapState.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new {@code RocksDBMapState}.
 *
 * @param columnFamily The RocksDB column family that this state is associated to.
 * @param namespaceSerializer The serializer for the namespace.
 * @param valueSerializer The serializer for the state.
 * @param defaultValue The default value for the state.
 * @param backend The backend for which this state is bind to.
 */
private RocksDBMapState(
		ColumnFamilyHandle columnFamily,
		TypeSerializer<N> namespaceSerializer,
		TypeSerializer<Map<UK, UV>> valueSerializer,
		Map<UK, UV> defaultValue,
		RocksDBKeyedStateBackend<K> backend) {

	super(columnFamily, namespaceSerializer, valueSerializer, defaultValue, backend);

	Preconditions.checkState(valueSerializer instanceof MapSerializer, "Unexpected serializer type.");

	MapSerializer<UK, UV> castedMapSerializer = (MapSerializer<UK, UV>) valueSerializer;
	this.userKeySerializer = castedMapSerializer.getKeySerializer();
	this.userValueSerializer = castedMapSerializer.getValueSerializer();
}
 
Example 5
Source Project: flink   Source File: RocksDBMapState.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void migrateSerializedValue(
	DataInputDeserializer serializedOldValueInput,
	DataOutputSerializer serializedMigratedValueOutput,
	TypeSerializer<Map<UK, UV>> priorSerializer,
	TypeSerializer<Map<UK, UV>> newSerializer) throws StateMigrationException {

	checkArgument(priorSerializer instanceof MapSerializer);
	checkArgument(newSerializer instanceof MapSerializer);

	TypeSerializer<UV> priorMapValueSerializer = ((MapSerializer<UK, UV>) priorSerializer).getValueSerializer();
	TypeSerializer<UV> newMapValueSerializer = ((MapSerializer<UK, UV>) newSerializer).getValueSerializer();

	try {
		boolean isNull = serializedOldValueInput.readBoolean();
		UV mapUserValue = null;
		if (!isNull) {
			mapUserValue = priorMapValueSerializer.deserialize(serializedOldValueInput);
		}
		serializedMigratedValueOutput.writeBoolean(mapUserValue == null);
		newMapValueSerializer.serialize(mapUserValue, serializedMigratedValueOutput);
	} catch (Exception e) {
		throw new StateMigrationException("Error while trying to migrate RocksDB map state.", e);
	}
}
 
Example 6
Source Project: Flink-CEPplus   Source File: MapTypeInfo.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public TypeSerializer<Map<K, V>> createSerializer(ExecutionConfig config) {
	TypeSerializer<K> keyTypeSerializer = keyTypeInfo.createSerializer(config);
	TypeSerializer<V> valueTypeSerializer = valueTypeInfo.createSerializer(config);
	
	return new MapSerializer<>(keyTypeSerializer, valueTypeSerializer);
}
 
Example 7
Source Project: Flink-CEPplus   Source File: MapStateDescriptor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Gets the serializer for the keys in the state.
 *
 * @return The serializer for the keys in the state.
 */
public TypeSerializer<UK> getKeySerializer() {
	final TypeSerializer<Map<UK, UV>> rawSerializer = getSerializer();
	if (!(rawSerializer instanceof MapSerializer)) {
		throw new IllegalStateException("Unexpected serializer type.");
	}

	return ((MapSerializer<UK, UV>) rawSerializer).getKeySerializer();
}
 
Example 8
Source Project: Flink-CEPplus   Source File: MapStateDescriptor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Gets the serializer for the values in the state.
 *
 * @return The serializer for the values in the state.
 */
public TypeSerializer<UV> getValueSerializer() {
	final TypeSerializer<Map<UK, UV>> rawSerializer = getSerializer();
	if (!(rawSerializer instanceof MapSerializer)) {
		throw new IllegalStateException("Unexpected serializer type.");
	}

	return ((MapSerializer<UK, UV>) rawSerializer).getValueSerializer();
}
 
Example 9
Source Project: Flink-CEPplus   Source File: MapStateDescriptorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testMapStateDescriptor() throws Exception {

	TypeSerializer<Integer> keySerializer = new KryoSerializer<>(Integer.class, new ExecutionConfig());
	TypeSerializer<String> valueSerializer = new KryoSerializer<>(String.class, new ExecutionConfig());

	MapStateDescriptor<Integer, String> descr =
			new MapStateDescriptor<>("testName", keySerializer, valueSerializer);

	assertEquals("testName", descr.getName());
	assertNotNull(descr.getSerializer());
	assertTrue(descr.getSerializer() instanceof MapSerializer);
	assertNotNull(descr.getKeySerializer());
	assertEquals(keySerializer, descr.getKeySerializer());
	assertNotNull(descr.getValueSerializer());
	assertEquals(valueSerializer, descr.getValueSerializer());

	MapStateDescriptor<Integer, String> copy = CommonTestUtils.createCopySerializable(descr);

	assertEquals("testName", copy.getName());
	assertNotNull(copy.getSerializer());
	assertTrue(copy.getSerializer() instanceof MapSerializer);

	assertNotNull(copy.getKeySerializer());
	assertEquals(keySerializer, copy.getKeySerializer());
	assertNotNull(copy.getValueSerializer());
	assertEquals(valueSerializer, copy.getValueSerializer());
}
 
Example 10
Source Project: Flink-CEPplus   Source File: HeapMapState.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new key/value state for the given hash map of key/value pairs.
 *
 * @param stateTable The state table for which this state is associated to.
 * @param keySerializer The serializer for the keys.
 * @param valueSerializer The serializer for the state.
 * @param namespaceSerializer The serializer for the namespace.
 * @param defaultValue The default value for the state.
 */
private HeapMapState(
	StateTable<K, N, Map<UK, UV>> stateTable,
	TypeSerializer<K> keySerializer,
	TypeSerializer<Map<UK, UV>> valueSerializer,
	TypeSerializer<N> namespaceSerializer,
	Map<UK, UV> defaultValue) {
	super(stateTable, keySerializer, valueSerializer, namespaceSerializer, defaultValue);

	Preconditions.checkState(valueSerializer instanceof MapSerializer, "Unexpected serializer type.");
}
 
Example 11
Source Project: Flink-CEPplus   Source File: HeapMapState.java    License: 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 12
Source Project: Flink-CEPplus   Source File: TtlStateFactory.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean isTtlStateSerializer(TypeSerializer<?> typeSerializer) {
	boolean ttlSerializer = typeSerializer instanceof TtlStateFactory.TtlSerializer;
	boolean ttlListSerializer = typeSerializer instanceof ListSerializer &&
		((ListSerializer) typeSerializer).getElementSerializer() instanceof TtlStateFactory.TtlSerializer;
	boolean ttlMapSerializer = typeSerializer instanceof MapSerializer &&
		((MapSerializer) typeSerializer).getValueSerializer() instanceof TtlStateFactory.TtlSerializer;
	return ttlSerializer || ttlListSerializer || ttlMapSerializer;
}
 
Example 13
Source Project: Flink-CEPplus   Source File: TtlMapState.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public Map<UK, TtlValue<UV>> getUnexpiredOrNull(@Nonnull Map<UK, TtlValue<UV>> ttlValue) {
	Map<UK, TtlValue<UV>> unexpired = new HashMap<>();
	TypeSerializer<TtlValue<UV>> valueSerializer =
		((MapSerializer<UK, TtlValue<UV>>) original.getValueSerializer()).getValueSerializer();
	for (Map.Entry<UK, TtlValue<UV>> e : ttlValue.entrySet()) {
		if (!expired(e.getValue())) {
			// we have to do the defensive copy to update the value
			unexpired.put(e.getKey(), valueSerializer.copy(e.getValue()));
		}
	}
	return ttlValue.size() == unexpired.size() ? ttlValue : unexpired;
}
 
Example 14
Source Project: flink   Source File: MapViewTypeInfo.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public TypeSerializer<MapView<K, V>> createSerializer(ExecutionConfig config) {
	if (nullSerializer) {
		return (TypeSerializer<MapView<K, V>>) (TypeSerializer<?>) new NullSerializer();
	} else {
		TypeSerializer<K> keySer = keyType.createSerializer(config);
		TypeSerializer<V> valueSer = valueType.createSerializer(config);
		if (nullAware) {
			return new MapViewSerializer<>(new NullAwareMapSerializer<>(keySer, valueSer));
		} else {
			return new MapViewSerializer<>(new MapSerializer<>(keySer, valueSer));
		}
	}
}
 
Example 15
Source Project: flink   Source File: MapTypeInfo.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public TypeSerializer<Map<K, V>> createSerializer(ExecutionConfig config) {
	TypeSerializer<K> keyTypeSerializer = keyTypeInfo.createSerializer(config);
	TypeSerializer<V> valueTypeSerializer = valueTypeInfo.createSerializer(config);
	
	return new MapSerializer<>(keyTypeSerializer, valueTypeSerializer);
}
 
Example 16
Source Project: flink   Source File: MapStateDescriptor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Gets the serializer for the keys in the state.
 *
 * @return The serializer for the keys in the state.
 */
public TypeSerializer<UK> getKeySerializer() {
	final TypeSerializer<Map<UK, UV>> rawSerializer = getSerializer();
	if (!(rawSerializer instanceof MapSerializer)) {
		throw new IllegalStateException("Unexpected serializer type.");
	}

	return ((MapSerializer<UK, UV>) rawSerializer).getKeySerializer();
}
 
Example 17
Source Project: flink   Source File: MapStateDescriptor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Gets the serializer for the values in the state.
 *
 * @return The serializer for the values in the state.
 */
public TypeSerializer<UV> getValueSerializer() {
	final TypeSerializer<Map<UK, UV>> rawSerializer = getSerializer();
	if (!(rawSerializer instanceof MapSerializer)) {
		throw new IllegalStateException("Unexpected serializer type.");
	}

	return ((MapSerializer<UK, UV>) rawSerializer).getValueSerializer();
}
 
Example 18
Source Project: flink   Source File: MapStateDescriptorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testMapStateDescriptor() throws Exception {

	TypeSerializer<Integer> keySerializer = new KryoSerializer<>(Integer.class, new ExecutionConfig());
	TypeSerializer<String> valueSerializer = new KryoSerializer<>(String.class, new ExecutionConfig());

	MapStateDescriptor<Integer, String> descr =
			new MapStateDescriptor<>("testName", keySerializer, valueSerializer);

	assertEquals("testName", descr.getName());
	assertNotNull(descr.getSerializer());
	assertTrue(descr.getSerializer() instanceof MapSerializer);
	assertNotNull(descr.getKeySerializer());
	assertEquals(keySerializer, descr.getKeySerializer());
	assertNotNull(descr.getValueSerializer());
	assertEquals(valueSerializer, descr.getValueSerializer());

	MapStateDescriptor<Integer, String> copy = CommonTestUtils.createCopySerializable(descr);

	assertEquals("testName", copy.getName());
	assertNotNull(copy.getSerializer());
	assertTrue(copy.getSerializer() instanceof MapSerializer);

	assertNotNull(copy.getKeySerializer());
	assertEquals(keySerializer, copy.getKeySerializer());
	assertNotNull(copy.getValueSerializer());
	assertEquals(valueSerializer, copy.getValueSerializer());
}
 
Example 19
Source Project: flink   Source File: RocksDBKeyedStateBackend.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <UK> boolean checkMapStateKeySchemaCompatibility(
	MapSerializerSnapshot<?, ?> mapStateSerializerSnapshot,
	MapSerializer<?, ?> newMapStateSerializer) {
	TypeSerializerSnapshot<UK> previousKeySerializerSnapshot = (TypeSerializerSnapshot<UK>) mapStateSerializerSnapshot.getKeySerializerSnapshot();
	TypeSerializer<UK> newUserKeySerializer = (TypeSerializer<UK>) newMapStateSerializer.getKeySerializer();

	TypeSerializerSchemaCompatibility<UK> keyCompatibility = previousKeySerializerSnapshot.resolveSchemaCompatibility(newUserKeySerializer);
	return keyCompatibility.isCompatibleAsIs();
}
 
Example 20
Source Project: flink   Source File: HeapMapState.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new key/value state for the given hash map of key/value pairs.
 *
 * @param stateTable The state table for which this state is associated to.
 * @param keySerializer The serializer for the keys.
 * @param valueSerializer The serializer for the state.
 * @param namespaceSerializer The serializer for the namespace.
 * @param defaultValue The default value for the state.
 */
private HeapMapState(
	StateTable<K, N, Map<UK, UV>> stateTable,
	TypeSerializer<K> keySerializer,
	TypeSerializer<Map<UK, UV>> valueSerializer,
	TypeSerializer<N> namespaceSerializer,
	Map<UK, UV> defaultValue) {
	super(stateTable, keySerializer, valueSerializer, namespaceSerializer, defaultValue);

	Preconditions.checkState(valueSerializer instanceof MapSerializer, "Unexpected serializer type.");
}
 
Example 21
Source Project: flink   Source File: HeapMapState.java    License: 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 22
Source Project: flink   Source File: TtlStateFactory.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean isTtlStateSerializer(TypeSerializer<?> typeSerializer) {
	boolean ttlSerializer = typeSerializer instanceof TtlStateFactory.TtlSerializer;
	boolean ttlListSerializer = typeSerializer instanceof ListSerializer &&
		((ListSerializer) typeSerializer).getElementSerializer() instanceof TtlStateFactory.TtlSerializer;
	boolean ttlMapSerializer = typeSerializer instanceof MapSerializer &&
		((MapSerializer) typeSerializer).getValueSerializer() instanceof TtlStateFactory.TtlSerializer;
	return ttlSerializer || ttlListSerializer || ttlMapSerializer;
}
 
Example 23
Source Project: flink   Source File: TtlMapState.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public Map<UK, TtlValue<UV>> getUnexpiredOrNull(@Nonnull Map<UK, TtlValue<UV>> ttlValue) {
	Map<UK, TtlValue<UV>> unexpired = new HashMap<>();
	TypeSerializer<TtlValue<UV>> valueSerializer =
		((MapSerializer<UK, TtlValue<UV>>) original.getValueSerializer()).getValueSerializer();
	for (Map.Entry<UK, TtlValue<UV>> e : ttlValue.entrySet()) {
		if (!expired(e.getValue())) {
			// we have to do the defensive copy to update the value
			unexpired.put(e.getKey(), valueSerializer.copy(e.getValue()));
		}
	}
	return ttlValue.size() == unexpired.size() ? ttlValue : unexpired;
}
 
Example 24
Source Project: bravo   Source File: AbstractMapStateReader.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void configure(int maxParallelism, TypeSerializer<?> keySerializer, TypeSerializer<?> valueSerializer)
		throws Exception {

	if (valueSerializer instanceof MapSerializer) {
		MapSerializer<?, ?> mapSerializer = (MapSerializer<?, ?>) valueSerializer;
		if (mapKeytype == null && mapKeySerializer == null) {
			mapKeySerializer = (TypeSerializer<Object>) mapSerializer.getKeySerializer();
		}

		super.configure(maxParallelism, keySerializer, mapSerializer.getValueSerializer());
	} else {
		throw new RuntimeException("Doesnt seem like a map state");
	}
}
 
Example 25
Source Project: flink   Source File: MapViewTypeInfo.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public TypeSerializer<MapView<K, V>> createSerializer(ExecutionConfig config) {
	if (nullSerializer) {
		return (TypeSerializer<MapView<K, V>>) (TypeSerializer<?>) NullSerializer.INSTANCE;
	} else {
		TypeSerializer<K> keySer = keyType.createSerializer(config);
		TypeSerializer<V> valueSer = valueType.createSerializer(config);
		if (nullAware) {
			return new MapViewSerializer<>(new NullAwareMapSerializer<>(keySer, valueSer));
		} else {
			return new MapViewSerializer<>(new MapSerializer<>(keySer, valueSer));
		}
	}
}
 
Example 26
Source Project: flink   Source File: MapTypeInfo.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public TypeSerializer<Map<K, V>> createSerializer(ExecutionConfig config) {
	TypeSerializer<K> keyTypeSerializer = keyTypeInfo.createSerializer(config);
	TypeSerializer<V> valueTypeSerializer = valueTypeInfo.createSerializer(config);
	
	return new MapSerializer<>(keyTypeSerializer, valueTypeSerializer);
}
 
Example 27
Source Project: flink   Source File: MapStateDescriptor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Gets the serializer for the keys in the state.
 *
 * @return The serializer for the keys in the state.
 */
public TypeSerializer<UK> getKeySerializer() {
	final TypeSerializer<Map<UK, UV>> rawSerializer = getSerializer();
	if (!(rawSerializer instanceof MapSerializer)) {
		throw new IllegalStateException("Unexpected serializer type.");
	}

	return ((MapSerializer<UK, UV>) rawSerializer).getKeySerializer();
}
 
Example 28
Source Project: flink   Source File: MapStateDescriptor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Gets the serializer for the values in the state.
 *
 * @return The serializer for the values in the state.
 */
public TypeSerializer<UV> getValueSerializer() {
	final TypeSerializer<Map<UK, UV>> rawSerializer = getSerializer();
	if (!(rawSerializer instanceof MapSerializer)) {
		throw new IllegalStateException("Unexpected serializer type.");
	}

	return ((MapSerializer<UK, UV>) rawSerializer).getValueSerializer();
}
 
Example 29
Source Project: flink   Source File: MapStateDescriptorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testMapStateDescriptor() throws Exception {

	TypeSerializer<Integer> keySerializer = new KryoSerializer<>(Integer.class, new ExecutionConfig());
	TypeSerializer<String> valueSerializer = new KryoSerializer<>(String.class, new ExecutionConfig());

	MapStateDescriptor<Integer, String> descr =
			new MapStateDescriptor<>("testName", keySerializer, valueSerializer);

	assertEquals("testName", descr.getName());
	assertNotNull(descr.getSerializer());
	assertTrue(descr.getSerializer() instanceof MapSerializer);
	assertNotNull(descr.getKeySerializer());
	assertEquals(keySerializer, descr.getKeySerializer());
	assertNotNull(descr.getValueSerializer());
	assertEquals(valueSerializer, descr.getValueSerializer());

	MapStateDescriptor<Integer, String> copy = CommonTestUtils.createCopySerializable(descr);

	assertEquals("testName", copy.getName());
	assertNotNull(copy.getSerializer());
	assertTrue(copy.getSerializer() instanceof MapSerializer);

	assertNotNull(copy.getKeySerializer());
	assertEquals(keySerializer, copy.getKeySerializer());
	assertNotNull(copy.getValueSerializer());
	assertEquals(valueSerializer, copy.getValueSerializer());
}
 
Example 30
Source Project: flink   Source File: RocksDBKeyedStateBackend.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <UK> boolean checkMapStateKeySchemaCompatibility(
	MapSerializerSnapshot<?, ?> mapStateSerializerSnapshot,
	MapSerializer<?, ?> newMapStateSerializer) {
	TypeSerializerSnapshot<UK> previousKeySerializerSnapshot = (TypeSerializerSnapshot<UK>) mapStateSerializerSnapshot.getKeySerializerSnapshot();
	TypeSerializer<UK> newUserKeySerializer = (TypeSerializer<UK>) newMapStateSerializer.getKeySerializer();

	TypeSerializerSchemaCompatibility<UK> keyCompatibility = previousKeySerializerSnapshot.resolveSchemaCompatibility(newUserKeySerializer);
	return keyCompatibility.isCompatibleAsIs();
}