Java Code Examples for org.apache.flink.core.memory.DataInputView#readInt()

The following examples show how to use org.apache.flink.core.memory.DataInputView#readInt() . 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: TypeSerializerSerializationUtil.java    From flink with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void read(DataInputView in) throws IOException {
	super.read(in);

	// read in a way that allows the stream to recover from exceptions
	int serializerBytes = in.readInt();
	byte[] buffer = new byte[serializerBytes];
	in.readFully(buffer);

	ClassLoader previousClassLoader = Thread.currentThread().getContextClassLoader();
	try (
		InstantiationUtil.FailureTolerantObjectInputStream ois =
			new InstantiationUtil.FailureTolerantObjectInputStream(new ByteArrayInputStream(buffer), userClassLoader)) {

		Thread.currentThread().setContextClassLoader(userClassLoader);
		typeSerializer = (TypeSerializer<T>) ois.readObject();
	} catch (Exception e) {
		throw new UnloadableTypeSerializerException(e, buffer);
	} finally {
		Thread.currentThread().setContextClassLoader(previousClassLoader);
	}
}
 
Example 2
Source File: StreamElementSerializer.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public StreamElement deserialize(DataInputView source) throws IOException {
	int tag = source.readByte();
	if (tag == TAG_REC_WITH_TIMESTAMP) {
		long timestamp = source.readLong();
		return new StreamRecord<T>(typeSerializer.deserialize(source), timestamp);
	}
	else if (tag == TAG_REC_WITHOUT_TIMESTAMP) {
		return new StreamRecord<T>(typeSerializer.deserialize(source));
	}
	else if (tag == TAG_WATERMARK) {
		return new Watermark(source.readLong());
	}
	else if (tag == TAG_STREAM_STATUS) {
		return new StreamStatus(source.readInt());
	}
	else if (tag == TAG_LATENCY_MARKER) {
		return new LatencyMarker(source.readLong(), new OperatorID(source.readLong(), source.readLong()), source.readInt());
	}
	else {
		throw new IOException("Corrupt stream, found tag: " + tag);
	}
}
 
Example 3
Source File: BaseRowSerializer.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void readSnapshot(int readVersion, DataInputView in, ClassLoader userCodeClassLoader)
		throws IOException {
	int length = in.readInt();
	DataInputViewStream stream = new DataInputViewStream(in);
	previousTypes = new LogicalType[length];
	for (int i = 0; i < length; i++) {
		try {
			previousTypes[i] = InstantiationUtil.deserializeObject(
					stream,
					userCodeClassLoader
			);
		}
		catch (ClassNotFoundException e) {
			throw new IOException(e);
		}
	}
	this.nestedSerializersSnapshotDelegate = NestedSerializersSnapshotDelegate.readNestedSerializerSnapshots(
			in,
			userCodeClassLoader
	);
}
 
Example 4
Source File: NullAwareMapSerializer.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void copy(DataInputView source, DataOutputView target) throws IOException {
	final int size = source.readInt();
	target.writeInt(size);

	for (int i = 0; i < size; ++i) {
		boolean keyIsNull = source.readBoolean();
		target.writeBoolean(keyIsNull);
		if (!keyIsNull) {
			keySerializer.copy(source, target);
		}

		boolean valueIsNull = source.readBoolean();
		target.writeBoolean(valueIsNull);

		if (!valueIsNull) {
			valueSerializer.copy(source, target);
		}
	}
}
 
Example 5
Source File: KeyGroupPartitioner.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void readMappingsInKeyGroup(@Nonnull DataInputView in, @Nonnegative int keyGroupId) throws IOException {
	int numElements = in.readInt();
	for (int i = 0; i < numElements; i++) {
		T element = readerFunction.readElement(in);
		elementConsumer.consume(element, keyGroupId);
	}
}
 
