Java Code Examples for org.apache.flink.core.memory.DataOutputSerializer#wrapAsByteBuffer()

The following examples show how to use org.apache.flink.core.memory.DataOutputSerializer#wrapAsByteBuffer() . 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: AvroSerializerSnapshotTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Serialize an (avro)TypeSerializerSnapshot and deserialize it.
 */
private static <T> AvroSerializerSnapshot<T> roundTrip(TypeSerializerSnapshot<T> original) throws IOException {
	// writeSnapshot();
	DataOutputSerializer out = new DataOutputSerializer(1024);
	original.writeSnapshot(out);

	// init
	AvroSerializerSnapshot<T> restored = new AvroSerializerSnapshot<>();

	// readSnapshot();
	DataInputView in = new DataInputDeserializer(out.wrapAsByteBuffer());
	restored.readSnapshot(restored.getCurrentVersion(), in, original.getClass().getClassLoader());

	return restored;
}
 
Example 2
Source File: TypeSerializerSnapshotMigrationTestBase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private TypeSerializerSnapshot<ElementT> writeAndThenReadTheSnapshot(
	TypeSerializer<ElementT> serializer,
	TypeSerializerSnapshot<ElementT> newSnapshot) throws IOException {

	DataOutputSerializer out = new DataOutputSerializer(128);
	TypeSerializerSnapshotSerializationUtil.writeSerializerSnapshot(out, newSnapshot, serializer);

	DataInputView in = new DataInputDeserializer(out.wrapAsByteBuffer());
	return readSnapshot(in);
}
 
Example 3
Source File: EventSerializer.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public static ByteBuffer toSerializedEvent(AbstractEvent event) throws IOException {
	final Class<?> eventClass = event.getClass();
	if (eventClass == EndOfPartitionEvent.class) {
		return ByteBuffer.wrap(new byte[] { 0, 0, 0, END_OF_PARTITION_EVENT });
	}
	else if (eventClass == CheckpointBarrier.class) {
		return serializeCheckpointBarrier((CheckpointBarrier) event);
	}
	else if (eventClass == EndOfSuperstepEvent.class) {
		return ByteBuffer.wrap(new byte[] { 0, 0, 0, END_OF_SUPERSTEP_EVENT });
	}
	else if (eventClass == CancelCheckpointMarker.class) {
		CancelCheckpointMarker marker = (CancelCheckpointMarker) event;

		ByteBuffer buf = ByteBuffer.allocate(12);
		buf.putInt(0, CANCEL_CHECKPOINT_MARKER_EVENT);
		buf.putLong(4, marker.getCheckpointId());
		return buf;
	}
	else {
		try {
			final DataOutputSerializer serializer = new DataOutputSerializer(128);
			serializer.writeInt(OTHER_EVENT);
			serializer.writeUTF(event.getClass().getName());
			event.write(serializer);
			return serializer.wrapAsByteBuffer();
		}
		catch (IOException e) {
			throw new IOException("Error while serializing event.", e);
		}
	}
}
 
Example 4
Source File: SpanningRecordSerializer.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public SpanningRecordSerializer() {
	serializationBuffer = new DataOutputSerializer(128);

	lengthBuffer = ByteBuffer.allocate(4);
	lengthBuffer.order(ByteOrder.BIG_ENDIAN);

	// ensure initial state with hasRemaining false (for correct continueWritingWithNextBufferBuilder logic)
	dataBuffer = serializationBuffer.wrapAsByteBuffer();
	lengthBuffer.position(4);
}
 
Example 5
Source File: AvroSerializerSnapshotTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Serialize an (avro)TypeSerializerSnapshot and deserialize it.
 */
private static <T> AvroSerializerSnapshot<T> roundTrip(TypeSerializerSnapshot<T> original) throws IOException {
	// writeSnapshot();
	DataOutputSerializer out = new DataOutputSerializer(1024);
	original.writeSnapshot(out);

	// init
	AvroSerializerSnapshot<T> restored = new AvroSerializerSnapshot<>();

	// readSnapshot();
	DataInputView in = new DataInputDeserializer(out.wrapAsByteBuffer());
	restored.readSnapshot(restored.getCurrentVersion(), in, original.getClass().getClassLoader());

	return restored;
}
 
