Search in sources :

Example 6 with DataInputDeserializer

use of org.apache.flink.runtime.util.DataInputDeserializer in project flink by apache.

the class KvStateRequestSerializer method deserializeKeyAndNamespace.

/**
	 * Deserializes the key and namespace into a {@link Tuple2}.
	 *
	 * @param serializedKeyAndNamespace Serialized key and namespace
	 * @param keySerializer             Serializer for the key
	 * @param namespaceSerializer       Serializer for the namespace
	 * @param <K>                       Key type
	 * @param <N>                       Namespace
	 * @return Tuple2 holding deserialized key and namespace
	 * @throws IOException              if the deserialization fails for any reason
	 */
public static <K, N> Tuple2<K, N> deserializeKeyAndNamespace(byte[] serializedKeyAndNamespace, TypeSerializer<K> keySerializer, TypeSerializer<N> namespaceSerializer) throws IOException {
    DataInputDeserializer dis = new DataInputDeserializer(serializedKeyAndNamespace, 0, serializedKeyAndNamespace.length);
    try {
        K key = keySerializer.deserialize(dis);
        byte magicNumber = dis.readByte();
        if (magicNumber != 42) {
            throw new IOException("Unexpected magic number " + magicNumber + ".");
        }
        N namespace = namespaceSerializer.deserialize(dis);
        if (dis.available() > 0) {
            throw new IOException("Unconsumed bytes in the serialized key and namespace.");
        }
        return new Tuple2<>(key, namespace);
    } catch (IOException e) {
        throw new IOException("Unable to deserialize key " + "and namespace. This indicates a mismatch in the key/namespace " + "serializers used by the KvState instance and this access.", e);
    }
}
Also used : Tuple2(org.apache.flink.api.java.tuple.Tuple2) IOException(java.io.IOException) DataInputDeserializer(org.apache.flink.runtime.util.DataInputDeserializer)

Example 7 with DataInputDeserializer

use of org.apache.flink.runtime.util.DataInputDeserializer in project flink by apache.

the class KvStateRequestSerializer method deserializeValue.

/**
	 * Deserializes the value with the given serializer.
	 *
	 * @param serializedValue Serialized value of type T
	 * @param serializer      Serializer for T
	 * @param <T>             Type of the value
	 * @return Deserialized value or <code>null</code> if the serialized value
	 * is <code>null</code>
	 * @throws IOException On failure during deserialization
	 */
public static <T> T deserializeValue(byte[] serializedValue, TypeSerializer<T> serializer) throws IOException {
    if (serializedValue == null) {
        return null;
    } else {
        final DataInputDeserializer deser = new DataInputDeserializer(serializedValue, 0, serializedValue.length);
        final T value = serializer.deserialize(deser);
        if (deser.available() > 0) {
            throw new IOException("Unconsumed bytes in the deserialized value. " + "This indicates a mismatch in the value serializers " + "used by the KvState instance and this access.");
        }
        return value;
    }
}
Also used : IOException(java.io.IOException) DataInputDeserializer(org.apache.flink.runtime.util.DataInputDeserializer)

Example 8 with DataInputDeserializer

use of org.apache.flink.runtime.util.DataInputDeserializer 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 9 with DataInputDeserializer

use of org.apache.flink.runtime.util.DataInputDeserializer 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

DataInputDeserializer (org.apache.flink.runtime.util.DataInputDeserializer)9 IOException (java.io.IOException)4 ByteOrder (java.nio.ByteOrder)2 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)2 CheckpointOptions (org.apache.flink.runtime.checkpoint.CheckpointOptions)2 AbstractEvent (org.apache.flink.runtime.event.AbstractEvent)2 DataOutputSerializer (org.apache.flink.runtime.util.DataOutputSerializer)2 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)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