Java Code Examples for org.apache.flink.types.Either

The following examples show how to use org.apache.flink.types.Either. These examples are extracted from open source projects. 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 Project: flink   Source File: RemoteChannelStateChecker.java    License: Apache License 2.0 6 votes vote down vote up
public boolean isProducerReadyOrAbortConsumption(ResponseHandle responseHandle) {
	Either<ExecutionState, Throwable> result = responseHandle.getProducerExecutionState();
	ExecutionState consumerExecutionState = responseHandle.getConsumerExecutionState();
	if (!isConsumerStateValidForConsumption(consumerExecutionState)) {
		LOG.debug(
			"Ignore a partition producer state notification for task {}, because it's not running.",
			taskNameWithSubtask);
	}
	else if (result.isLeft() || result.right() instanceof TimeoutException) {
		boolean isProducerConsumerReady = isProducerConsumerReady(responseHandle);
		if (isProducerConsumerReady) {
			return true;
		} else {
			abortConsumptionOrIgnoreCheckResult(responseHandle);
		}
	} else {
		handleFailedCheckResult(responseHandle);
	}
	return false;
}
 
Example 2
Source Project: Flink-CEPplus   Source File: EitherSerializerSnapshot.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public TypeSerializerSchemaCompatibility<Either<L, R>> resolveSchemaCompatibility(
		TypeSerializer<Either<L, R>> newSerializer) {
	checkState(nestedSnapshot != null);

	if (newSerializer instanceof EitherSerializer) {
		// delegate compatibility check to the new snapshot class
		return CompositeTypeSerializerUtil.delegateCompatibilityCheckToNewSnapshot(
			newSerializer,
			new JavaEitherSerializerSnapshot<>(),
			nestedSnapshot.getNestedSerializerSnapshots());
	}
	else {
		return TypeSerializerSchemaCompatibility.incompatible();
	}
}
 
Example 3
Source Project: flink   Source File: AkkaRpcActor.java    License: Apache License 2.0 6 votes vote down vote up
private Either<SerializedValue<?>, AkkaRpcException> serializeRemoteResultAndVerifySize(Object result, String methodName) {
	try {
		SerializedValue<?> serializedResult = new SerializedValue<>(result);

		long resultSize = serializedResult.getByteArray().length;
		if (resultSize > maximumFramesize) {
			return Either.Right(new AkkaRpcException(
				"The method " + methodName + "'s result size " + resultSize
					+ " exceeds the maximum size " + maximumFramesize + " ."));
		} else {
			return Either.Left(serializedResult);
		}
	} catch (IOException e) {
		return Either.Right(new AkkaRpcException(
			"Failed to serialize the result for RPC call : " + methodName + '.', e));
	}
}
 
Example 4
Source Project: flink   Source File: EitherSerializerConfigSnapshot.java    License: Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public TypeSerializerSchemaCompatibility<Either<L, R>> resolveSchemaCompatibility(
		TypeSerializer<Either<L, R>> newSerializer) {

	// this class was shared between the Java Either Serializer and the
	// Scala Either serializer
	if (newSerializer.getClass() == EitherSerializer.class) {
		List<Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>> nestedSerializersAndConfigs = getNestedSerializersAndConfigs();
		return CompositeTypeSerializerUtil.delegateCompatibilityCheckToNewSnapshot(
			newSerializer,
			new JavaEitherSerializerSnapshot<>(),
			nestedSerializersAndConfigs.get(0).f1,
			nestedSerializersAndConfigs.get(1).f1);
	}
	else {
		// fall back to the backwards compatibility path
		return super.resolveSchemaCompatibility(newSerializer);
	}
}
 