Example 6
Source File: TypeSerializerSnapshotMigrationTestBase.java    From flink with Apache License 2.0 5 votes vote down vote up
private TypeSerializerSnapshot<ElementT> writeAndThenReadTheSnapshot(
	TypeSerializer<ElementT> serializer,
	TypeSerializerSnapshot<ElementT> newSnapshot) throws IOException {

	DataOutputSerializer out = new DataOutputSerializer(128);
	TypeSerializerSnapshotSerializationUtil.writeSerializerSnapshot(out, newSnapshot, serializer);

	DataInputView in = new DataInputDeserializer(out.wrapAsByteBuffer());
	return readSnapshot(in);
}
 
Example 7
Source File: EventSerializer.java    From flink with Apache License 2.0 5 votes vote down vote up
public static ByteBuffer toSerializedEvent(AbstractEvent event) throws IOException {
	final Class<?> eventClass = event.getClass();
	if (eventClass == EndOfPartitionEvent.class) {
		return ByteBuffer.wrap(new byte[] { 0, 0, 0, END_OF_PARTITION_EVENT });
	}
	else if (eventClass == CheckpointBarrier.class) {
		return serializeCheckpointBarrier((CheckpointBarrier) event);
	}
	else if (eventClass == EndOfSuperstepEvent.class) {
		return ByteBuffer.wrap(new byte[] { 0, 0, 0, END_OF_SUPERSTEP_EVENT });
	}
	else if (eventClass == CancelCheckpointMarker.class) {
		CancelCheckpointMarker marker = (CancelCheckpointMarker) event;

		ByteBuffer buf = ByteBuffer.allocate(12);
		buf.putInt(0, CANCEL_CHECKPOINT_MARKER_EVENT);
		buf.putLong(4, marker.getCheckpointId());
		return buf;
	}
	else {
		try {
			final DataOutputSerializer serializer = new DataOutputSerializer(128);
			serializer.writeInt(OTHER_EVENT);
			serializer.writeUTF(event.getClass().getName());
			event.write(serializer);
			return serializer.wrapAsByteBuffer();
		}
		catch (IOException e) {
			throw new IOException("Error while serializing event.", e);
		}
	}
}
 
Example 8
Source File: SpanningRecordSerializer.java    From flink with Apache License 2.0 5 votes vote down vote up
public SpanningRecordSerializer() {
	serializationBuffer = new DataOutputSerializer(128);

	lengthBuffer = ByteBuffer.allocate(4);
	lengthBuffer.order(ByteOrder.BIG_ENDIAN);

	// ensure initial state with hasRemaining false (for correct continueWritingWithNextBufferBuilder logic)
	dataBuffer = serializationBuffer.wrapAsByteBuffer();
	lengthBuffer.position(4);
}
 
Example 9
Source File: AvroSerializerSnapshotTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Serialize an (avro)TypeSerializerSnapshot and deserialize it.
 */
private static <T> AvroSerializerSnapshot<T> roundTrip(TypeSerializerSnapshot<T> original) throws IOException {
	// writeSnapshot();
	DataOutputSerializer out = new DataOutputSerializer(1024);
	original.writeSnapshot(out);

	// init
	AvroSerializerSnapshot<T> restored = new AvroSerializerSnapshot<>();

	// readSnapshot();
	DataInputView in = new DataInputDeserializer(out.wrapAsByteBuffer());
	restored.readSnapshot(restored.getCurrentVersion(), in, original.getClass().getClassLoader());

	return restored;
}
 
Example 10
Source File: TypeSerializerUpgradeTestBase.java    From flink with Apache License 2.0 5 votes vote down vote up
private static <T> DataInputView readAndThenWriteData(
		DataInputView originalDataInput,
		TypeSerializer<T> readSerializer,
		TypeSerializer<T> writeSerializer,
		Matcher<T> testDataMatcher) throws IOException {

	T data = readSerializer.deserialize(originalDataInput);
	assertThat(data, testDataMatcher);

	DataOutputSerializer out = new DataOutputSerializer(INITIAL_OUTPUT_BUFFER_SIZE);
	writeSerializer.serialize(data, out);
	return new DataInputDeserializer(out.wrapAsByteBuffer());
}
 
