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

The following examples show how to use org.apache.flink.api.common.typeutils.base.MapSerializer. 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: RocksDBMapState.java    From flink with 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 File: RocksDBMapState.java    From flink with 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 #3
Source File: RocksDBMapState.java    From Flink-CEPplus with 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 #4
Source File: RocksDBMapState.java    From flink with 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 #5
Source File: RocksDBMapState.java    From flink with 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 #6
Source File: MapStateDescriptor.java    From flink with 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 #7
Source File: RocksDBKeyedStateBackend.java    From flink with 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 #8
Source File: HeapMapState.java    From flink with 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 #9
Source File: MapTypeInfo.java    From Flink-CEPplus with 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 #10
Source File: TtlStateFactory.java    From flink with 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 #11
Source File: TtlMapState.java    From flink with 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 #12
Source File: AbstractMapStateReader.java    From bravo with 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 #13
Source File: MapViewTypeInfo.java    From flink with 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 #14
Source File: MapTypeInfo.java    From flink with 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 #15
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 #16
Source File: MapStateDescriptor.java    From flink with 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 #17
Source File: MapStateDescriptorTest.java    From flink with 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 #18
Source File: RocksDBKeyedStateBackend.java    From flink with 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 #19
Source File: HeapMapState.java    From flink with 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 #20
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 #21
Source File: TtlStateFactory.java    From flink with 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 #22
Source File: TtlMapState.java    From flink with 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 #23
Source File: MapStateDescriptor.java    From flink with 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 #24
Source File: MapStateDescriptorTest.java    From flink with 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 #25
Source File: MapViewTypeInfo.java    From flink with 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 #26
Source File: TtlMapState.java    From Flink-CEPplus with 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 #27
Source File: TtlStateFactory.java    From Flink-CEPplus with 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 #28
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 #29
Source File: HeapMapState.java    From Flink-CEPplus with 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 #30
Source File: MapStateDescriptorTest.java    From Flink-CEPplus with 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());
}