Java Code Examples for org.apache.flink.util.InstantiationUtil#serializeToByteArray()

The following examples show how to use org.apache.flink.util.InstantiationUtil#serializeToByteArray() . 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: KryoUtils.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Tries to copy the given record from using the provided Kryo instance. If this fails, then
 * the record from is copied by serializing it into a byte buffer and deserializing it from
 * there.
 *
 * @param from Element to copy
 * @param kryo Kryo instance to use
 * @param serializer TypeSerializer which is used in case of a Kryo failure
 * @param <T> Type of the element to be copied
 * @return Copied element
 */
public static <T> T copy(T from, Kryo kryo, TypeSerializer<T> serializer) {
	try {
		return kryo.copy(from);
	} catch (KryoException ke) {
		// Kryo could not copy the object --> try to serialize/deserialize the object
		try {
			byte[] byteArray = InstantiationUtil.serializeToByteArray(serializer, from);

			return InstantiationUtil.deserializeFromByteArray(serializer, byteArray);
		} catch (IOException ioe) {
			throw new RuntimeException("Could not copy object by serializing/deserializing" +
				" it.", ioe);
		}
	}
}
 
Example 2
Source File: KryoUtils.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Tries to copy the given record from using the provided Kryo instance. If this fails, then
 * the record from is copied by serializing it into a byte buffer and deserializing it from
 * there.
 *
 * @param from Element to copy
 * @param reuse Reuse element for the deserialization
 * @param kryo Kryo instance to use
 * @param serializer TypeSerializer which is used in case of a Kryo failure
 * @param <T> Type of the element to be copied
 * @return Copied element
 */
public static <T> T copy(T from, T reuse, Kryo kryo, TypeSerializer<T> serializer) {
	try {
		return kryo.copy(from);
	} catch (KryoException ke) {
		// Kryo could not copy the object --> try to serialize/deserialize the object
		try {
			byte[] byteArray = InstantiationUtil.serializeToByteArray(serializer, from);

			return InstantiationUtil.deserializeFromByteArray(serializer, reuse, byteArray);
		} catch (IOException ioe) {
			throw new RuntimeException("Could not copy object by serializing/deserializing" +
				" it.", ioe);
		}
	}
}
 
Example 3
Source File: KryoUtils.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tries to copy the given record from using the provided Kryo instance. If this fails, then
 * the record from is copied by serializing it into a byte buffer and deserializing it from
 * there.
 *
 * @param from Element to copy
 * @param kryo Kryo instance to use
 * @param serializer TypeSerializer which is used in case of a Kryo failure
 * @param <T> Type of the element to be copied
 * @return Copied element
 */
public static <T> T copy(T from, Kryo kryo, TypeSerializer<T> serializer) {
	try {
		return kryo.copy(from);
	} catch (KryoException ke) {
		// Kryo could not copy the object --> try to serialize/deserialize the object
		try {
			byte[] byteArray = InstantiationUtil.serializeToByteArray(serializer, from);

			return InstantiationUtil.deserializeFromByteArray(serializer, byteArray);
		} catch (IOException ioe) {
			throw new RuntimeException("Could not copy object by serializing/deserializing" +
				" it.", ioe);
		}
	}
}
 
Example 4
Source File: KryoUtils.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tries to copy the given record from using the provided Kryo instance. If this fails, then
 * the record from is copied by serializing it into a byte buffer and deserializing it from
 * there.
 *
 * @param from Element to copy
 * @param reuse Reuse element for the deserialization
 * @param kryo Kryo instance to use
 * @param serializer TypeSerializer which is used in case of a Kryo failure
 * @param <T> Type of the element to be copied
 * @return Copied element
 */
public static <T> T copy(T from, T reuse, Kryo kryo, TypeSerializer<T> serializer) {
	try {
		return kryo.copy(from);
	} catch (KryoException ke) {
		// Kryo could not copy the object --> try to serialize/deserialize the object
		try {
			byte[] byteArray = InstantiationUtil.serializeToByteArray(serializer, from);

			return InstantiationUtil.deserializeFromByteArray(serializer, reuse, byteArray);
		} catch (IOException ioe) {
			throw new RuntimeException("Could not copy object by serializing/deserializing" +
				" it.", ioe);
		}
	}
}
 
Example 5
Source File: KryoUtils.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tries to copy the given record from using the provided Kryo instance. If this fails, then
 * the record from is copied by serializing it into a byte buffer and deserializing it from
 * there.
 *
 * @param from Element to copy
 * @param kryo Kryo instance to use
 * @param serializer TypeSerializer which is used in case of a Kryo failure
 * @param <T> Type of the element to be copied
 * @return Copied element
 */
public static <T> T copy(T from, Kryo kryo, TypeSerializer<T> serializer) {
	try {
		return kryo.copy(from);
	} catch (KryoException ke) {
		// Kryo could not copy the object --> try to serialize/deserialize the object
		try {
			byte[] byteArray = InstantiationUtil.serializeToByteArray(serializer, from);

			return InstantiationUtil.deserializeFromByteArray(serializer, byteArray);
		} catch (IOException ioe) {
			throw new RuntimeException("Could not copy object by serializing/deserializing" +
				" it.", ioe);
		}
	}
}
 
