Java Code Examples for org.apache.flink.core.memory.DataInputView#readUTF()

The following examples show how to use org.apache.flink.core.memory.DataInputView#readUTF() . 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: KryoRegistrationSerializerConfigSnapshot.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void read(DataInputView in) throws IOException {
	super.read(in);

	int numKryoRegistrations = in.readInt();
	kryoRegistrations = new LinkedHashMap<>(numKryoRegistrations);

	KryoRegistrationSerializationProxy proxy;
	for (int i = 0; i < numKryoRegistrations; i++) {
		String classTag = in.readUTF();

		proxy = new KryoRegistrationSerializationProxy(getUserCodeClassLoader());
		proxy.read(in);

		kryoRegistrations.put(classTag, proxy.kryoRegistration);
	}
}
 
Example 2
Source File: KryoRegistrationSerializerConfigSnapshot.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public void read(DataInputView in) throws IOException {
	super.read(in);

	int numKryoRegistrations = in.readInt();
	kryoRegistrations = new LinkedHashMap<>(numKryoRegistrations);

	KryoRegistrationSerializationProxy proxy;
	for (int i = 0; i < numKryoRegistrations; i++) {
		String classTag = in.readUTF();

		proxy = new KryoRegistrationSerializationProxy(getUserCodeClassLoader());
		proxy.read(in);

		kryoRegistrations.put(classTag, proxy.kryoRegistration);
	}
}
 
