Search in sources :

Example 31 with DataOutputSerializer

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

the class KvStateSerializer method serializeValue.

/**
 * Serializes the value with the given serializer.
 *
 * @param value Value of type T to serialize
 * @param serializer Serializer for T
 * @param <T> Type of the value
 * @return Serialized value or <code>null</code> if value <code>null</code>
 * @throws IOException On failure during serialization
 */
public static <T> byte[] serializeValue(T value, TypeSerializer<T> serializer) throws IOException {
    if (value != null) {
        // Serialize
        DataOutputSerializer dos = new DataOutputSerializer(32);
        serializer.serialize(value, dos);
        return dos.getCopyOfBuffer();
    } else {
        return null;
    }
}
Also used : DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer)

Example 32 with DataOutputSerializer

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

the class JobConfWrapper method writeObject.

private void writeObject(ObjectOutputStream out) throws IOException {
    out.defaultWriteObject();
    // we write the jobConf through a separate serializer to avoid cryptic exceptions when it
    // corrupts the serialization stream
    final DataOutputSerializer ser = new DataOutputSerializer(256);
    jobConf.write(ser);
    out.writeInt(ser.length());
    out.write(ser.getSharedBuffer(), 0, ser.length());
}
Also used : DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer)

Example 33 with DataOutputSerializer

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

the class NullableSerializer method checkIfNullSupported.

/**
 * This method checks if {@code serializer} supports {@code null} value.
 *
 * @param serializer serializer to check
 */
public static <T> boolean checkIfNullSupported(@Nonnull TypeSerializer<T> serializer) {
    int length = serializer.getLength() > 0 ? serializer.getLength() : 1;
    DataOutputSerializer dos = new DataOutputSerializer(length);
    try {
        serializer.serialize(null, dos);
    } catch (IOException | RuntimeException e) {
        return false;
    }
    checkArgument(serializer.getLength() < 0 || serializer.getLength() == dos.getCopyOfBuffer().length, "The serialized form of the null value should have the same length " + "as any other if the length is fixed in the serializer");
    DataInputDeserializer dis = new DataInputDeserializer(dos.getSharedBuffer());
    try {
        checkArgument(serializer.deserialize(dis) == null);
    } catch (IOException e) {
        throw new RuntimeException(String.format("Unexpected failure to deserialize just serialized null value with %s", serializer.getClass().getName()), e);
    }
    checkArgument(serializer.copy(null) == null, "Serializer %s has to be able properly copy null value if it can serialize it", serializer.getClass().getName());
    return true;
}
Also used : DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer) IOException(java.io.IOException) DataInputDeserializer(org.apache.flink.core.memory.DataInputDeserializer)

Example 34 with DataOutputSerializer

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

the class PassThroughPythonStreamGroupWindowAggregateOperator method emitTimerData.

private void emitTimerData(RowData key, TimeWindow window, byte timerOperand) throws IOException {
    reusePythonTimerData.setByte(0, timerOperand);
    reusePythonTimerData.setField(1, key);
    reusePythonTimerData.setLong(2, window.maxTimestamp());
    windowSerializer.serialize(window, windowBaosWrapper);
    reusePythonTimerData.setField(3, windowBaos.toByteArray());
    windowBaos.reset();
    DataOutputSerializer output = new DataOutputSerializer(1);
    udfOutputTypeSerializer.serialize(reusePythonTimerRowData, output);
    resultBuffer.add(output.getCopyOfBuffer());
}
Also used : DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer)

Example 35 with DataOutputSerializer

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

the class TypeInformationKeyValueSerializationSchema method serializeValue.

@Override
public byte[] serializeValue(Tuple2<K, V> element) {
    // if the value is null, its serialized value is null as well.
    if (element.f1 == null) {
        return null;
    }
    if (valueOutputSerializer == null) {
        valueOutputSerializer = new DataOutputSerializer(16);
    }
    try {
        valueSerializer.serialize(element.f1, valueOutputSerializer);
    } catch (IOException e) {
        throw new RuntimeException("Unable to serialize record", e);
    }
    byte[] res = valueOutputSerializer.getByteArray();
    if (res.length != valueOutputSerializer.length()) {
        byte[] n = new byte[valueOutputSerializer.length()];
        System.arraycopy(res, 0, n, 0, valueOutputSerializer.length());
        res = n;
    }
    valueOutputSerializer.clear();
    return res;
}
Also used : DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer) IOException(java.io.IOException)

Aggregations

DataOutputSerializer (org.apache.flink.core.memory.DataOutputSerializer)63 DataInputDeserializer (org.apache.flink.core.memory.DataInputDeserializer)15 Test (org.junit.Test)15 IOException (java.io.IOException)10 ByteBuffer (java.nio.ByteBuffer)6 List (java.util.List)4 IntSerializer (org.apache.flink.api.common.typeutils.base.IntSerializer)4 StringSerializer (org.apache.flink.api.common.typeutils.base.StringSerializer)4 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)4 ArrayList (java.util.ArrayList)3 IntStream (java.util.stream.IntStream)3 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)3 TypeSerializer (org.apache.flink.api.common.typeutils.TypeSerializer)3 Comparator (java.util.Comparator)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Function (java.util.function.Function)2 Stream (java.util.stream.Stream)2 ValueState (org.apache.flink.api.common.state.ValueState)2 ValueStateDescriptor (org.apache.flink.api.common.state.ValueStateDescriptor)2