Java Code Examples for org.apache.flink.core.memory.DataOutputViewStreamWrapper#flush()

The following examples show how to use org.apache.flink.core.memory.DataOutputViewStreamWrapper#flush() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IterationEventWithAggregators.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public void write(DataOutputView out) throws IOException {
	int num = this.aggNames.length;
	out.writeInt(num);

	ByteArrayOutputStream boas = new ByteArrayOutputStream();
	DataOutputViewStreamWrapper bufferStream = new DataOutputViewStreamWrapper(boas);

	for (int i = 0; i < num; i++) {
		// aggregator name and type
		out.writeUTF(this.aggNames[i]);
		out.writeUTF(this.aggregates[i].getClass().getName());

		// aggregator value indirect as a byte array
		this.aggregates[i].write(bufferStream);
		bufferStream.flush();
		byte[] bytes = boas.toByteArray();
		out.writeInt(bytes.length);
		out.write(bytes);
		boas.reset();
	}
	bufferStream.close();
	boas.close();
}
 
Example 2
Source File: ImmutableListStateTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Copied from HeapListState.getSerializedValue(Object, Object).
 */
private byte[] serializeInitValue(List<Long> toSerialize) throws IOException {
	TypeSerializer<Long> serializer = listStateDesc.getElementSerializer();

	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	DataOutputViewStreamWrapper view = new DataOutputViewStreamWrapper(baos);

	// write the same as RocksDB writes lists, with one ',' separator
	for (int i = 0; i < toSerialize.size(); i++) {
		serializer.serialize(toSerialize.get(i), view);
		if (i < toSerialize.size() - 1) {
			view.writeByte(',');
		}
	}
	view.flush();

	return baos.toByteArray();
}
 
Example 3
Source File: IterationEventWithAggregators.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void write(DataOutputView out) throws IOException {
	int num = this.aggNames.length;
	out.writeInt(num);

	ByteArrayOutputStream boas = new ByteArrayOutputStream();
	DataOutputViewStreamWrapper bufferStream = new DataOutputViewStreamWrapper(boas);

	for (int i = 0; i < num; i++) {
		// aggregator name and type
		out.writeUTF(this.aggNames[i]);
		out.writeUTF(this.aggregates[i].getClass().getName());

		// aggregator value indirect as a byte array
		this.aggregates[i].write(bufferStream);
		bufferStream.flush();
		byte[] bytes = boas.toByteArray();
		out.writeInt(bytes.length);
		out.write(bytes);
		boas.reset();
	}
	bufferStream.close();
	boas.close();
}
 
Example 4
Source File: ImmutableListStateTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Copied from HeapListState.getSerializedValue(Object, Object).
 */
private byte[] serializeInitValue(List<Long> toSerialize) throws IOException {
	TypeSerializer<Long> serializer = listStateDesc.getElementSerializer();

	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	DataOutputViewStreamWrapper view = new DataOutputViewStreamWrapper(baos);

	// write the same as RocksDB writes lists, with one ',' separator
	for (int i = 0; i < toSerialize.size(); i++) {
		serializer.serialize(toSerialize.get(i), view);
		if (i < toSerialize.size() - 1) {
			view.writeByte(',');
		}
	}
	view.flush();

	return baos.toByteArray();
}
 
Example 5
Source File: IterationEventWithAggregators.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void write(DataOutputView out) throws IOException {
	int num = this.aggNames.length;
	out.writeInt(num);

	ByteArrayOutputStream boas = new ByteArrayOutputStream();
	DataOutputViewStreamWrapper bufferStream = new DataOutputViewStreamWrapper(boas);

	for (int i = 0; i < num; i++) {
		// aggregator name and type
		out.writeUTF(this.aggNames[i]);
		out.writeUTF(this.aggregates[i].getClass().getName());

		// aggregator value indirect as a byte array
		this.aggregates[i].write(bufferStream);
		bufferStream.flush();
		byte[] bytes = boas.toByteArray();
		out.writeInt(bytes.length);
		out.write(bytes);
		boas.reset();
	}
	bufferStream.close();
	boas.close();
}
 
Example 6
Source File: ImmutableListStateTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Copied from HeapListState.getSerializedValue(Object, Object).
 */
private byte[] serializeInitValue(List<Long> toSerialize) throws IOException {
	TypeSerializer<Long> serializer = listStateDesc.getElementSerializer();

	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	DataOutputViewStreamWrapper view = new DataOutputViewStreamWrapper(baos);

	// write the same as RocksDB writes lists, with one ',' separator
	for (int i = 0; i < toSerialize.size(); i++) {
		serializer.serialize(toSerialize.get(i), view);
		if (i < toSerialize.size() - 1) {
			view.writeByte(',');
		}
	}
	view.flush();

	return baos.toByteArray();
}
 
