Search in sources :

Example 6 with DataOutputSerializer

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();
}
Also used : DataOutputSerializer(org.apache.flink.runtime.util.DataOutputSerializer)

Example 7 with DataOutputSerializer

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
    }
}
Also used : DataOutputSerializer(org.apache.flink.runtime.util.DataOutputSerializer) CheckpointOptions(org.apache.flink.runtime.checkpoint.CheckpointOptions) DataInputDeserializer(org.apache.flink.runtime.util.DataInputDeserializer) Test(org.junit.Test)

Example 8 with DataOutputSerializer

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;
}
Also used : DataOutputSerializer(org.apache.flink.runtime.util.DataOutputSerializer) IOException(java.io.IOException)

Example 9 with DataOutputSerializer

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);
}
Also used : DataOutputSerializer(org.apache.flink.runtime.util.DataOutputSerializer) DataInputDeserializer(org.apache.flink.runtime.util.DataInputDeserializer)

Aggregations

DataOutputSerializer (org.apache.flink.runtime.util.DataOutputSerializer)9 IOException (java.io.IOException)4 CheckpointOptions (org.apache.flink.runtime.checkpoint.CheckpointOptions)2 DataInputDeserializer (org.apache.flink.runtime.util.DataInputDeserializer)2 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CheckpointType (org.apache.flink.runtime.checkpoint.CheckpointOptions.CheckpointType)1 CancelCheckpointMarker (org.apache.flink.runtime.io.network.api.CancelCheckpointMarker)1 CheckpointBarrier (org.apache.flink.runtime.io.network.api.CheckpointBarrier)1 Test (org.junit.Test)1