Example 5
Source Project: flink   Source File: EitherSerializerTest.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testEitherWithTuple() {

Either<Tuple2<Long, Long>, Double>[] testData = new Either[] {
		Either.Left(new Tuple2<>(2L, 9L)),
		new Left<>(new Tuple2<>(Long.MIN_VALUE, Long.MAX_VALUE)),
		new Right<>(32.0),
		Right(Double.MIN_VALUE),
		Right(Double.MAX_VALUE)};

EitherTypeInfo<Tuple2<Long, Long>, Double> eitherTypeInfo = (EitherTypeInfo<Tuple2<Long, Long>, Double>)
		new EitherTypeInfo<Tuple2<Long, Long>, Double>(
		new TupleTypeInfo<Tuple2<Long, Long>>(BasicTypeInfo.LONG_TYPE_INFO, BasicTypeInfo.LONG_TYPE_INFO),
		BasicTypeInfo.DOUBLE_TYPE_INFO);
EitherSerializer<Tuple2<Long, Long>, Double> eitherSerializer =
		(EitherSerializer<Tuple2<Long, Long>, Double>) eitherTypeInfo.createSerializer(new ExecutionConfig());
SerializerTestInstance<Either<Tuple2<Long, Long>, Double>> testInstance =
		new EitherSerializerTestInstance<Either<Tuple2<Long, Long>, Double>>(
				eitherSerializer, eitherTypeInfo.getTypeClass(), -1, testData);
testInstance.testAll();
}
 
Example 6
Source Project: Flink-CEPplus   Source File: EitherSerializerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testEitherWithTupleValues() {
	@SuppressWarnings("unchecked")
	Either<Tuple2<LongValue, LongValue>, DoubleValue>[] testData = new Either[] {
		Left(new Tuple2<>(new LongValue(2L), new LongValue(9L))),
		new Left<>(new Tuple2<>(new LongValue(Long.MIN_VALUE), new LongValue(Long.MAX_VALUE))),
		new Right<>(new DoubleValue(32.0)),
		Right(new DoubleValue(Double.MIN_VALUE)),
		Right(new DoubleValue(Double.MAX_VALUE))};

	EitherTypeInfo<Tuple2<LongValue, LongValue>, DoubleValue> eitherTypeInfo = new EitherTypeInfo<>(
		new TupleTypeInfo<Tuple2<LongValue, LongValue>>(ValueTypeInfo.LONG_VALUE_TYPE_INFO, ValueTypeInfo.LONG_VALUE_TYPE_INFO),
		ValueTypeInfo.DOUBLE_VALUE_TYPE_INFO);
	EitherSerializer<Tuple2<LongValue, LongValue>, DoubleValue> eitherSerializer =
		(EitherSerializer<Tuple2<LongValue, LongValue>, DoubleValue>) eitherTypeInfo.createSerializer(new ExecutionConfig());
	SerializerTestInstance<Either<Tuple2<LongValue, LongValue>, DoubleValue>> testInstance =
		new EitherSerializerTestInstance<>(eitherSerializer, eitherTypeInfo.getTypeClass(), -1, testData);
	testInstance.testAll();
}
 
Example 7
Source Project: flink   Source File: EitherSerializerTest.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testStringDoubleEither() {

Either<String, Double>[] testData = new Either[] {
		Left("banana"),
		Left(""),
		Right(32.0),
		Right(Double.MIN_VALUE),
		Right(Double.MAX_VALUE)};

EitherTypeInfo<String, Double> eitherTypeInfo = (EitherTypeInfo<String, Double>) new EitherTypeInfo<String, Double>(
		BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.DOUBLE_TYPE_INFO);
EitherSerializer<String, Double> eitherSerializer =
		(EitherSerializer<String, Double>) eitherTypeInfo.createSerializer(new ExecutionConfig());
SerializerTestInstance<Either<String, Double>> testInstance =
		new EitherSerializerTestInstance<Either<String, Double>>(eitherSerializer, eitherTypeInfo.getTypeClass(), -1, testData);
testInstance.testAll();
}
 
Example 8
Source Project: flink   Source File: EitherSerializerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testEitherWithTupleValues() {
	@SuppressWarnings("unchecked")
	Either<Tuple2<LongValue, LongValue>, DoubleValue>[] testData = new Either[] {
		Left(new Tuple2<>(new LongValue(2L), new LongValue(9L))),
		new Left<>(new Tuple2<>(new LongValue(Long.MIN_VALUE), new LongValue(Long.MAX_VALUE))),
		new Right<>(new DoubleValue(32.0)),
		Right(new DoubleValue(Double.MIN_VALUE)),
		Right(new DoubleValue(Double.MAX_VALUE))};

	EitherTypeInfo<Tuple2<LongValue, LongValue>, DoubleValue> eitherTypeInfo = new EitherTypeInfo<>(
		new TupleTypeInfo<Tuple2<LongValue, LongValue>>(ValueTypeInfo.LONG_VALUE_TYPE_INFO, ValueTypeInfo.LONG_VALUE_TYPE_INFO),
		ValueTypeInfo.DOUBLE_VALUE_TYPE_INFO);
	EitherSerializer<Tuple2<LongValue, LongValue>, DoubleValue> eitherSerializer =
		(EitherSerializer<Tuple2<LongValue, LongValue>, DoubleValue>) eitherTypeInfo.createSerializer(new ExecutionConfig());
	SerializerTestInstance<Either<Tuple2<LongValue, LongValue>, DoubleValue>> testInstance =
		new EitherSerializerTestInstance<>(eitherSerializer, eitherTypeInfo.getTypeClass(), -1, testData);
	testInstance.testAll();
}
 