Example 6
Source File: BucketStateSerializer.java    From flink with Apache License 2.0 5 votes vote down vote up
private BucketState<BucketID> deserializeV2(DataInputView dataInputView) throws IOException {
	final BucketID bucketId = SimpleVersionedSerialization.readVersionAndDeSerialize(bucketIdSerializer, dataInputView);
	final String bucketPathStr = dataInputView.readUTF();
	final long creationTime = dataInputView.readLong();

	// then get the current resumable stream
	InProgressFileWriter.InProgressFileRecoverable current = null;
	if (dataInputView.readBoolean()) {
		current = SimpleVersionedSerialization.readVersionAndDeSerialize(inProgressFileRecoverableSerializer, dataInputView);
	}

	final int pendingFileRecoverableSerializerVersion = dataInputView.readInt();
	final int numCheckpoints = dataInputView.readInt();
	final HashMap<Long, List<InProgressFileWriter.PendingFileRecoverable>> pendingFileRecoverablesPerCheckpoint = new HashMap<>(numCheckpoints);

	for (int i = 0; i < numCheckpoints; i++) {
		final long checkpointId = dataInputView.readLong();
		final int numOfPendingFileRecoverables = dataInputView.readInt();

		final List<InProgressFileWriter.PendingFileRecoverable> pendingFileRecoverables = new ArrayList<>(numOfPendingFileRecoverables);
		for (int j = 0; j < numOfPendingFileRecoverables; j++) {
			final byte[] bytes = new byte[dataInputView.readInt()];
			dataInputView.readFully(bytes);
			pendingFileRecoverables.add(pendingFileRecoverableSerializer.deserialize(pendingFileRecoverableSerializerVersion, bytes));
		}
		pendingFileRecoverablesPerCheckpoint.put(checkpointId, pendingFileRecoverables);
	}

	return new BucketState<>(bucketId, new Path(bucketPathStr), creationTime, current, pendingFileRecoverablesPerCheckpoint);
}
 
Example 7
Source File: KeyGroupPartitioner.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void readMappingsInKeyGroup(@Nonnull DataInputView in, @Nonnegative int keyGroupId) throws IOException {
	int numElements = in.readInt();
	for (int i = 0; i < numElements; i++) {
		T element = readerFunction.readElement(in);
		elementConsumer.consume(element, keyGroupId);
	}
}
 
Example 8
Source File: SiddhiStreamOperator.java    From bahir-flink with Apache License 2.0 5 votes vote down vote up
@Override
protected PriorityQueue<StreamRecord<Tuple2<String, IN>>> restoreQueuerState(DataInputView dataInputView) throws IOException {
    int snapshotSize = dataInputView.readInt();
    int sizeOfQueue = snapshotSize > 0 ? snapshotSize : this.INITIAL_PRIORITY_QUEUE_CAPACITY;
    PriorityQueue<StreamRecord<Tuple2<String, IN>>> priorityQueue = new PriorityQueue<>(sizeOfQueue);
    for (int i = 0; i < snapshotSize; i++) {
        String streamId = dataInputView.readUTF();
        StreamElement streamElement = getStreamRecordSerializer(streamId).deserialize(dataInputView);
        priorityQueue.offer(streamElement.<Tuple2<String, IN>>asRecord());
    }
    return priorityQueue;
}
 
Example 9
Source File: GenericArraySerializer.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void copy(DataInputView source, DataOutputView target) throws IOException {
	int len = source.readInt();
	target.writeInt(len);
	
	for (int i = 0; i < len; i++) {
		boolean isNonNull = source.readBoolean();
		target.writeBoolean(isNonNull);
		
		if (isNonNull) {
			componentSerializer.copy(source, target);
		}
	}
}
 
Example 10
Source File: FloatValueArray.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void read(DataInputView in) throws IOException {
	position = in.readInt();
	mark = 0;

	ensureCapacity(position);

	for (int i = 0; i < position; i++) {
		data[i] = in.readFloat();
	}
}
 
Example 11
Source File: StateInitializationContextImplTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void close() throws Exception {

	int count = 0;
	int stopCount = NUM_HANDLES / 2;
	boolean isClosed = false;

	try {
		for (KeyGroupStatePartitionStreamProvider stateStreamProvider
				: initializationContext.getRawKeyedStateInputs()) {
			Assert.assertNotNull(stateStreamProvider);

			if (count == stopCount) {
				closableRegistry.close();
				isClosed = true;
			}

			try (InputStream is = stateStreamProvider.getStream()) {
				DataInputView div = new DataInputViewStreamWrapper(is);
				try {
					int val = div.readInt();
					Assert.assertEquals(stateStreamProvider.getKeyGroupId(), val);
					if (isClosed) {
						Assert.fail("Close was ignored: stream");
					}
					++count;
				} catch (IOException ioex) {
					if (!isClosed) {
						throw ioex;
					}
				}
			}
		}
		Assert.fail("Close was ignored: registry");
	} catch (IOException iex) {
		Assert.assertTrue(isClosed);
		Assert.assertEquals(stopCount, count);
	}

}
 