Example 11
Source File: TypeSerializerUpgradeTestBase.java    From flink with Apache License 2.0 5 votes vote down vote up
private static <T> TypeSerializerSnapshot<T> writeAndThenReadSerializerSnapshot(
		TypeSerializer<T> serializer) throws IOException {

	DataOutputSerializer out = new DataOutputSerializer(INITIAL_OUTPUT_BUFFER_SIZE);
	writeSerializerSnapshotCurrentFormat(out, serializer);

	DataInputDeserializer in = new DataInputDeserializer(out.wrapAsByteBuffer());
	return readSerializerSnapshotCurrentFormat(
			in,
			Thread.currentThread().getContextClassLoader());
}
 
Example 12
Source File: EventSerializer.java    From flink with Apache License 2.0 5 votes vote down vote up
public static ByteBuffer toSerializedEvent(AbstractEvent event) throws IOException {
	final Class<?> eventClass = event.getClass();
	if (eventClass == EndOfPartitionEvent.class) {
		return ByteBuffer.wrap(new byte[] { 0, 0, 0, END_OF_PARTITION_EVENT });
	}
	else if (eventClass == CheckpointBarrier.class) {
		return serializeCheckpointBarrier((CheckpointBarrier) event);
	}
	else if (eventClass == EndOfSuperstepEvent.class) {
		return ByteBuffer.wrap(new byte[] { 0, 0, 0, END_OF_SUPERSTEP_EVENT });
	}
	else if (eventClass == EndOfChannelStateEvent.class) {
		return ByteBuffer.wrap(new byte[] { 0, 0, 0, END_OF_CHANNEL_STATE_EVENT });
	}
	else if (eventClass == CancelCheckpointMarker.class) {
		CancelCheckpointMarker marker = (CancelCheckpointMarker) event;

		ByteBuffer buf = ByteBuffer.allocate(12);
		buf.putInt(0, CANCEL_CHECKPOINT_MARKER_EVENT);
		buf.putLong(4, marker.getCheckpointId());
		return buf;
	}
	else {
		try {
			final DataOutputSerializer serializer = new DataOutputSerializer(128);
			serializer.writeInt(OTHER_EVENT);
			serializer.writeUTF(event.getClass().getName());
			event.write(serializer);
			return serializer.wrapAsByteBuffer();
		}
		catch (IOException e) {
			throw new IOException("Error while serializing event.", e);
		}
	}
}
 
Example 13
Source File: AvroSerializerSnapshotTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private static <T> ByteBuffer serialize(TypeSerializer<T> serializer, T record) throws IOException {
	DataOutputSerializer out = new DataOutputSerializer(1024);
	serializer.serialize(record, out);
	return out.wrapAsByteBuffer();
}
 
Example 14
Source File: AvroSerializerSnapshotTest.java    From flink with Apache License 2.0 4 votes vote down vote up
private static <T> ByteBuffer serialize(TypeSerializer<T> serializer, T record) throws IOException {
	DataOutputSerializer out = new DataOutputSerializer(1024);
	serializer.serialize(record, out);
	return out.wrapAsByteBuffer();
}
 
Example 15
Source File: AvroSerializerSnapshotTest.java    From flink with Apache License 2.0 4 votes vote down vote up
private static <T> ByteBuffer serialize(TypeSerializer<T> serializer, T record) throws IOException {
	DataOutputSerializer out = new DataOutputSerializer(1024);
	serializer.serialize(record, out);
	return out.wrapAsByteBuffer();
}
 
Example 16
Source File: SpanningRecordSerializer.java    From flink with Apache License 2.0 4 votes vote down vote up
public SpanningRecordSerializer() {
	serializationBuffer = new DataOutputSerializer(128);

	// ensure initial state with hasRemaining false (for correct continueWritingWithNextBufferBuilder logic)
	dataBuffer = serializationBuffer.wrapAsByteBuffer();
}