Example 9
Source Project: Flink-CEPplus   Source File: VertexCentricIteration.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void reduce(Iterable<Tuple2<K, Either<NullValue, Message>>> messages,
		Collector<Tuple2<K, Either<NullValue, Message>>> out) throws Exception {

	final Iterator<Tuple2<K, Either<NullValue, Message>>> messageIterator = messages.iterator();

	if (messageIterator.hasNext()) {

		final Tuple2<K, Either<NullValue, Message>> first = messageIterator.next();
		final K vertexID = first.f0;
		final MessageIterator<Message> messageIter = new MessageIterator<>();
		messageIter.setFirst(first.f1.right());

		@SuppressWarnings("unchecked")
		Iterator<Tuple2<?, Either<NullValue, Message>>> downcastIter =
				(Iterator<Tuple2<?, Either<NullValue, Message>>>) (Iterator<?>) messageIterator;
		messageIter.setSource(downcastIter);

		combinerFunction.set(vertexID, out);
		combinerFunction.combineMessages(messageIter);
	}
}
 
Example 10
Source Project: flink   Source File: AkkaRpcActor.java    License: Apache License 2.0 6 votes vote down vote up
private Either<SerializedValue<?>, AkkaRpcException> serializeRemoteResultAndVerifySize(Object result, String methodName) {
	try {
		SerializedValue<?> serializedResult = new SerializedValue<>(result);

		long resultSize = serializedResult.getByteArray().length;
		if (resultSize > maximumFramesize) {
			return Either.Right(new AkkaRpcException(
				"The method " + methodName + "'s result size " + resultSize
					+ " exceeds the maximum size " + maximumFramesize + " ."));
		} else {
			return Either.Left(serializedResult);
		}
	} catch (IOException e) {
		return Either.Right(new AkkaRpcException(
			"Failed to serialize the result for RPC call : " + methodName + '.', e));
	}
}
 
Example 11
Source Project: flink   Source File: EitherSerializerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testEitherWithTupleValues() {
	@SuppressWarnings("unchecked")
	Either<Tuple2<LongValue, LongValue>, DoubleValue>[] testData = new Either[] {
		Left(new Tuple2<>(new LongValue(2L), new LongValue(9L))),
		new Left<>(new Tuple2<>(new LongValue(Long.MIN_VALUE), new LongValue(Long.MAX_VALUE))),
		new Right<>(new DoubleValue(32.0)),
		Right(new DoubleValue(Double.MIN_VALUE)),
		Right(new DoubleValue(Double.MAX_VALUE))};

	EitherTypeInfo<Tuple2<LongValue, LongValue>, DoubleValue> eitherTypeInfo = new EitherTypeInfo<>(
		new TupleTypeInfo<Tuple2<LongValue, LongValue>>(ValueTypeInfo.LONG_VALUE_TYPE_INFO, ValueTypeInfo.LONG_VALUE_TYPE_INFO),
		ValueTypeInfo.DOUBLE_VALUE_TYPE_INFO);
	EitherSerializer<Tuple2<LongValue, LongValue>, DoubleValue> eitherSerializer =
		(EitherSerializer<Tuple2<LongValue, LongValue>, DoubleValue>) eitherTypeInfo.createSerializer(new ExecutionConfig());
	SerializerTestInstance<Either<Tuple2<LongValue, LongValue>, DoubleValue>> testInstance =
		new EitherSerializerTestInstance<>(eitherSerializer, eitherTypeInfo.getTypeClass(), -1, testData);
	testInstance.testAll();
}
 