Example 12
Source File: KryoSerializerSnapshotData.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
static KryoRegistration tryReadKryoRegistration(
	DataInputView in,
	ClassLoader userCodeClassLoader) throws IOException {

	String registeredClassname = in.readUTF();
	Class<?> registeredClass;
	try {
		registeredClass = Class.forName(registeredClassname, true, userCodeClassLoader);
	}
	catch (ClassNotFoundException e) {
		LOG.warn("Cannot find registered class " + registeredClassname + " for Kryo serialization in classpath;" +
			" using a dummy class as a placeholder.", e);
		return null;
	}

	final KryoRegistration.SerializerDefinitionType serializerDefinitionType =
		KryoRegistration.SerializerDefinitionType.values()[in.readInt()];

	switch (serializerDefinitionType) {
		case UNSPECIFIED: {
			return new KryoRegistration(registeredClass);
		}
		case CLASS: {
			return tryReadWithSerializerClass(in, userCodeClassLoader, registeredClassname, registeredClass);
		}
		case INSTANCE: {
			return tryReadWithSerializerInstance(in, userCodeClassLoader, registeredClassname, registeredClass);
		}
		default: {
			throw new IllegalStateException(
				"Unrecognized Kryo registration serializer definition type: " + serializerDefinitionType);
		}
	}
}
 
Example 13
Source File: ExternalSortLargeRecordsITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void read(DataInputView in) throws IOException {
	val = in.readInt();
	size = in.readInt();
	
	for (int i = 0; i < size; i++) {
		byte b = in.readByte();
		assertEquals((byte) i, b);
	}
}
 
Example 14
Source File: UnknownTupleSerializer.java    From cascading-flink with Apache License 2.0 5 votes vote down vote up
@Override
public Tuple deserialize(DataInputView source) throws IOException {

	// read length
	int arity = source.readInt();

	// initialize or resize null fields if necessary
	if(this.nullFields == null || this.nullFields.length < arity) {
		this.nullFields = new boolean[arity];
	}

	// read null mask
	NullMaskSerDeUtils.readNullMask(this.nullFields, arity, source);

	// read non-null fields
	Tuple tuple = Tuple.size(arity);
	for (int i = 0; i < arity; i++) {
		Object field;
		if(!this.nullFields[i]) {
			field = fieldSer.deserialize(source);
		}
		else {
			field = null;
		}
		tuple.set(i, field);
	}

	return tuple;
}
 
Example 15
Source File: FloatPrimitiveArraySerializer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public float[] deserialize(DataInputView source) throws IOException {
	final int len = source.readInt();
	float[] result = new float[len];
	
	for (int i = 0; i < len; i++) {
		result[i] = source.readFloat();
	}
	
	return result;
}
 
Example 16
Source File: IntegerTaskEvent.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void read(final DataInputView in) throws IOException {
	this.value = in.readInt();
}
 
Example 17
Source File: IntPairSerializer.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public IntPair deserialize(DataInputView source) throws IOException {
	return new IntPair(source.readInt(), source.readInt());
}
 
Example 18
Source File: ProtobufSerializer.java    From flink-statefun with Apache License 2.0 4 votes vote down vote up
public void copy(DataInputView source, DataOutputView target) throws IOException {
  int serializedSize = source.readInt();
  target.writeInt(serializedSize);
  target.write(source, serializedSize);
}
 
Example 19
Source File: IntType.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public void read(DataInputView in) throws IOException {
	this.value = in.readInt();
}
 
Example 20
Source File: BytePrimitiveArraySerializer.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public void copy(DataInputView source, DataOutputView target) throws IOException {
	final int len = source.readInt();
	target.writeInt(len);
	target.write(source, len);
}