Search in sources :

Example 6 with FunctionEx

use of com.hazelcast.function.FunctionEx in project hazelcast by hazelcast.

the class ComputeStageImplBase method attachFilterUsingPartitionedService.

@Nonnull
@SuppressWarnings({ "unchecked", "rawtypes" })
<S, K, RET> RET attachFilterUsingPartitionedService(@Nonnull ServiceFactory<?, S> serviceFactory, @Nonnull FunctionEx<? super T, ? extends K> partitionKeyFn, @Nonnull BiPredicateEx<? super S, ? super T> filterFn) {
    checkSerializable(filterFn, "filterFn");
    checkSerializable(partitionKeyFn, "partitionKeyFn");
    serviceFactory = moveAttachedFilesToPipeline(serviceFactory);
    BiPredicateEx adaptedFilterFn = fnAdapter.adaptFilterUsingServiceFn(filterFn);
    FunctionEx adaptedPartitionKeyFn = fnAdapter.adaptKeyFn(partitionKeyFn);
    return (RET) attach(filterUsingServicePartitionedTransform(transform, serviceFactory, adaptedFilterFn, adaptedPartitionKeyFn), fnAdapter);
}
Also used : BiPredicateEx(com.hazelcast.function.BiPredicateEx) BiFunctionEx(com.hazelcast.function.BiFunctionEx) FunctionEx(com.hazelcast.function.FunctionEx) ToLongFunctionEx(com.hazelcast.function.ToLongFunctionEx) Nonnull(javax.annotation.Nonnull)

Example 7 with FunctionEx

use of com.hazelcast.function.FunctionEx in project hazelcast by hazelcast.

the class ComputeStageImplBase method attachMapUsingPartitionedServiceAsyncBatched.

