Java Code Examples for org.apache.flink.api.common.typeutils.TypeSerializerSnapshot#restoreSerializer()

The following examples show how to use org.apache.flink.api.common.typeutils.TypeSerializerSnapshot#restoreSerializer() . 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: LogicalTypeParser.java    From flink with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private LogicalType parseAnyType() {
	nextToken(TokenType.BEGIN_PARAMETER);
	nextToken(TokenType.LITERAL_STRING);
	final String className = tokenAsString();

	nextToken(TokenType.LIST_SEPARATOR);
	nextToken(TokenType.LITERAL_STRING);
	final String serializer = tokenAsString();
	nextToken(TokenType.END_PARAMETER);

	try {
		final Class<?> clazz = Class.forName(className, true, classLoader);
		final byte[] bytes = EncodingUtils.decodeBase64ToBytes(serializer);
		final DataInputDeserializer inputDeserializer = new DataInputDeserializer(bytes);
		final TypeSerializerSnapshot<?> snapshot = TypeSerializerSnapshot.readVersionedSnapshot(
			inputDeserializer,
			classLoader);
		return new AnyType(clazz, snapshot.restoreSerializer());
	} catch (Throwable t) {
		throw parsingError(
			"Unable to restore the ANY type of class '" + className + "' with " +
				"serializer snapshot '" + serializer + "'.", t);
	}
}
 
Example 2
Source File: RawType.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Restores a raw type from the components of a serialized string representation.
 */
@SuppressWarnings({"unchecked", "rawtypes"})
public static RawType<?> restore(
		ClassLoader classLoader,
		String className,
		String serializerString) {
	try {
		final Class<?> clazz = Class.forName(className, true, classLoader);
		final byte[] bytes = EncodingUtils.decodeBase64ToBytes(serializerString);
		final DataInputDeserializer inputDeserializer = new DataInputDeserializer(bytes);
		final TypeSerializerSnapshot<?> snapshot = TypeSerializerSnapshot.readVersionedSnapshot(
			inputDeserializer,
			classLoader);
		return (RawType<?>) new RawType(clazz, snapshot.restoreSerializer());
	} catch (Throwable t) {
		throw new ValidationException(
			String.format(
				"Unable to restore the RAW type of class '%s' with serializer snapshot '%s'.",
				className,
				serializerString),
			t);
	}
}
 
Example 3
Source File: SerializerTestUtil.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Snapshot and restore the given serializer. Returns the restored serializer.
 */
public static <T> TypeSerializer<T> snapshotAndReconfigure(
	TypeSerializer<T> serializer, SerializerGetter<T> serializerGetter) throws IOException {
	TypeSerializerSnapshot<T> configSnapshot = serializer.snapshotConfiguration();

	byte[] serializedConfig;
	try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
		TypeSerializerSnapshotSerializationUtil.writeSerializerSnapshot(
			new DataOutputViewStreamWrapper(out), configSnapshot, serializer);
		serializedConfig = out.toByteArray();
	}

	TypeSerializerSnapshot<T> restoredConfig;
	try (ByteArrayInputStream in = new ByteArrayInputStream(serializedConfig)) {
		restoredConfig = TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(
			new DataInputViewStreamWrapper(in),
			Thread.currentThread().getContextClassLoader(),
			serializerGetter.getSerializer());
	}

	TypeSerializerSchemaCompatibility<T> strategy =
		restoredConfig.resolveSchemaCompatibility(serializerGetter.getSerializer());
	final TypeSerializer<T> restoredSerializer;
	if (strategy.isCompatibleAsIs()) {
		restoredSerializer = restoredConfig.restoreSerializer();
	}
	else if (strategy.isCompatibleWithReconfiguredSerializer()) {
		restoredSerializer = strategy.getReconfiguredSerializer();
	}
	else {
		throw new AssertionError("Unable to restore serializer with " + strategy);
	}
	assertEquals(serializer.getClass(), restoredSerializer.getClass());

	return restoredSerializer;
}
 
Example 4
Source File: SerializerTestUtil.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Snapshot and restore the given serializer. Returns the restored serializer.
 */
public static <T> TypeSerializer<T> snapshotAndReconfigure(
	TypeSerializer<T> serializer, SerializerGetter<T> serializerGetter) throws IOException {
	TypeSerializerSnapshot<T> configSnapshot = serializer.snapshotConfiguration();

	byte[] serializedConfig;
	try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
		TypeSerializerSnapshotSerializationUtil.writeSerializerSnapshot(
			new DataOutputViewStreamWrapper(out), configSnapshot, serializer);
		serializedConfig = out.toByteArray();
	}

	TypeSerializerSnapshot<T> restoredConfig;
	try (ByteArrayInputStream in = new ByteArrayInputStream(serializedConfig)) {
		restoredConfig = TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(
			new DataInputViewStreamWrapper(in),
			Thread.currentThread().getContextClassLoader(),
			serializerGetter.getSerializer());
	}

	TypeSerializerSchemaCompatibility<T> strategy =
		restoredConfig.resolveSchemaCompatibility(serializerGetter.getSerializer());
	final TypeSerializer<T> restoredSerializer;
	if (strategy.isCompatibleAsIs()) {
		restoredSerializer = restoredConfig.restoreSerializer();
	}
	else if (strategy.isCompatibleWithReconfiguredSerializer()) {
		restoredSerializer = strategy.getReconfiguredSerializer();
	}
	else {
		throw new AssertionError("Unable to restore serializer with " + strategy);
	}
	assertEquals(serializer.getClass(), restoredSerializer.getClass());

	return restoredSerializer;
}