org.apache.flink.types.Either Java Examples

The following examples show how to use org.apache.flink.types.Either. 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: VertexCentricIteration.java    From flink with 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 #2
Source File: AkkaRpcActor.java    From flink with 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 #3
Source File: EitherSerializerConfigSnapshot.java    From flink with 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 #4
Source File: EitherSerializerSnapshot.java    From Flink-CEPplus with 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 #5
Source File: EitherSerializerTest.java    From flink with 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 File: EitherSerializerTest.java    From Flink-CEPplus with 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 File: EitherSerializerTest.java    From flink with 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 File: EitherSerializerTest.java    From flink with 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 File: VertexCentricIteration.java    From Flink-CEPplus with 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 File: AkkaRpcActor.java    From flink with 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 File: EitherSerializerTest.java    From flink with 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 File: StatefulStreamingJob.java    From flink with 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 File: AkkaRpcActor.java    From Flink-CEPplus with 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 #14
Source File: AbstractAsynchronousOperationHandlers.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@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 #15
Source File: ExecutionJobVertex.java    From Flink-CEPplus with 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 #16
Source File: TypeExtractorTest.java    From flink with 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 #17
Source File: StatefulStreamingJob.java    From Flink-CEPplus with 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 #18
Source File: EitherSerializerConfigSnapshot.java    From flink with 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 #19
Source File: AkkaRpcActor.java    From Flink-CEPplus with 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 #20
Source File: EitherSerializerTest.java    From flink with 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
Source File: DepositsThenTransactionsSource.java    From da-streamingledger with Apache License 2.0 6 votes vote down vote up
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 File: EitherTypeInfoFactory.java    From flink with 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 File: RemoteChannelStateChecker.java    From flink with 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 #24
Source File: JavaTableEnvironmentITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testFromNonAtomicAndNonComposite() throws Exception {
	ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
	BatchTableEnvironment tableEnv = BatchTableEnvironment.create(env, config());

	List<Either<String, Integer>> data = new ArrayList<>();
	data.add(new Either.Left<>("Hello"));
	data.add(new Either.Right<>(42));
	data.add(new Either.Left<>("World"));

	Table table = tableEnv
		.fromDataSet(
			env.fromCollection(
				data,
				TypeInformation.of(new TypeHint<Either<String, Integer>>() { })
			),
			$("either"))
		.select($("either"));

	DataSet<Row> ds = tableEnv.toDataSet(table, Row.class);
	List<Row> results = ds.collect();
	String expected =
		"Left(Hello)\n" +
		"Left(World)\n" +
		"Right(42)\n";
	compareResultAsText(results, expected);
}
 
Example #25
Source File: EitherSerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testEitherWithObjectReuse() {
	EitherTypeInfo<LongValue, DoubleValue> eitherTypeInfo = new EitherTypeInfo<>(
		ValueTypeInfo.LONG_VALUE_TYPE_INFO, ValueTypeInfo.DOUBLE_VALUE_TYPE_INFO);
	EitherSerializer<LongValue, DoubleValue> eitherSerializer =
		(EitherSerializer<LongValue, DoubleValue>) eitherTypeInfo.createSerializer(new ExecutionConfig());

	LongValue lv = new LongValue();
	DoubleValue dv = new DoubleValue();

	Either<LongValue, DoubleValue> left = Left(lv);
	Either<LongValue, DoubleValue> right = Right(dv);

	// the first copy creates a new instance of Left
	Either<LongValue, DoubleValue> copy0 = eitherSerializer.copy(left, right);

	// then the cross-references are used for future copies
	Either<LongValue, DoubleValue> copy1 = eitherSerializer.copy(right, copy0);
	Either<LongValue, DoubleValue> copy2 = eitherSerializer.copy(left, copy1);

	// validate reference equality
	assertSame(right, copy1);
	assertSame(copy0, copy2);

	// validate reference equality of contained objects
	assertSame(right.right(), copy1.right());
	assertSame(copy0.left(), copy2.left());
}
 
Example #26
Source File: TaskDeploymentDescriptorFactory.java    From flink with Apache License 2.0 5 votes vote down vote up
private static MaybeOffloaded<TaskInformation> getSerializedTaskInformation(
	Either<SerializedValue<TaskInformation>,
		PermanentBlobKey> taskInfo) {
	return taskInfo.isLeft() ?
		new TaskDeploymentDescriptor.NonOffloaded<>(taskInfo.left()) :
		new TaskDeploymentDescriptor.Offloaded<>(taskInfo.right());
}
 
Example #27
Source File: TaskDeploymentDescriptorFactory.java    From flink with Apache License 2.0 5 votes vote down vote up
private static MaybeOffloaded<TaskInformation> getSerializedTaskInformation(
	Either<SerializedValue<TaskInformation>,
		PermanentBlobKey> taskInfo) {
	return taskInfo.isLeft() ?
		new TaskDeploymentDescriptor.NonOffloaded<>(taskInfo.left()) :
		new TaskDeploymentDescriptor.Offloaded<>(taskInfo.right());
}
 
Example #28
Source File: DepositsThenTransactionsSource.java    From da-streamingledger with Apache License 2.0 5 votes vote down vote up
@Override
public void run(SourceContext<Either<DepositEvent, TransactionEvent>> context) throws Exception {
    final int numberOfParallelSubtasks = getRuntimeContext().getNumberOfParallelSubtasks();
    final int indexOfThisSubtask = getRuntimeContext().getIndexOfThisSubtask();

    collectDeposits(context, indexOfThisSubtask, numberOfParallelSubtasks);
    collectTransactions(context, numberOfParallelSubtasks);
}
 
Example #29
Source File: ComputeFunction.java    From Flink-CEPplus with 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 #30
Source File: CompletedOperationCache.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * If present, returns the result of the asynchronous operation, and marks the result as
 * accessed. If the result is not present, this method returns null.
 */
@Nullable
public Either<Throwable, R> accessOperationResultOrError() {
	if (operationResultOrError != null) {
		markAccessed();
	}
	return operationResultOrError;
}