@Nonnull
@SuppressWarnings({ "unchecked", "rawtypes" })
<S, K, R, RET> RET attachMapUsingPartitionedServiceAsyncBatched(@Nonnull ServiceFactory<?, S> serviceFactory, int maxBatchSize, @Nonnull FunctionEx<? super T, ? extends K> partitionKeyFn, @Nonnull BiFunctionEx<? super S, ? super List<T>, ? extends CompletableFuture<List<R>>> mapAsyncFn) {
    checkSerializable(mapAsyncFn, "mapAsyncFn");
    checkSerializable(partitionKeyFn, "partitionKeyFn");
    serviceFactory = moveAttachedFilesToPipeline(serviceFactory);
    BiFunctionEx<? super S, ? super List<T>, ? extends CompletableFuture<List<Traverser<R>>>> flatMapAsyncFn = (s, items) -> mapAsyncFn.apply(s, items).thenApply(list -> toList(list, Traversers::singleton));
    BiFunctionEx adaptedFlatMapFn = fnAdapter.adaptFlatMapUsingServiceAsyncBatchedFn(flatMapAsyncFn);
    FunctionEx adaptedPartitionKeyFn = fnAdapter.adaptKeyFn(partitionKeyFn);
    // Here we flatten the result from List<Traverser<R>> to Traverser<R>.
    // The former is used in pipeline API, the latter in core API.
    BiFunctionEx<? super S, ? super List<T>, ? extends CompletableFuture<Traverser<R>>> flattenedFn = (svc, items) -> {
        // R might actually be JetEvent<R> -- we can't represent this with static types
        CompletableFuture<List<Traverser<R>>> f = (CompletableFuture<List<Traverser<R>>>) adaptedFlatMapFn.apply(svc, items);
        return f.thenApply(res -> traverseIterable(res).flatMap(Function.identity()));
    };
    PartitionedProcessorTransform processorTransform = flatMapUsingServiceAsyncBatchedPartitionedTransform(transform, "map", serviceFactory, MAX_CONCURRENT_ASYNC_BATCHES, maxBatchSize, flattenedFn, adaptedPartitionKeyFn);
    return attach(processorTransform, fnAdapter);
}
Also used : Traverser(com.hazelcast.jet.Traverser) PartitionedProcessorTransform.partitionedCustomProcessorTransform(com.hazelcast.jet.impl.pipeline.transform.PartitionedProcessorTransform.partitionedCustomProcessorTransform) PartitionedProcessorTransform.flatMapUsingServiceAsyncBatchedPartitionedTransform(com.hazelcast.jet.impl.pipeline.transform.PartitionedProcessorTransform.flatMapUsingServiceAsyncBatchedPartitionedTransform) ProcessorTransform.flatMapUsingServiceTransform(com.hazelcast.jet.impl.pipeline.transform.ProcessorTransform.flatMapUsingServiceTransform) ProcessorTransform.mapUsingServiceTransform(com.hazelcast.jet.impl.pipeline.transform.ProcessorTransform.mapUsingServiceTransform) Collections.singletonList(java.util.Collections.singletonList) BiFunctionEx(com.hazelcast.function.BiFunctionEx) JoinClause(com.hazelcast.jet.pipeline.JoinClause) Traversers.traverseIterable(com.hazelcast.jet.Traversers.traverseIterable) Arrays.asList(java.util.Arrays.asList) ProcessorTransform.flatMapUsingServiceAsyncTransform(com.hazelcast.jet.impl.pipeline.transform.ProcessorTransform.flatMapUsingServiceAsyncTransform) Preconditions.checkTrue(com.hazelcast.internal.util.Preconditions.checkTrue) PredicateEx(com.hazelcast.function.PredicateEx) GlobalMapStatefulTransform(com.hazelcast.jet.impl.pipeline.transform.GlobalMapStatefulTransform) PeekTransform(com.hazelcast.jet.impl.pipeline.transform.PeekTransform) GeneralStage(com.hazelcast.jet.pipeline.GeneralStage) FunctionEx(com.hazelcast.function.FunctionEx) PartitionedProcessorTransform.flatMapUsingServiceAsyncPartitionedTransform(com.hazelcast.jet.impl.pipeline.transform.PartitionedProcessorTransform.flatMapUsingServiceAsyncPartitionedTransform) Collections.emptyList(java.util.Collections.emptyList) SupplierEx(com.hazelcast.function.SupplierEx) List(java.util.List) JetEvent.jetEvent(com.hazelcast.jet.impl.JetEvent.jetEvent) ProcessorTransform(com.hazelcast.jet.impl.pipeline.transform.ProcessorTransform) MergeTransform(com.hazelcast.jet.impl.pipeline.transform.MergeTransform) PartitionedProcessorTransform(com.hazelcast.jet.impl.pipeline.transform.PartitionedProcessorTransform) Sink(com.hazelcast.jet.pipeline.Sink) MapStatefulTransform(com.hazelcast.jet.impl.pipeline.transform.MapStatefulTransform) ComparatorEx(com.hazelcast.function.ComparatorEx) Util.checkSerializable(com.hazelcast.jet.impl.util.Util.checkSerializable) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) JetException(com.hazelcast.jet.JetException) BatchStage(com.hazelcast.jet.pipeline.BatchStage) GlobalFlatMapStatefulTransform(com.hazelcast.jet.impl.pipeline.transform.GlobalFlatMapStatefulTransform) SinkTransform(com.hazelcast.jet.impl.pipeline.transform.SinkTransform) ProcessorTransform.filterUsingServiceTransform(com.hazelcast.jet.impl.pipeline.transform.ProcessorTransform.filterUsingServiceTransform) PartitionedProcessorTransform.flatMapUsingServicePartitionedTransform(com.hazelcast.jet.impl.pipeline.transform.PartitionedProcessorTransform.flatMapUsingServicePartitionedTransform) DEFAULT_IDLE_TIMEOUT(com.hazelcast.jet.core.EventTimePolicy.DEFAULT_IDLE_TIMEOUT) MapTransform(com.hazelcast.jet.impl.pipeline.transform.MapTransform) TimestampTransform(com.hazelcast.jet.impl.pipeline.transform.TimestampTransform) ServiceFactory(com.hazelcast.jet.pipeline.ServiceFactory) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) BiPredicateEx(com.hazelcast.function.BiPredicateEx) FlatMapStatefulTransform(com.hazelcast.jet.impl.pipeline.transform.FlatMapStatefulTransform) StreamStage(com.hazelcast.jet.pipeline.StreamStage) ProcessorTransform.flatMapUsingServiceAsyncBatchedTransform(com.hazelcast.jet.impl.pipeline.transform.ProcessorTransform.flatMapUsingServiceAsyncBatchedTransform) Util.toList(com.hazelcast.jet.impl.util.Util.toList) ProcessorMetaSupplier(com.hazelcast.jet.core.ProcessorMetaSupplier) PartitionedProcessorTransform.filterUsingServicePartitionedTransform(com.hazelcast.jet.impl.pipeline.transform.PartitionedProcessorTransform.filterUsingServicePartitionedTransform) WatermarkPolicy.limitingLag(com.hazelcast.jet.core.WatermarkPolicy.limitingLag) Traversers(com.hazelcast.jet.Traversers) ProcessorTransform.customProcessorTransform(com.hazelcast.jet.impl.pipeline.transform.ProcessorTransform.customProcessorTransform) AbstractTransform(com.hazelcast.jet.impl.pipeline.transform.AbstractTransform) Transform(com.hazelcast.jet.impl.pipeline.transform.Transform) PartitionedProcessorTransform.mapUsingServicePartitionedTransform(com.hazelcast.jet.impl.pipeline.transform.PartitionedProcessorTransform.mapUsingServicePartitionedTransform) SinkStage(com.hazelcast.jet.pipeline.SinkStage) ToLongFunctionEx(com.hazelcast.function.ToLongFunctionEx) HashJoinTransform(com.hazelcast.jet.impl.pipeline.transform.HashJoinTransform) SortTransform(com.hazelcast.jet.impl.pipeline.transform.SortTransform) EventTimePolicy.eventTimePolicy(com.hazelcast.jet.core.EventTimePolicy.eventTimePolicy) FlatMapTransform(com.hazelcast.jet.impl.pipeline.transform.FlatMapTransform) TriFunction(com.hazelcast.jet.function.TriFunction) CompletableFuture(java.util.concurrent.CompletableFuture) DEFAULT_IDLE_TIMEOUT(com.hazelcast.jet.core.EventTimePolicy.DEFAULT_IDLE_TIMEOUT) Traverser(com.hazelcast.jet.Traverser) PartitionedProcessorTransform(com.hazelcast.jet.impl.pipeline.transform.PartitionedProcessorTransform) BiFunctionEx(com.hazelcast.function.BiFunctionEx) FunctionEx(com.hazelcast.function.FunctionEx) ToLongFunctionEx(com.hazelcast.function.ToLongFunctionEx) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Util.toList(com.hazelcast.jet.impl.util.Util.toList) BiFunctionEx(com.hazelcast.function.BiFunctionEx) Nonnull(javax.annotation.Nonnull)

