Search in sources :

Example 16 with DataInputDeserializer

use of org.apache.flink.core.memory.DataInputDeserializer in project flink by apache.

the class TypeSerializerUpgradeTestBase method readAndThenWriteData.

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

Example 17 with DataInputDeserializer

use of org.apache.flink.core.memory.DataInputDeserializer in project flink by apache.

the class TypeSerializerUpgradeTestBase method writeAndThenReadSerializerSnapshot.

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

Example 18 with DataInputDeserializer

use of org.apache.flink.core.memory.DataInputDeserializer in project flink by apache.

the class KvStateSerializer 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 != MAGIC_NUMBER) {
            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.core.memory.DataInputDeserializer)

Example 19 with DataInputDeserializer

use of org.apache.flink.core.memory.DataInputDeserializer in project flink by apache.

the class KvStateSerializer 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.core.memory.DataInputDeserializer)

Example 20 with DataInputDeserializer

use of org.apache.flink.core.memory.DataInputDeserializer in project flink by apache.

the class JobConfWrapper method readObject.

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();
    final byte[] data = new byte[in.readInt()];
    in.readFully(data);
    final DataInputDeserializer deser = new DataInputDeserializer(data);
    this.jobConf = new JobConf();
    try {
        jobConf.readFields(deser);
    } catch (IOException e) {
        throw new IOException("Could not deserialize JobConf, the serialized and de-serialized don't match.", e);
    }
    Credentials currentUserCreds = HadoopInputFormatCommonBase.getCredentialsFromUGI(UserGroupInformation.getCurrentUser());
    if (currentUserCreds != null) {
        jobConf.getCredentials().addAll(currentUserCreds);
    }
}
Also used : IOException(java.io.IOException) JobConf(org.apache.hadoop.mapred.JobConf) Credentials(org.apache.hadoop.security.Credentials) DataInputDeserializer(org.apache.flink.core.memory.DataInputDeserializer)

Aggregations

DataInputDeserializer (org.apache.flink.core.memory.DataInputDeserializer)36 DataOutputSerializer (org.apache.flink.core.memory.DataOutputSerializer)15 IOException (java.io.IOException)9 Test (org.junit.Test)9 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)3 Path (org.apache.flink.core.fs.Path)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Set (java.util.Set)2 MapSerializer (org.apache.flink.api.common.typeutils.base.MapSerializer)2 DataInputView (org.apache.flink.core.memory.DataInputView)2 GenericRowData (org.apache.flink.table.data.GenericRowData)2 RowData (org.apache.flink.table.data.RowData)2 ByteOrder (java.nio.ByteOrder)1 ArrayDeque (java.util.ArrayDeque)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1