use of org.apache.flink.runtime.util.DataOutputSerializer in project flink by apache.
the class KvStateRequestSerializer method serializeKeyAndNamespace.
// ------------------------------------------------------------------------
// Generic serialization utils
// ------------------------------------------------------------------------
/**
* Serializes the key and namespace into a {@link ByteBuffer}.
*
* <p>The serialized format matches the RocksDB state backend key format, i.e.
* the key and namespace don't have to be deserialized for RocksDB lookups.
*
* @param key Key to serialize
* @param keySerializer Serializer for the key
* @param namespace Namespace to serialize
* @param namespaceSerializer Serializer for the namespace
* @param <K> Key type
* @param <N> Namespace type
* @return Buffer holding the serialized key and namespace
* @throws IOException Serialization errors are forwarded
*/
public static <K, N> byte[] serializeKeyAndNamespace(K key, TypeSerializer<K> keySerializer, N namespace, TypeSerializer<N> namespaceSerializer) throws IOException {
DataOutputSerializer dos = new DataOutputSerializer(32);
keySerializer.serialize(key, dos);
dos.writeByte(42);
namespaceSerializer.serialize(namespace, dos);
return dos.getCopyOfBuffer();
}
use of org.apache.flink.runtime.util.DataOutputSerializer in project flink by apache.
the class CheckpointBarrierTest method testSerialization.
/**
* Test serialization of the checkpoint barrier.
* The checkpoint barrier does not support its own serialization, in order to be immutable.
*/
@Test
public void testSerialization() throws Exception {
long id = Integer.MAX_VALUE + 123123L;
long timestamp = Integer.MAX_VALUE + 1228L;
CheckpointOptions options = CheckpointOptions.forFullCheckpoint();
CheckpointBarrier barrier = new CheckpointBarrier(id, timestamp, options);
try {
barrier.write(new DataOutputSerializer(1024));
fail("should throw an exception");
} catch (UnsupportedOperationException e) {
// expected
}
try {
barrier.read(new DataInputDeserializer(new byte[32]));
fail("should throw an exception");
} catch (UnsupportedOperationException e) {
// expected
}
}
use of org.apache.flink.runtime.util.DataOutputSerializer in project flink by apache.
the class TypeInformationSerializationSchema method serialize.
@Override
public byte[] serialize(T element) {
if (dos == null) {
dos = new DataOutputSerializer(16);
}
try {
serializer.serialize(element, dos);
} catch (IOException e) {
throw new RuntimeException("Unable to serialize record", e);
}
byte[] ret = dos.getByteArray();
if (ret.length != dos.length()) {
byte[] n = new byte[dos.length()];
System.arraycopy(ret, 0, n, 0, dos.length());
ret = n;
}
dos.clear();
return ret;
}
use of org.apache.flink.runtime.util.DataOutputSerializer in project flink by apache.
the class StreamElementSerializerTest method serializeAndDeserialize.
@SuppressWarnings("unchecked")
private static <T, X extends StreamElement> X serializeAndDeserialize(X record, StreamElementSerializer<T> serializer) throws IOException {
DataOutputSerializer output = new DataOutputSerializer(32);
serializer.serialize(record, output);
// additional binary copy step
DataInputDeserializer copyInput = new DataInputDeserializer(output.getByteArray(), 0, output.length());
DataOutputSerializer copyOutput = new DataOutputSerializer(32);
serializer.copy(copyInput, copyOutput);
DataInputDeserializer input = new DataInputDeserializer(copyOutput.getByteArray(), 0, copyOutput.length());
return (X) serializer.deserialize(input);
}
Aggregations