Example 3
Source File: InstantiationUtil.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Loads a class by name from the given input stream and reflectively instantiates it.
 *
 * <p>This method will use {@link DataInputView#readUTF()} to read the class name, and
 * then attempt to load the class from the given ClassLoader.
 *
 * <p>The resolved class is checked to be equal to or a subtype of the given supertype
 * class.
 *
 * @param in The stream to read the class name from.
 * @param cl The class loader to resolve the class.
 * @param supertype A class that the resolved class must extend.
 *
 * @throws IOException Thrown, if the class name could not be read, the class could not be found,
 *                     or the class is not a subtype of the given supertype class.
 */
public static <T> Class<T> resolveClassByName(
		DataInputView in,
		ClassLoader cl,
		Class<? super T> supertype) throws IOException {

	final String className = in.readUTF();
	final Class<?> rawClazz;
	try {
		rawClazz = Class.forName(className, false, cl);
	}
	catch (ClassNotFoundException e) {
		throw new IOException(
				"Could not find class '" + className +  "' in classpath.", e);
	}

	if (!supertype.isAssignableFrom(rawClazz)) {
		throw new IOException("The class " + className + " is not a subclass of " + supertype.getName());
	}

	@SuppressWarnings("unchecked")
	Class<T> clazz = (Class<T>) rawClazz;
	return clazz;
}
 
Example 4
Source File: SimpleTypeSerializerSnapshot.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void readSnapshot(int readVersion, DataInputView in, ClassLoader classLoader) throws IOException {
	switch (readVersion) {
		case 3: {
			break;
		}
		case 2: {
			// we don't need the classname any more; read and drop to maintain compatibility
			in.readUTF();
			break;
		}
		default: {
			throw new IOException("Unrecognized version: " + readVersion);
		}
	}
}
 
Example 5
Source File: FlinkKafkaProducer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public FlinkKafkaProducer.KafkaTransactionState deserialize(DataInputView source) throws IOException {
	String transactionalId = null;
	if (source.readBoolean()) {
		transactionalId = source.readUTF();
	}
	long producerId = source.readLong();
	short epoch = source.readShort();
	return new FlinkKafkaProducer.KafkaTransactionState(transactionalId, producerId, epoch, null);
}
 
Example 6
Source File: TypeSerializerSnapshotMigrationITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void readSnapshot(int readVersion, DataInputView in, ClassLoader userCodeClassLoader) throws IOException {
	if (readVersion != 1) {
		throw new IllegalStateException("Can not recognize read version: " + readVersion);
	}

	this.configPayload = in.readUTF();
}
 
Example 7
Source File: KryoSerializerSnapshotData.java    From flink with Apache License 2.0 5 votes vote down vote up
static KryoRegistration tryReadKryoRegistration(
	DataInputView in,
	ClassLoader userCodeClassLoader) throws IOException {

	String registeredClassname = in.readUTF();
	Class<?> registeredClass;
	try {
		registeredClass = Class.forName(registeredClassname, true, userCodeClassLoader);
	}
	catch (ClassNotFoundException e) {
		LOG.warn("Cannot find registered class " + registeredClassname + " for Kryo serialization in classpath;" +
			" using a dummy class as a placeholder.", e);
		return null;
	}

	final KryoRegistration.SerializerDefinitionType serializerDefinitionType =
		KryoRegistration.SerializerDefinitionType.values()[in.readInt()];

	switch (serializerDefinitionType) {
		case UNSPECIFIED: {
			return new KryoRegistration(registeredClass);
		}
		case CLASS: {
			return tryReadWithSerializerClass(in, userCodeClassLoader, registeredClassname, registeredClass);
		}
		case INSTANCE: {
			return tryReadWithSerializerInstance(in, userCodeClassLoader, registeredClassname, registeredClass);
		}
		default: {
			throw new IllegalStateException(
				"Unrecognized Kryo registration serializer definition type: " + serializerDefinitionType);
		}
	}
}
 
Example 8
Source File: StateMetaInfoSnapshotReadersWriters.java    From flink with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public StateMetaInfoSnapshot readStateMetaInfoSnapshot(
	@Nonnull DataInputView inputView,
	@Nonnull ClassLoader userCodeClassLoader) throws IOException {

	final String stateName = inputView.readUTF();
	final StateMetaInfoSnapshot.BackendStateType stateType =
		StateMetaInfoSnapshot.BackendStateType.values()[inputView.readInt()];
	final int numOptions = inputView.readInt();
	HashMap<String, String> optionsMap = new HashMap<>(numOptions);
	for (int i = 0; i < numOptions; ++i) {
		String key = inputView.readUTF();
		String value = inputView.readUTF();
		optionsMap.put(key, value);
	}

	final int numSerializerConfigSnapshots = inputView.readInt();
	final HashMap<String, TypeSerializerSnapshot<?>> serializerConfigsMap = new HashMap<>(numSerializerConfigSnapshots);

	for (int i = 0; i < numSerializerConfigSnapshots; ++i) {
		serializerConfigsMap.put(
			inputView.readUTF(),
			TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot(
				inputView, userCodeClassLoader, null));
	}

	return new StateMetaInfoSnapshot(stateName, stateType, optionsMap, serializerConfigsMap);
}
 
Example 9
Source File: KryoSerializerSnapshotData.java    From flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Class<T> apply(DataInputView stream, String unused) throws IOException {
	String className = stream.readUTF();
	try {
		return (Class<T>) Class.forName(className, false, classLoader);
	}
	catch (ClassNotFoundException e) {
		LOG.warn("Cannot find registered class " + className + " for Kryo serialization in classpath.", e);
		return null;
	}
}
 
Example 10
Source File: FlinkKafkaProducer011.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public KafkaTransactionState deserialize(DataInputView source) throws IOException {
	String transactionalId = null;
	if (source.readBoolean()) {
		transactionalId = source.readUTF();
	}
	long producerId = source.readLong();
	short epoch = source.readShort();
	return new KafkaTransactionState(transactionalId, producerId, epoch, null);
}
 
Example 11
Source File: ValueSerializerUpgradeTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void read(DataInputView in) throws IOException {
	name = in.readUTF();
}
 
Example 12
Source File: TypeSerializerSerializationUtilTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void read(DataInputView in) throws IOException {
	super.read(in);
	val = in.readInt();
	msg = in.readUTF();
}
 
Example 13
Source File: PojoSerializer.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void copy(DataInputView source, DataOutputView target) throws IOException {
	// copy the flags
	int flags = source.readByte();
	target.writeByte(flags);

	if ((flags & IS_NULL) != 0) {
		// is a null value, nothing further to copy
		return;
	}

	TypeSerializer<?> subclassSerializer = null;
	if ((flags & IS_SUBCLASS) != 0) {
		String className = source.readUTF();
		target.writeUTF(className);
		try {
			Class<?> subclass = Class.forName(className, true, Thread.currentThread()
					.getContextClassLoader());
			subclassSerializer = getSubclassSerializer(subclass);
		} catch (ClassNotFoundException e) {
			throw new RuntimeException("Cannot instantiate class.", e);
		}
	} else if ((flags & IS_TAGGED_SUBCLASS) != 0) {
		int subclassTag = source.readByte();
		target.writeByte(subclassTag);
		subclassSerializer = registeredSerializers[subclassTag];
	}

	if ((flags & NO_SUBCLASS) != 0) {
		for (int i = 0; i < numFields; i++) {
			boolean isNull = source.readBoolean();
			target.writeBoolean(isNull);
			if (!isNull) {
				fieldSerializers[i].copy(source, target);
			}
		}
	} else {
		if (subclassSerializer != null) {
			subclassSerializer.copy(source, target);
		}
	}
}
 
Example 14
Source File: VersionedIOWriteableTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void read(DataInputView in) throws IOException {
	super.read(in);
	this.data = in.readUTF();
}
 
Example 15
Source File: AsciiStringType.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void read(DataInputView in) throws IOException {
	this.value = in.readUTF();
}
 
Example 16
Source File: LegacyStateMetaInfoReaders.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public StateMetaInfoSnapshot readStateMetaInfoSnapshot(
	@Nonnull DataInputView in,
	@Nonnull ClassLoader userCodeClassLoader) throws IOException {

	final String name = in.readUTF();
	final OperatorStateHandle.Mode mode = OperatorStateHandle.Mode.values()[in.readByte()];

	Map<String, String> optionsMap = Collections.singletonMap(
		StateMetaInfoSnapshot.CommonOptionsKeys.OPERATOR_STATE_DISTRIBUTION_MODE.toString(),
		mode.toString());

	List<Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>> stateSerializerAndConfigList =
		TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(in, userCodeClassLoader);

	final int listSize = stateSerializerAndConfigList.size();
	StateMetaInfoSnapshot.BackendStateType stateType = listSize == 1 ?
		StateMetaInfoSnapshot.BackendStateType.OPERATOR : StateMetaInfoSnapshot.BackendStateType.BROADCAST;

	Map<String, TypeSerializerSnapshot<?>> serializerConfigsMap = new HashMap<>(listSize);
	switch (stateType) {
		case OPERATOR:
			serializerConfigsMap.put(
				StateMetaInfoSnapshot.CommonSerializerKeys.VALUE_SERIALIZER.toString(),
				stateSerializerAndConfigList.get(0).f1);
			break;
		case BROADCAST:
			serializerConfigsMap.put(
				StateMetaInfoSnapshot.CommonSerializerKeys.KEY_SERIALIZER.toString(),
				stateSerializerAndConfigList.get(0).f1);

			serializerConfigsMap.put(
				StateMetaInfoSnapshot.CommonSerializerKeys.VALUE_SERIALIZER.toString(),
				stateSerializerAndConfigList.get(1).f1);
			break;
		default:
			throw new IllegalStateException("Unknown operator state type " + stateType);
	}

	return new StateMetaInfoSnapshot(
		name,
		stateType,
		optionsMap,
		serializerConfigsMap);
}
 
Example 17
Source File: MetricDumpSerialization.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private static MetricDump.GaugeDump deserializeGauge(DataInputView dis) throws IOException {
	QueryScopeInfo scope = deserializeMetricInfo(dis);
	String name = dis.readUTF();
	String value = dis.readUTF();
	return new MetricDump.GaugeDump(scope, name, value);
}
 
Example 18
Source File: CompositeTypeSerializerSnapshotTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public void readOuterSnapshot(int readOuterSnapshotVersion, DataInputView in, ClassLoader userCodeClassLoader) throws IOException {
	Assert.assertEquals(getCurrentOuterSnapshotVersion(), readOuterSnapshotVersion);
	this.outerConfiguration = in.readUTF();
}
 
Example 19
Source File: AsciiStringType.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void read(DataInputView in) throws IOException {
	this.value = in.readUTF();
}
 
Example 20
Source File: LegacyStateMetaInfoReaders.java    From flink with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public StateMetaInfoSnapshot readStateMetaInfoSnapshot(
	@Nonnull DataInputView in,
	@Nonnull ClassLoader userCodeClassLoader) throws IOException {

	final String name = in.readUTF();
	final OperatorStateHandle.Mode mode = OperatorStateHandle.Mode.values()[in.readByte()];

	Map<String, String> optionsMap = Collections.singletonMap(
		StateMetaInfoSnapshot.CommonOptionsKeys.OPERATOR_STATE_DISTRIBUTION_MODE.toString(),
		mode.toString());

	List<Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>> stateSerializerAndConfigList =
		TypeSerializerSerializationUtil.readSerializersAndConfigsWithResilience(in, userCodeClassLoader);

	final int listSize = stateSerializerAndConfigList.size();
	StateMetaInfoSnapshot.BackendStateType stateType = listSize == 1 ?
		StateMetaInfoSnapshot.BackendStateType.OPERATOR : StateMetaInfoSnapshot.BackendStateType.BROADCAST;

	Map<String, TypeSerializerSnapshot<?>> serializerConfigsMap = new HashMap<>(listSize);
	switch (stateType) {
		case OPERATOR:
			serializerConfigsMap.put(
				StateMetaInfoSnapshot.CommonSerializerKeys.VALUE_SERIALIZER.toString(),
				stateSerializerAndConfigList.get(0).f1);
			break;
		case BROADCAST:
			serializerConfigsMap.put(
				StateMetaInfoSnapshot.CommonSerializerKeys.KEY_SERIALIZER.toString(),
				stateSerializerAndConfigList.get(0).f1);

			serializerConfigsMap.put(
				StateMetaInfoSnapshot.CommonSerializerKeys.VALUE_SERIALIZER.toString(),
				stateSerializerAndConfigList.get(1).f1);
			break;
		default:
			throw new IllegalStateException("Unknown operator state type " + stateType);
	}

	return new StateMetaInfoSnapshot(
		name,
		stateType,
		optionsMap,
		serializerConfigsMap);
}