Example 12
Source Project: flink   Source File: StatefulStreamingJob.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public String map(Integer value) throws Exception {
	touchState(tupleState, () -> Tuple2.of("19", 19));
	touchState(eitherState, () -> Either.Left("255"));

	final Address newAddress = Address.newBuilder()
			.setCity("New York")
			.setZip("10036")
			.setStreet("555 W 42nd St")
			.setState("NY")
			.setNum(555)
			.build();

	Address existingAddress = avroState.value();
	if (existingAddress != null) {
		if (!Objects.equals(existingAddress.getAppno(), EXPECTED_DEFAULT_VALUE)) {
			// this is expected to fail the job, if found in the output files.
			System.out.println("Wrong Default Value.");
		}
	}
	avroState.update(newAddress);

	return "";
}
 
Example 13
Source Project: Flink-CEPplus   Source File: AkkaRpcActor.java    License: Apache License 2.0 6 votes vote down vote up
private void sendAsyncResponse(CompletableFuture<?> asyncResponse, String methodName) {
	final ActorRef sender = getSender();
	Promise.DefaultPromise<Object> promise = new Promise.DefaultPromise<>();

	asyncResponse.whenComplete(
		(value, throwable) -> {
			if (throwable != null) {
				promise.failure(throwable);
			} else {
				if (isRemoteSender(sender)) {
					Either<SerializedValue<?>, AkkaRpcException> serializedResult = serializeRemoteResultAndVerifySize(value, methodName);

					if (serializedResult.isLeft()) {
						promise.success(serializedResult.left());
					} else {
						promise.failure(serializedResult.right());
					}
				} else {
					promise.success(value);
				}
			}
		});

	Patterns.pipe(promise.future(), getContext().dispatcher()).to(sender);
}
 
Example 14
Source Project: Flink-CEPplus   Source File: AkkaRpcActor.java    License: Apache License 2.0 6 votes vote down vote up
private Either<SerializedValue<?>, AkkaRpcException> serializeRemoteResultAndVerifySize(Object result, String methodName) {
	try {
		SerializedValue<?> serializedResult = new SerializedValue<>(result);

		long resultSize = serializedResult.getByteArray().length;
		if (resultSize > maximumFramesize) {
			return Either.Right(new AkkaRpcException(
				"The method " + methodName + "'s result size " + resultSize
					+ " exceeds the maximum size " + maximumFramesize + " ."));
		} else {
			return Either.Left(serializedResult);
		}
	} catch (IOException e) {
		return Either.Right(new AkkaRpcException(
			"Failed to serialize the result for RPC call : " + methodName + '.', e));
	}
}
 
Example 15
@Override
public CompletableFuture<AsynchronousOperationResult<V>> handleRequest(@Nonnull HandlerRequest<EmptyRequestBody, M> request, @Nonnull T gateway) throws RestHandlerException {

	final K key = getOperationKey(request);

	final Either<Throwable, R> operationResultOrError;
	try {
		operationResultOrError = completedOperationCache.get(key);
	} catch (UnknownOperationKeyException e) {
		return FutureUtils.completedExceptionally(
			new NotFoundException("Operation not found under key: " + key, e));
	}

	if (operationResultOrError != null) {
		if (operationResultOrError.isLeft()) {
			return CompletableFuture.completedFuture(
				AsynchronousOperationResult.completed(exceptionalOperationResultResponse(operationResultOrError.left())));
		} else {
			return CompletableFuture.completedFuture(
				AsynchronousOperationResult.completed(operationResultResponse(operationResultOrError.right())));
		}
	} else {
		return CompletableFuture.completedFuture(AsynchronousOperationResult.inProgress());
	}
}
 
Example 16
Source Project: Flink-CEPplus   Source File: ExecutionJobVertex.java    License: Apache License 2.0 6 votes vote down vote up
public Either<SerializedValue<TaskInformation>, PermanentBlobKey> getTaskInformationOrBlobKey() throws IOException {
	// only one thread should offload the task information, so let's also let only one thread
	// serialize the task information!
	synchronized (stateMonitor) {
		if (taskInformationOrBlobKey == null) {
			final BlobWriter blobWriter = graph.getBlobWriter();

			final TaskInformation taskInformation = new TaskInformation(
				jobVertex.getID(),
				jobVertex.getName(),
				parallelism,
				maxParallelism,
				jobVertex.getInvokableClassName(),
				jobVertex.getConfiguration());

			taskInformationOrBlobKey = BlobWriter.serializeAndTryOffload(
				taskInformation,
				getJobId(),
				blobWriter);
		}

		return taskInformationOrBlobKey;
	}
}
 
