use of org.apache.flink.runtime.util.DataOutputSerializer in project flink by apache.
the class KvStateRequestSerializer 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;
}
}
use of org.apache.flink.runtime.util.DataOutputSerializer in project flink by apache.
the class TypeInformationKeyValueSerializationSchema method serializeKey.
@Override
public byte[] serializeKey(Tuple2<K, V> element) {
if (element.f0 == null) {
return null;
} else {
// key is not null. serialize it:
if (keyOutputSerializer == null) {
keyOutputSerializer = new DataOutputSerializer(16);
}
try {
keySerializer.serialize(element.f0, keyOutputSerializer);
} catch (IOException e) {
throw new RuntimeException("Unable to serialize record", e);
}
// check if key byte array size changed
byte[] res = keyOutputSerializer.getByteArray();
if (res.length != keyOutputSerializer.length()) {
byte[] n = new byte[keyOutputSerializer.length()];
System.arraycopy(res, 0, n, 0, keyOutputSerializer.length());
res = n;
}
keyOutputSerializer.clear();
return res;
}
}
use of org.apache.flink.runtime.util.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;
}
use of org.apache.flink.runtime.util.DataOutputSerializer in project flink by apache.
the class EventSerializer method toSerializedEvent.
// ------------------------------------------------------------------------
public static ByteBuffer toSerializedEvent(AbstractEvent event) throws IOException {
final Class<?> eventClass = event.getClass();
if (eventClass == EndOfPartitionEvent.class) {
return ByteBuffer.wrap(new byte[] { 0, 0, 0, END_OF_PARTITION_EVENT });
} else if (eventClass == CheckpointBarrier.class) {
CheckpointBarrier barrier = (CheckpointBarrier) event;
CheckpointOptions checkpointOptions = barrier.getCheckpointOptions();
CheckpointType checkpointType = checkpointOptions.getCheckpointType();
ByteBuffer buf;
if (checkpointType == CheckpointType.FULL_CHECKPOINT) {
buf = ByteBuffer.allocate(24);
buf.putInt(0, CHECKPOINT_BARRIER_EVENT);
buf.putLong(4, barrier.getId());
buf.putLong(12, barrier.getTimestamp());
buf.putInt(20, checkpointType.ordinal());
} else if (checkpointType == CheckpointType.SAVEPOINT) {
String targetLocation = checkpointOptions.getTargetLocation();
assert (targetLocation != null);
byte[] locationBytes = targetLocation.getBytes(STRING_CODING_CHARSET);
buf = ByteBuffer.allocate(24 + 4 + locationBytes.length);
buf.putInt(0, CHECKPOINT_BARRIER_EVENT);
buf.putLong(4, barrier.getId());
buf.putLong(12, barrier.getTimestamp());
buf.putInt(20, checkpointType.ordinal());
buf.putInt(24, locationBytes.length);
for (int i = 0; i < locationBytes.length; i++) {
buf.put(28 + i, locationBytes[i]);
}
} else {
throw new IOException("Unknown checkpoint type: " + checkpointType);
}
return buf;
} else if (eventClass == EndOfSuperstepEvent.class) {
return ByteBuffer.wrap(new byte[] { 0, 0, 0, END_OF_SUPERSTEP_EVENT });
} else if (eventClass == CancelCheckpointMarker.class) {
CancelCheckpointMarker marker = (CancelCheckpointMarker) event;
ByteBuffer buf = ByteBuffer.allocate(12);
buf.putInt(0, CANCEL_CHECKPOINT_MARKER_EVENT);
buf.putLong(4, marker.getCheckpointId());
return buf;
} else {
try {
final DataOutputSerializer serializer = new DataOutputSerializer(128);
serializer.writeInt(OTHER_EVENT);
serializer.writeUTF(event.getClass().getName());
event.write(serializer);
return serializer.wrapAsByteBuffer();
} catch (IOException e) {
throw new IOException("Error while serializing event.", e);
}
}
}
use of org.apache.flink.runtime.util.DataOutputSerializer in project flink by apache.
the class KvStateRequestSerializer method serializeMap.
/**
* Serializes all values of the Iterable with the given serializer.
*
* @param entries Key-value pairs to serialize
* @param keySerializer Serializer for UK
* @param valueSerializer Serializer for UV
* @param <UK> Type of the keys
* @param <UV> Type of the values
* @return Serialized values or <code>null</code> if values <code>null</code> or empty
* @throws IOException On failure during serialization
*/
public static <UK, UV> byte[] serializeMap(Iterable<Map.Entry<UK, UV>> entries, TypeSerializer<UK> keySerializer, TypeSerializer<UV> valueSerializer) throws IOException {
if (entries != null) {
// Serialize
DataOutputSerializer dos = new DataOutputSerializer(32);
for (Map.Entry<UK, UV> entry : entries) {
keySerializer.serialize(entry.getKey(), dos);
if (entry.getValue() == null) {
dos.writeBoolean(true);
} else {
dos.writeBoolean(false);
valueSerializer.serialize(entry.getValue(), dos);
}
}
return dos.getCopyOfBuffer();
} else {
return null;
}
}
Aggregations