Example 8 with FunctionEx

use of com.hazelcast.function.FunctionEx in project hazelcast by hazelcast.

the class GrAggBuilder method buildStream.

public <A, R> StreamStage<KeyedWindowResult<K, R>> buildStream(@Nonnull AggregateOperation<A, ? extends R> aggrOp) {
    List<Transform> upstreamTransforms = toList(upstreamStages, s -> s.transform);
    FunctionAdapter fnAdapter = ADAPT_TO_JET_EVENT;
    // Casts in this expression are a workaround for JDK 8 compiler bug:
    @SuppressWarnings("unchecked") List<FunctionEx<?, ? extends K>> adaptedKeyFns = toList(keyFns, fn -> fnAdapter.adaptKeyFn((FunctionEx) fn));
    AbstractTransform transform = new WindowGroupTransform<K, R>(upstreamTransforms, wDef, adaptedKeyFns, fnAdapter.adaptAggregateOperation(aggrOp));
    pipelineImpl.connect(upstreamStages, transform);
    return new StreamStageImpl<>(transform, fnAdapter, pipelineImpl);
}
Also used : AbstractTransform(com.hazelcast.jet.impl.pipeline.transform.AbstractTransform) BiFunctionEx(com.hazelcast.function.BiFunctionEx) FunctionEx(com.hazelcast.function.FunctionEx) WindowGroupTransform(com.hazelcast.jet.impl.pipeline.transform.WindowGroupTransform) GroupTransform(com.hazelcast.jet.impl.pipeline.transform.GroupTransform) AbstractTransform(com.hazelcast.jet.impl.pipeline.transform.AbstractTransform) Transform(com.hazelcast.jet.impl.pipeline.transform.Transform) WindowGroupTransform(com.hazelcast.jet.impl.pipeline.transform.WindowGroupTransform)