Example 17
Source Project: flink   Source File: TypeExtractorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testEitherHierarchy() {
	MapFunction<?, ?> function = new EitherMapper<Boolean>();
	TypeInformation<?> ti = TypeExtractor.getMapReturnTypes((MapFunction) function, BasicTypeInfo.BOOLEAN_TYPE_INFO);
	TypeInformation<?> expected = new EitherTypeInfo(BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.BOOLEAN_TYPE_INFO);
	Assert.assertEquals(expected, ti);

	function = new EitherMapper2();
	ti = TypeExtractor.getMapReturnTypes((MapFunction) function, BasicTypeInfo.STRING_TYPE_INFO);
	expected = new EitherTypeInfo(BasicTypeInfo.STRING_TYPE_INFO, new TupleTypeInfo(BasicTypeInfo.INT_TYPE_INFO));
	Assert.assertEquals(expected, ti);

	function = new EitherMapper3();
	ti = TypeExtractor.getMapReturnTypes((MapFunction) function, expected);
	Assert.assertEquals(expected, ti);

	Either<String, Tuple1<Integer>> either = new Either2();
	ti = TypeExtractor.getForObject(either);
	Assert.assertEquals(expected, ti);
}
 
Example 18
Source Project: Flink-CEPplus   Source File: StatefulStreamingJob.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public String map(Integer value) throws Exception {
	touchState(tupleState, () -> Tuple2.of("19", 19));
	touchState(eitherState, () -> Either.Left("255"));

	final Address newAddress = Address.newBuilder()
			.setCity("New York")
			.setZip("10036")
			.setStreet("555 W 42nd St")
			.setState("NY")
			.setNum(555)
			.build();

	Address existingAddress = avroState.value();
	if (existingAddress != null) {
		if (!Objects.equals(existingAddress.getAppno(), EXPECTED_DEFAULT_VALUE)) {
			// this is expected to fail the job, if found in the output files.
			System.out.println("Wrong Default Value.");
		}
	}
	avroState.update(newAddress);

	return "";
}
 
Example 19
Source Project: flink   Source File: EitherSerializerConfigSnapshot.java    License: Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public TypeSerializerSchemaCompatibility<Either<L, R>> resolveSchemaCompatibility(
		TypeSerializer<Either<L, R>> newSerializer) {

	// this class was shared between the Java Either Serializer and the
	// Scala Either serializer
	if (newSerializer.getClass() == EitherSerializer.class) {
		List<Tuple2<TypeSerializer<?>, TypeSerializerSnapshot<?>>> nestedSerializersAndConfigs = getNestedSerializersAndConfigs();
		return CompositeTypeSerializerUtil.delegateCompatibilityCheckToNewSnapshot(
			newSerializer,
			new JavaEitherSerializerSnapshot<>(),
			nestedSerializersAndConfigs.get(0).f1,
			nestedSerializersAndConfigs.get(1).f1);
	}
	else {
		// fall back to the backwards compatibility path
		return super.resolveSchemaCompatibility(newSerializer);
	}
}
 
Example 20
Source Project: flink   Source File: EitherSerializerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testStringValueDoubleValueEither() {
	@SuppressWarnings("unchecked")
	Either<StringValue, DoubleValue>[] testData = new Either[] {
		Left(new StringValue("banana")),
		Left.of(new StringValue("apple")),
		new Left(new StringValue("")),
		Right(new DoubleValue(32.0)),
		Right.of(new DoubleValue(Double.MIN_VALUE)),
		new Right(new DoubleValue(Double.MAX_VALUE))};

	EitherTypeInfo<StringValue, DoubleValue> eitherTypeInfo = new EitherTypeInfo<>(
		ValueTypeInfo.STRING_VALUE_TYPE_INFO, ValueTypeInfo.DOUBLE_VALUE_TYPE_INFO);
	EitherSerializer<StringValue, DoubleValue> eitherSerializer =
		(EitherSerializer<StringValue, DoubleValue>) eitherTypeInfo.createSerializer(new ExecutionConfig());
	SerializerTestInstance<Either<StringValue, DoubleValue>> testInstance =
		new EitherSerializerTestInstance<>(eitherSerializer, eitherTypeInfo.getTypeClass(), -1, testData);
	testInstance.testAll();
}
 