Example 7
Source File: HeapListState.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] getSerializedValue(
		final byte[] serializedKeyAndNamespace,
		final TypeSerializer<K> safeKeySerializer,
		final TypeSerializer<N> safeNamespaceSerializer,
		final TypeSerializer<List<V>> safeValueSerializer) throws Exception {

	Preconditions.checkNotNull(serializedKeyAndNamespace);
	Preconditions.checkNotNull(safeKeySerializer);
	Preconditions.checkNotNull(safeNamespaceSerializer);
	Preconditions.checkNotNull(safeValueSerializer);

	Tuple2<K, N> keyAndNamespace = KvStateSerializer.deserializeKeyAndNamespace(
			serializedKeyAndNamespace, safeKeySerializer, safeNamespaceSerializer);

	List<V> result = stateTable.get(keyAndNamespace.f0, keyAndNamespace.f1);

	if (result == null) {
		return null;
	}

	final TypeSerializer<V> dupSerializer = ((ListSerializer<V>) safeValueSerializer).getElementSerializer();

	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	DataOutputViewStreamWrapper view = new DataOutputViewStreamWrapper(baos);

	// write the same as RocksDB writes lists, with one ',' separator
	for (int i = 0; i < result.size(); i++) {
		dupSerializer.serialize(result.get(i), view);
		if (i < result.size() -1) {
			view.writeByte(',');
		}
	}
	view.flush();

	return baos.toByteArray();
}
 
Example 8
Source File: HeapListState.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] getSerializedValue(
		final byte[] serializedKeyAndNamespace,
		final TypeSerializer<K> safeKeySerializer,
		final TypeSerializer<N> safeNamespaceSerializer,
		final TypeSerializer<List<V>> safeValueSerializer) throws Exception {

	Preconditions.checkNotNull(serializedKeyAndNamespace);
	Preconditions.checkNotNull(safeKeySerializer);
	Preconditions.checkNotNull(safeNamespaceSerializer);
	Preconditions.checkNotNull(safeValueSerializer);

	Tuple2<K, N> keyAndNamespace = KvStateSerializer.deserializeKeyAndNamespace(
			serializedKeyAndNamespace, safeKeySerializer, safeNamespaceSerializer);

	List<V> result = stateTable.get(keyAndNamespace.f0, keyAndNamespace.f1);

	if (result == null) {
		return null;
	}

	final TypeSerializer<V> dupSerializer = ((ListSerializer<V>) safeValueSerializer).getElementSerializer();

	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	DataOutputViewStreamWrapper view = new DataOutputViewStreamWrapper(baos);

	// write the same as RocksDB writes lists, with one ',' separator
	for (int i = 0; i < result.size(); i++) {
		dupSerializer.serialize(result.get(i), view);
		if (i < result.size() -1) {
			view.writeByte(',');
		}
	}
	view.flush();

	return baos.toByteArray();
}
 
Example 9
Source File: HeapListState.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] getSerializedValue(
		final byte[] serializedKeyAndNamespace,
		final TypeSerializer<K> safeKeySerializer,
		final TypeSerializer<N> safeNamespaceSerializer,
		final TypeSerializer<List<V>> safeValueSerializer) throws Exception {

	Preconditions.checkNotNull(serializedKeyAndNamespace);
	Preconditions.checkNotNull(safeKeySerializer);
	Preconditions.checkNotNull(safeNamespaceSerializer);
	Preconditions.checkNotNull(safeValueSerializer);

	Tuple2<K, N> keyAndNamespace = KvStateSerializer.deserializeKeyAndNamespace(
			serializedKeyAndNamespace, safeKeySerializer, safeNamespaceSerializer);

	List<V> result = stateTable.get(keyAndNamespace.f0, keyAndNamespace.f1);

	if (result == null) {
		return null;
	}

	final TypeSerializer<V> dupSerializer = ((ListSerializer<V>) safeValueSerializer).getElementSerializer();

	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	DataOutputViewStreamWrapper view = new DataOutputViewStreamWrapper(baos);

	// write the same as RocksDB writes lists, with one ',' separator
	for (int i = 0; i < result.size(); i++) {
		dupSerializer.serialize(result.get(i), view);
		if (i < result.size() -1) {
			view.writeByte(',');
		}
	}
	view.flush();

	return baos.toByteArray();
}