Example 9 with FunctionEx

use of com.hazelcast.function.FunctionEx in project hazelcast by hazelcast.

the class GroupTransform method addToDagTwoStage.

// ---------        ---------
// | source0 |  ... | sourceN |
// ---------        ---------
// |                |
// local            local
// partitioned      partitioned
// v                v
// --------------------
// |  accumulateByKeyP  |
// --------------------
// |
// distributed
// partitioned
// v
// ---------------
// | combineByKeyP |
// ---------------
private void addToDagTwoStage(Planner p) {
    List<FunctionEx<?, ? extends K>> groupKeyFns = this.groupKeyFns;
    Vertex v1 = p.dag.newVertex(name() + FIRST_STAGE_VERTEX_NAME_SUFFIX, accumulateByKeyP(groupKeyFns, aggrOp)).localParallelism(determinedLocalParallelism());
    PlannerVertex pv2 = p.addVertex(this, name(), determinedLocalParallelism(), combineByKeyP(aggrOp, mapToOutputFn));
    p.addEdges(this, v1, (e, ord) -> e.partitioned(groupKeyFns.get(ord), HASH_CODE));
    p.dag.edge(between(v1, pv2.v).distributed().partitioned(entryKey()));
}
Also used : Vertex(com.hazelcast.jet.core.Vertex) PlannerVertex(com.hazelcast.jet.impl.pipeline.Planner.PlannerVertex) PlannerVertex(com.hazelcast.jet.impl.pipeline.Planner.PlannerVertex) FunctionEx(com.hazelcast.function.FunctionEx) BiFunctionEx(com.hazelcast.function.BiFunctionEx)

Example 10 with FunctionEx

use of com.hazelcast.function.FunctionEx in project hazelcast by hazelcast.

the class SinkTransform method addToDag.

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void addToDag(Planner p, Context context) {
    determineLocalParallelism(sink.metaSupplier().preferredLocalParallelism(), context, false);
    PlannerVertex pv = p.addVertex(this, name(), determinedLocalParallelism(), adaptingMetaSupplier(sink.metaSupplier(), ordinalsToAdapt));
    p.addEdges(this, pv.v, (e, ord) -> {
        // all the items will be routed to the member with the partition key
        if (sink.getType() == TOTAL_PARALLELISM_ONE) {
            e.allToOne(sink.name()).distributed();
        } else {
            if (sink.getType().isPartitioned()) {
                FunctionEx keyFn = sink.partitionKeyFunction();
                if (arrayIndexOf(ord, ordinalsToAdapt) >= 0) {
                    keyFn = ADAPT_TO_JET_EVENT.adaptKeyFn(keyFn);
                }
                e.partitioned(keyFn, Partitioner.defaultPartitioner());
            }
            if (sink.getType().isDistributed()) {
                e.distributed();
            }
        }
    });
}
Also used : PlannerVertex(com.hazelcast.jet.impl.pipeline.Planner.PlannerVertex) FunctionEx(com.hazelcast.function.FunctionEx)

Aggregations

FunctionEx (com.hazelcast.function.FunctionEx)44 List (java.util.List)30 Nonnull (javax.annotation.Nonnull)20 DAG (com.hazelcast.jet.core.DAG)18 Test (org.junit.Test)18 Collections.singletonList (java.util.Collections.singletonList)15 Assert.assertEquals (org.junit.Assert.assertEquals)15 Edge (com.hazelcast.jet.core.Edge)14 BiFunctionEx (com.hazelcast.function.BiFunctionEx)13 Function (java.util.function.Function)13 LongAccumulator (com.hazelcast.jet.accumulator.LongAccumulator)12 ArrayList (java.util.ArrayList)12 ToLongFunctionEx (com.hazelcast.function.ToLongFunctionEx)11 Vertex (com.hazelcast.jet.core.Vertex)11 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)11 Entry (java.util.Map.Entry)11 Assert.assertTrue (org.junit.Assert.assertTrue)11 JetException (com.hazelcast.jet.JetException)10 AggregateOperation1 (com.hazelcast.jet.aggregate.AggregateOperation1)10 QuickTest (com.hazelcast.test.annotation.QuickTest)10