Example 21
private void collectDeposits(
        SourceContext<Either<DepositEvent, TransactionEvent>> context,
        final int indexOfThisSubtask,
        final int numberOfParallelSubtasks) {

    final int startId = (indexOfThisSubtask * NUM_ROWS) / numberOfParallelSubtasks;
    final int endId = ((indexOfThisSubtask + 1) * NUM_ROWS) / numberOfParallelSubtasks;

    for (int i = startId; i < endId; i++) {
        String accountId = ACCOUNT_ID_PREFIX + i;
        String bookEntryId = BOOK_ENTRY_ID_PREFIX + i;

        DepositEvent event = new DepositEvent(
                accountId,
                bookEntryId,
                MAX_ACCOUNT_TRANSFER,
                MAX_BOOK_TRANSFER);

        context.collect(Either.Left(event));
    }
}
 
Example 22
Source Project: flink   Source File: EitherTypeInfoFactory.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public TypeInformation<Either<L, R>> createTypeInfo(Type t, Map<String, TypeInformation<?>> genericParameters) {
	TypeInformation<?> leftType = genericParameters.get("L");
	TypeInformation<?> rightType = genericParameters.get("R");

	if (leftType == null) {
		throw new InvalidTypesException("Type extraction is not possible on Either" +
			" type as it does not contain information about the 'left' type.");
	}

	if (rightType == null) {
		throw new InvalidTypesException("Type extraction is not possible on Either" +
			" type as it does not contain information about the 'right' type.");
	}

	return new EitherTypeInfo(leftType, rightType);
}
 
Example 23
Source Project: flink   Source File: VertexCentricIteration.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void reduce(Iterable<Tuple2<K, Either<NullValue, Message>>> messages,
		Collector<Tuple2<K, Either<NullValue, Message>>> out) throws Exception {

	final Iterator<Tuple2<K, Either<NullValue, Message>>> messageIterator = messages.iterator();

	if (messageIterator.hasNext()) {

		final Tuple2<K, Either<NullValue, Message>> first = messageIterator.next();
		final K vertexID = first.f0;
		final MessageIterator<Message> messageIter = new MessageIterator<>();
		messageIter.setFirst(first.f1.right());

		@SuppressWarnings("unchecked")
		Iterator<Tuple2<?, Either<NullValue, Message>>> downcastIter =
				(Iterator<Tuple2<?, Either<NullValue, Message>>>) (Iterator<?>) messageIterator;
		messageIter.setSource(downcastIter);

		combinerFunction.set(vertexID, out);
		combinerFunction.combineMessages(messageIter);
	}
}
 
Example 24
Source Project: flink   Source File: ComputeFunction.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Sets the new value of this vertex.
 *
 * <p>This should be called at most once per ComputeFunction.
 *
 * @param newValue The new vertex value.
 */
public final void setNewVertexValue(VV newValue) {
	if (setNewVertexValueCalled) {
		throw new IllegalStateException("setNewVertexValue should only be called at most once per updateVertex");
	}
	setNewVertexValueCalled = true;

	outVertex.f1 = newValue;

	out.collect(Either.Left(outVertex));
}
 
Example 25
Source Project: flink   Source File: ComputeFunction.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
void set(K vertexId, Iterator<Edge<K, EV>> edges,
		Collector<Either<Vertex<K, VV>, Tuple2<K, Message>>> out) {

	this.outVertex.f0 = vertexId;
	this.edges = edges;
	this.out = (Collector<Either<?, ?>>) (Collector<?>) out;
	this.edgesUsed = false;
	setNewVertexValueCalled = false;
}
 
Example 26
Source Project: Flink-CEPplus   Source File: EitherSerializer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Either<L, R> deserialize(DataInputView source) throws IOException {
	boolean isLeft = source.readBoolean();
	if (isLeft) {
		return Left(leftSerializer.deserialize(source));
	}
	else {
		return Right(rightSerializer.deserialize(source));
	}
}
 