Example 6
Source File: KryoUtils.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tries to copy the given record from using the provided Kryo instance. If this fails, then
 * the record from is copied by serializing it into a byte buffer and deserializing it from
 * there.
 *
 * @param from Element to copy
 * @param reuse Reuse element for the deserialization
 * @param kryo Kryo instance to use
 * @param serializer TypeSerializer which is used in case of a Kryo failure
 * @param <T> Type of the element to be copied
 * @return Copied element
 */
public static <T> T copy(T from, T reuse, Kryo kryo, TypeSerializer<T> serializer) {
	try {
		return kryo.copy(from);
	} catch (KryoException ke) {
		// Kryo could not copy the object --> try to serialize/deserialize the object
		try {
			byte[] byteArray = InstantiationUtil.serializeToByteArray(serializer, from);

			return InstantiationUtil.deserializeFromByteArray(serializer, reuse, byteArray);
		} catch (IOException ioe) {
			throw new RuntimeException("Could not copy object by serializing/deserializing" +
				" it.", ioe);
		}
	}
}
 
Example 7
Source File: BinaryGeneric.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void materialize() {
	try {
		byte[] bytes = InstantiationUtil.serializeToByteArray(javaObjectSer, javaObject);
		pointTo(new MemorySegment[] {MemorySegmentFactory.wrap(bytes)}, 0, bytes.length);
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
 
Example 8
Source File: ValueStateToKeyedStateRow.java    From bravo with Apache License 2.0 5 votes vote down vote up
@Override
public KeyedStateRow map(Tuple2<K, V> t) throws Exception {
	int keyGroup = KeyGroupRangeAssignment.assignToKeyGroup(t.f0, maxParallelism);
	ByteArrayOutputStreamWithPos os = new ByteArrayOutputStreamWithPos();
	DataOutputViewStreamWrapper ov = new DataOutputViewStreamWrapper(os);

	RocksDBUtils.writeKeyGroup(keyGroup, keygroupPrefixBytes, ov);
	RocksDBUtils.writeKey(t.f0, keySerializer, os, ov, false);
	RocksDBUtils.writeNameSpace(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, os,
			ov, false);

	os.close();
	return new KeyedStateRow(stateName, os.toByteArray(),
			InstantiationUtil.serializeToByteArray(valueSerializer, t.f1));
}
 
Example 9
Source File: FlinkKafkaConsumerBaseTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Before using an explicit TypeSerializer for the partition state the {@link
 * FlinkKafkaConsumerBase} was creating a serializer using a {@link TypeHint}. Here, we verify
 * that the two methods create compatible serializers.
 */
@Test
public void testExplicitStateSerializerCompatibility() throws Exception {
	ExecutionConfig executionConfig = new ExecutionConfig();

	Tuple2<KafkaTopicPartition, Long> tuple =
			new Tuple2<>(new KafkaTopicPartition("dummy", 0), 42L);

	// This is how the KafkaConsumerBase used to create the TypeSerializer
	TypeInformation<Tuple2<KafkaTopicPartition, Long>> originalTypeHintTypeInfo =
			new TypeHint<Tuple2<KafkaTopicPartition, Long>>() {}.getTypeInfo();
	TypeSerializer<Tuple2<KafkaTopicPartition, Long>> serializerFromTypeHint =
			originalTypeHintTypeInfo.createSerializer(executionConfig);
	byte[] bytes = InstantiationUtil.serializeToByteArray(serializerFromTypeHint, tuple);

	// Directly use the Consumer to create the TypeSerializer (using the new method)
	TupleSerializer<Tuple2<KafkaTopicPartition, Long>> kafkaConsumerSerializer =
			FlinkKafkaConsumerBase.createStateSerializer(executionConfig);
	Tuple2<KafkaTopicPartition, Long> actualTuple =
			InstantiationUtil.deserializeFromByteArray(kafkaConsumerSerializer, bytes);

	Assert.assertEquals(
			"Explicit Serializer is not compatible with previous method of creating Serializer using TypeHint.",
			tuple,
			actualTuple
	);
}
 
Example 10
Source File: BinaryRawValueData.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected BinarySection materialize(TypeSerializer<T> serializer) {
	try {
		byte[] bytes = InstantiationUtil.serializeToByteArray(serializer, javaObject);
		return new BinarySection(new MemorySegment[] {MemorySegmentFactory.wrap(bytes)}, 0, bytes.length);
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
 
Example 11
Source File: ResettableExternalBuffer.java    From flink with Apache License 2.0 4 votes vote down vote up
private void throwTooBigException(BaseRow row) throws IOException {
	int rowSize = InstantiationUtil.serializeToByteArray(inMemoryBuffer.serializer, row).length;
	throw new IOException("Record is too big, it can't be added to a empty InMemoryBuffer! " +
			"Record size: " + rowSize + ", Buffer: " + memorySize());
}
 
Example 12
Source File: ResettableExternalBuffer.java    From flink with Apache License 2.0 4 votes vote down vote up
private void throwTooBigException(RowData row) throws IOException {
	int rowSize = InstantiationUtil.serializeToByteArray(inMemoryBuffer.serializer, row).length;
	throw new IOException("Record is too big, it can't be added to a empty InMemoryBuffer! " +
			"Record size: " + rowSize + ", Buffer: " + memorySize());
}