Example 27
Source Project: da-streamingledger   Source File: SyntheticSources.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates and adds two synthetic sources for {@link DepositEvent} and {@link TransactionEvent}.
 *
 * @param env              the streaming environment to add the sources to.
 * @param recordsPerSecond the number of {@link TransactionEvent} per second to generate.
 * @return a {@link DataStream} for each event type generated.
 */
public static SyntheticSources create(StreamExecutionEnvironment env, int recordsPerSecond) {

    final DataStreamSource<Either<DepositEvent, TransactionEvent>> depositsAndTransactions = env.addSource(
            new DepositsThenTransactionsSource(recordsPerSecond));

    final OutputTag<TransactionEvent> transactionsSideOutput = new OutputTag<>(
            "transactions side output",
            TypeInformation.of(TransactionEvent.class));

    final SingleOutputStreamOperator<DepositEvent> deposits = depositsAndTransactions.process(
            new ProcessFunction<Either<DepositEvent, TransactionEvent>, DepositEvent>() {

                @Override
                public void processElement(
                        Either<DepositEvent, TransactionEvent> depositOrTransaction,
                        Context context,
                        Collector<DepositEvent> out) {

                    if (depositOrTransaction.isLeft()) {
                        out.collect(depositOrTransaction.left());
                    }
                    else {
                        context.output(transactionsSideOutput, depositOrTransaction.right());
                    }
                }
            });

    final DataStream<TransactionEvent> transactions = deposits.getSideOutput(transactionsSideOutput);

    return new SyntheticSources(deposits, transactions);
}
 
Example 28
Source Project: flink   Source File: VertexCentricIteration.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void coGroup(
		Iterable<Tuple2<Vertex<K, VV>, Either<NullValue, Message>>> messages,
		Iterable<Edge<K, EV>> edgesIterator,
		Collector<Either<Vertex<K, VV>, Tuple2<K, Message>>> out) throws Exception {

	final Iterator<Tuple2<Vertex<K, VV>, Either<NullValue, Message>>> vertexIter =
			messages.iterator();

	if (vertexIter.hasNext()) {

		final Tuple2<Vertex<K, VV>, Either<NullValue, Message>> first = vertexIter.next();
		final Vertex<K, VV> vertexState = first.f0;
		final MessageIterator<Message> messageIter = new MessageIterator<>();

		if (getIterationRuntimeContext().getSuperstepNumber() == 1) {
			// there are no messages during the 1st superstep
		}
		else {
			messageIter.setFirst(first.f1.right());
			@SuppressWarnings("unchecked")
			Iterator<Tuple2<?, Either<NullValue, Message>>> downcastIter =
				(Iterator<Tuple2<?, Either<NullValue, Message>>>) (Iterator<?>) vertexIter;
			messageIter.setSource(downcastIter);
		}

		computeFunction.set(vertexState.getId(), edgesIterator.iterator(), out);
		computeFunction.compute(vertexState, messageIter);
	}
}
 
Example 29
Source Project: flink   Source File: EitherSerializer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(Either<L, R> record, DataOutputView target) throws IOException {
	if (record.isLeft()) {
		target.writeBoolean(true);
		leftSerializer.serialize(record.left(), target);
	}
	else {
		target.writeBoolean(false);
		rightSerializer.serialize(record.right(), target);
	}
}
 
Example 30
Source Project: flink   Source File: CompletedOperationCache.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns the operation result or a {@code Throwable} if the {@code CompletableFuture}
 * finished, otherwise {@code null}.
 *
 * @throws UnknownOperationKeyException If the operation is not found, and there is no ongoing
 *                                      operation under the provided key.
 */
@Nullable
public Either<Throwable, R> get(
		final K operationKey) throws UnknownOperationKeyException {
	ResultAccessTracker<R> resultAccessTracker;
	if ((resultAccessTracker = registeredOperationTriggers.get(operationKey)) == null
		&& (resultAccessTracker = completedOperations.getIfPresent(operationKey)) == null) {
		throw new UnknownOperationKeyException(operationKey);
	}

	return resultAccessTracker.accessOperationResultOrError();
}