Search in sources :

Example 1 with Tag

use of com.hazelcast.jet.datamodel.Tag in project hazelcast-jet by hazelcast.

the class AggregateTransform_IntegrationTest method test_aggregate3_with_aggBuilder.

@Test
public void test_aggregate3_with_aggBuilder() {
    // Given
    JetInstance instance = createJetMember();
    IListJet<Integer> list = instance.getList("list");
    list.add(1);
    list.add(2);
    list.add(3);
    IListJet<String> list1 = instance.getList("list1");
    list1.add("a");
    list1.add("b");
    list1.add("c");
    IListJet<Double> list2 = instance.getList("list2");
    list2.add(6.0d);
    list2.add(7.0d);
    list2.add(8.0d);
    // When
    Pipeline p = Pipeline.create();
    BatchStage<Integer> stage0 = p.drawFrom(Sources.list("list"));
    BatchStage<String> stage1 = p.drawFrom(Sources.list("list1"));
    BatchStage<Double> stage2 = p.drawFrom(Sources.list("list2"));
    AggregateBuilder<Integer> builder = stage0.aggregateBuilder();
    Tag<Integer> tag0 = builder.tag0();
    Tag<String> tag1 = builder.add(stage1);
    Tag<Double> tag2 = builder.add(stage2);
    BatchStage<ThreeBags> resultStage = builder.build(AggregateOperation.withCreate(ThreeBags::threeBags).andAccumulate(tag0, (acc, item0) -> acc.bag0().add(item0)).andAccumulate(tag1, (acc, item1) -> acc.bag1().add(item1)).andAccumulate(tag2, (acc, item2) -> acc.bag2().add(item2)).andCombine(ThreeBags::combineWith).andDeduct(ThreeBags::deduct).andFinish(ThreeBags::finish));
    resultStage.drainTo(Sinks.list("sink"));
    instance.newJob(p).join();
    // Then
    ThreeBags threeBags = (ThreeBags) instance.getHazelcastInstance().getList("sink").iterator().next();
    assertNotNull(threeBags);
    sort(threeBags);
    assertEquals(threeBags(list, list1, list2), threeBags);
}
Also used : JetInstance(com.hazelcast.jet.JetInstance) RunWith(org.junit.runner.RunWith) ThreeBags.threeBags(com.hazelcast.jet.datamodel.ThreeBags.threeBags) TestUtil.set(com.hazelcast.jet.core.TestUtil.set) Collections.singletonList(java.util.Collections.singletonList) BatchStage(com.hazelcast.jet.pipeline.BatchStage) ThreeBags(com.hazelcast.jet.datamodel.ThreeBags) AggregateOperation(com.hazelcast.jet.aggregate.AggregateOperation) Util.entry(com.hazelcast.jet.Util.entry) AggregateOperations.toThreeBags(com.hazelcast.jet.aggregate.AggregateOperations.toThreeBags) Pipeline(com.hazelcast.jet.pipeline.Pipeline) JetTestSupport(com.hazelcast.jet.core.JetTestSupport) AggregateOperations.toTwoBags(com.hazelcast.jet.aggregate.AggregateOperations.toTwoBags) Assert.assertNotNull(org.junit.Assert.assertNotNull) AggregateOperations.toSet(com.hazelcast.jet.aggregate.AggregateOperations.toSet) Tag(com.hazelcast.jet.datamodel.Tag) Sinks(com.hazelcast.jet.pipeline.Sinks) Test(org.junit.Test) IListJet(com.hazelcast.jet.IListJet) AggregateBuilder(com.hazelcast.jet.pipeline.AggregateBuilder) ParallelTest(com.hazelcast.test.annotation.ParallelTest) Category(org.junit.experimental.categories.Category) Sources(com.hazelcast.jet.pipeline.Sources) List(java.util.List) IMap(com.hazelcast.core.IMap) HazelcastParallelClassRunner(com.hazelcast.test.HazelcastParallelClassRunner) TwoBags(com.hazelcast.jet.datamodel.TwoBags) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) TestSupport.listToString(com.hazelcast.jet.core.test.TestSupport.listToString) JetInstance(com.hazelcast.jet.JetInstance) TestSupport.listToString(com.hazelcast.jet.core.test.TestSupport.listToString) Pipeline(com.hazelcast.jet.pipeline.Pipeline) ThreeBags(com.hazelcast.jet.datamodel.ThreeBags) AggregateOperations.toThreeBags(com.hazelcast.jet.aggregate.AggregateOperations.toThreeBags) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 2 with Tag

use of com.hazelcast.jet.datamodel.Tag in project hazelcast-jet by hazelcast.

the class WindowAggregateTransform_IntegrationTest method test_aggregate2_with_aggregateBuilder.

@Test
public void test_aggregate2_with_aggregateBuilder() {
    IMap<Long, String> map = instance.getMap("source");
    // key is timestamp
    map.put(0L, "foo");
    map.put(2L, "baz");
    map.put(10L, "flush-item");
    IMap<Long, String> map2 = instance.getMap("source1");
    // key is timestamp
    map2.put(0L, "faa");
    map2.put(2L, "buu");
    map2.put(10L, "flush-item");
    Pipeline p = Pipeline.create();
    StreamStage<Entry<Long, String>> stage1 = p.drawFrom(Sources.<Long, String>mapJournal("source1", START_FROM_OLDEST));
    stage1.addTimestamps(Entry::getKey, 0);
    WindowAggregateBuilder<Entry<Long, String>> b = p.drawFrom(Sources.<Long, String>mapJournal("source", START_FROM_OLDEST)).addTimestamps(Entry::getKey, 0).window(WindowDefinition.tumbling(2)).aggregateBuilder();
    Tag<Entry<Long, String>> tag0 = b.tag0();
    Tag<Entry<Long, String>> tag1 = b.add(stage1);
    b.build(AggregateOperation.withCreate(TwoBags::twoBags).andAccumulate(tag0, (acc, item0) -> acc.bag0().add(item0)).andAccumulate(tag1, (acc, item1) -> acc.bag1().add(item1)).andCombine(TwoBags::combineWith).andDeduct(TwoBags::deduct).andFinish(TwoBags::finish)).peek().drainTo(Sinks.list("sink"));
    instance.newJob(p);
    assertTrueEventually(() -> assertEquals(listToString(asList(new TimestampedItem<>(2, TwoBags.twoBags(asList(entry(0L, "foo")), asList(entry(0L, "faa")))), new TimestampedItem<>(4, TwoBags.twoBags(asList(entry(2L, "baz")), asList(entry(2L, "buu")))))), listToString(instance.getHazelcastInstance().getList("sink"))), 5);
}
Also used : AggregateOperations.toTwoBags(com.hazelcast.jet.aggregate.AggregateOperations.toTwoBags) TwoBags(com.hazelcast.jet.datamodel.TwoBags) JetInstance(com.hazelcast.jet.JetInstance) GroupProperty(com.hazelcast.spi.properties.GroupProperty) WindowAggregateBuilder(com.hazelcast.jet.pipeline.WindowAggregateBuilder) RunWith(org.junit.runner.RunWith) EventJournalConfig(com.hazelcast.config.EventJournalConfig) TestUtil.set(com.hazelcast.jet.core.TestUtil.set) ThreeBags(com.hazelcast.jet.datamodel.ThreeBags) AggregateOperation(com.hazelcast.jet.aggregate.AggregateOperation) SlidingWindowDef(com.hazelcast.jet.pipeline.SlidingWindowDef) Util.entry(com.hazelcast.jet.Util.entry) Arrays.asList(java.util.Arrays.asList) Before(org.junit.Before) AggregateOperations.toThreeBags(com.hazelcast.jet.aggregate.AggregateOperations.toThreeBags) JetConfig(com.hazelcast.jet.config.JetConfig) StreamStage(com.hazelcast.jet.pipeline.StreamStage) WindowDefinition(com.hazelcast.jet.pipeline.WindowDefinition) Pipeline(com.hazelcast.jet.pipeline.Pipeline) JetTestSupport(com.hazelcast.jet.core.JetTestSupport) AggregateOperations.toTwoBags(com.hazelcast.jet.aggregate.AggregateOperations.toTwoBags) AggregateOperations.toSet(com.hazelcast.jet.aggregate.AggregateOperations.toSet) Tag(com.hazelcast.jet.datamodel.Tag) Sinks(com.hazelcast.jet.pipeline.Sinks) Test(org.junit.Test) START_FROM_OLDEST(com.hazelcast.jet.pipeline.JournalInitialPosition.START_FROM_OLDEST) ParallelTest(com.hazelcast.test.annotation.ParallelTest) Category(org.junit.experimental.categories.Category) StageWithWindow(com.hazelcast.jet.pipeline.StageWithWindow) TimestampedItem(com.hazelcast.jet.datamodel.TimestampedItem) Sources(com.hazelcast.jet.pipeline.Sources) IMap(com.hazelcast.core.IMap) HazelcastParallelClassRunner(com.hazelcast.test.HazelcastParallelClassRunner) Entry(java.util.Map.Entry) TwoBags(com.hazelcast.jet.datamodel.TwoBags) Assert.assertEquals(org.junit.Assert.assertEquals) WindowResult(com.hazelcast.jet.datamodel.WindowResult) TestSupport.listToString(com.hazelcast.jet.core.test.TestSupport.listToString) Entry(java.util.Map.Entry) TimestampedItem(com.hazelcast.jet.datamodel.TimestampedItem) TestSupport.listToString(com.hazelcast.jet.core.test.TestSupport.listToString) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 3 with Tag

use of com.hazelcast.jet.datamodel.Tag in project hazelcast-jet by hazelcast.

the class HashJoinTransform method addToDag.

// ---------           ----------           ----------
// | primary |         | joined-1 |         | joined-2 |
// ---------           ----------           ----------
// |                   |                     |
// |              distributed          distributed
// |               broadcast            broadcast
// |                   v                     v
// |             -------------         -------------
// |            | collector-1 |       | collector-2 |
// |             -------------         -------------
// |                   |                     |
// |                 local                 local
// local             broadcast             broadcast
// unicast           prioritized           prioritized
// ordinal 0           ordinal 1             ordinal 2
// \                   |                     |
// ----------------\  |   /----------------/
// v  v  v
// --------
// | joiner |
// --------
@Override
@SuppressWarnings("unchecked")
public void addToDag(Planner p) {
    String namePrefix = p.uniqueVertexName(this.name(), "");
    PlannerVertex primary = p.xform2vertex.get(this.upstream().get(0));
    List keyFns = this.clauses.stream().map(JoinClause::leftKeyFn).collect(toList());
    List<Tag> tags = this.tags;
    DistributedBiFunction mapToOutputBiFn = this.mapToOutputBiFn;
    DistributedTriFunction mapToOutputTriFn = this.mapToOutputTriFn;
    Vertex joiner = p.addVertex(this, namePrefix + "-joiner", localParallelism(), () -> new HashJoinP<>(keyFns, tags, mapToOutputBiFn, mapToOutputTriFn)).v;
    p.dag.edge(from(primary.v, primary.nextAvailableOrdinal()).to(joiner, 0));
    String collectorName = namePrefix + "-collector";
    int collectorOrdinal = 1;
    for (Transform fromTransform : tailList(this.upstream())) {
        PlannerVertex fromPv = p.xform2vertex.get(fromTransform);
        JoinClause<?, ?, ?, ?> clause = this.clauses.get(collectorOrdinal - 1);
        DistributedFunction<Object, Object> getKeyFn = (DistributedFunction<Object, Object>) clause.rightKeyFn();
        DistributedFunction<Object, Object> projectFn = (DistributedFunction<Object, Object>) clause.rightProjectFn();
        Vertex collector = p.dag.newVertex(collectorName + collectorOrdinal, () -> new HashJoinCollectP(getKeyFn, projectFn));
        collector.localParallelism(1);
        p.dag.edge(from(fromPv.v, fromPv.nextAvailableOrdinal()).to(collector, 0).distributed().broadcast());
        p.dag.edge(from(collector, 0).to(joiner, collectorOrdinal).broadcast().priority(-1));
        collectorOrdinal++;
    }
}
Also used : Vertex(com.hazelcast.jet.core.Vertex) PlannerVertex(com.hazelcast.jet.impl.pipeline.Planner.PlannerVertex) HashJoinCollectP(com.hazelcast.jet.impl.processor.HashJoinCollectP) HashJoinP(com.hazelcast.jet.impl.processor.HashJoinP) PlannerVertex(com.hazelcast.jet.impl.pipeline.Planner.PlannerVertex) DistributedBiFunction(com.hazelcast.jet.function.DistributedBiFunction) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Planner.tailList(com.hazelcast.jet.impl.pipeline.Planner.tailList) Tag(com.hazelcast.jet.datamodel.Tag) DistributedTriFunction(com.hazelcast.jet.function.DistributedTriFunction) DistributedFunction(com.hazelcast.jet.function.DistributedFunction)

Example 4 with Tag

use of com.hazelcast.jet.datamodel.Tag in project hazelcast-jet-reference-manual by hazelcast.

the class BuildComputation method s14.

static void s14() {
    // tag::s14[]
    Pipeline p = Pipeline.create();
    p.<Tweet>drawFrom(Sources.mapJournal("tweets", mapPutEvents(), mapEventNewValue(), START_FROM_CURRENT)).flatMap(tweet -> traverseArray(tweet.text().toLowerCase().split("\\W+")).map(word -> new TweetWord(tweet.timestamp(), word))).filter(tweetWord -> !tweetWord.word().isEmpty()).addTimestamps(TweetWord::timestamp, TimeUnit.SECONDS.toMillis(5)).window(sliding(MINUTES.toMillis(1), SECONDS.toMillis(1))).groupingKey(TweetWord::word).aggregate(counting()).drainTo(Sinks.list("result"));
// end::s14[]
}
Also used : AggregateOperations.counting(com.hazelcast.jet.aggregate.AggregateOperations.counting) Delivery(datamodel.Delivery) MINUTES(java.util.concurrent.TimeUnit.MINUTES) Tweet(datamodel.Tweet) JoinClause.joinMapEntries(com.hazelcast.jet.pipeline.JoinClause.joinMapEntries) DistributedFunctions.wholeItem(com.hazelcast.jet.function.DistributedFunctions.wholeItem) BatchStage(com.hazelcast.jet.pipeline.BatchStage) Util.mapEventNewValue(com.hazelcast.jet.Util.mapEventNewValue) PageVisit(datamodel.PageVisit) AggregateOperation(com.hazelcast.jet.aggregate.AggregateOperation) START_FROM_CURRENT(com.hazelcast.jet.pipeline.JournalInitialPosition.START_FROM_CURRENT) DAG(com.hazelcast.jet.core.DAG) StreamHashJoinBuilder(com.hazelcast.jet.pipeline.StreamHashJoinBuilder) Tuple2(com.hazelcast.jet.datamodel.Tuple2) AddToCart(datamodel.AddToCart) Tuple3(com.hazelcast.jet.datamodel.Tuple3) StreamStage(com.hazelcast.jet.pipeline.StreamStage) StageWithGrouping(com.hazelcast.jet.pipeline.StageWithGrouping) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Market(datamodel.Market) AggregateOperation2(com.hazelcast.jet.aggregate.AggregateOperation2) Tag(com.hazelcast.jet.datamodel.Tag) Sinks(com.hazelcast.jet.pipeline.Sinks) Payment(datamodel.Payment) Sources(com.hazelcast.jet.pipeline.Sources) Product(datamodel.Product) TimeUnit(java.util.concurrent.TimeUnit) BagsByTag(com.hazelcast.jet.datamodel.BagsByTag) WindowDefinition.sliding(com.hazelcast.jet.pipeline.WindowDefinition.sliding) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) Traversers.traverseArray(com.hazelcast.jet.Traversers.traverseArray) ItemsByTag(com.hazelcast.jet.datamodel.ItemsByTag) Entry(java.util.Map.Entry) Trade(datamodel.Trade) TweetWord(datamodel.TweetWord) Util.mapPutEvents(com.hazelcast.jet.Util.mapPutEvents) AggregateOperations.toBagsByTag(com.hazelcast.jet.aggregate.AggregateOperations.toBagsByTag) GroupAggregateBuilder(com.hazelcast.jet.pipeline.GroupAggregateBuilder) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Broker(datamodel.Broker) TweetWord(datamodel.TweetWord) Pipeline(com.hazelcast.jet.pipeline.Pipeline)

Example 5 with Tag

use of com.hazelcast.jet.datamodel.Tag in project hazelcast-jet-reference-manual by hazelcast.

the class BuildComputation method s5.

static void s5() {
    // tag::s5[]
    Pipeline p = Pipeline.create();
    p.drawFrom(Sources.<String>list("text")).flatMap(line -> traverseArray(line.toLowerCase().split("\\W+"))).filter(word -> !word.isEmpty()).aggregate(counting()).drainTo(Sinks.list("result"));
// end::s5[]
}
Also used : AggregateOperations.counting(com.hazelcast.jet.aggregate.AggregateOperations.counting) Delivery(datamodel.Delivery) MINUTES(java.util.concurrent.TimeUnit.MINUTES) Tweet(datamodel.Tweet) JoinClause.joinMapEntries(com.hazelcast.jet.pipeline.JoinClause.joinMapEntries) DistributedFunctions.wholeItem(com.hazelcast.jet.function.DistributedFunctions.wholeItem) BatchStage(com.hazelcast.jet.pipeline.BatchStage) Util.mapEventNewValue(com.hazelcast.jet.Util.mapEventNewValue) PageVisit(datamodel.PageVisit) AggregateOperation(com.hazelcast.jet.aggregate.AggregateOperation) START_FROM_CURRENT(com.hazelcast.jet.pipeline.JournalInitialPosition.START_FROM_CURRENT) DAG(com.hazelcast.jet.core.DAG) StreamHashJoinBuilder(com.hazelcast.jet.pipeline.StreamHashJoinBuilder) Tuple2(com.hazelcast.jet.datamodel.Tuple2) AddToCart(datamodel.AddToCart) Tuple3(com.hazelcast.jet.datamodel.Tuple3) StreamStage(com.hazelcast.jet.pipeline.StreamStage) StageWithGrouping(com.hazelcast.jet.pipeline.StageWithGrouping) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Market(datamodel.Market) AggregateOperation2(com.hazelcast.jet.aggregate.AggregateOperation2) Tag(com.hazelcast.jet.datamodel.Tag) Sinks(com.hazelcast.jet.pipeline.Sinks) Payment(datamodel.Payment) Sources(com.hazelcast.jet.pipeline.Sources) Product(datamodel.Product) TimeUnit(java.util.concurrent.TimeUnit) BagsByTag(com.hazelcast.jet.datamodel.BagsByTag) WindowDefinition.sliding(com.hazelcast.jet.pipeline.WindowDefinition.sliding) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) Traversers.traverseArray(com.hazelcast.jet.Traversers.traverseArray) ItemsByTag(com.hazelcast.jet.datamodel.ItemsByTag) Entry(java.util.Map.Entry) Trade(datamodel.Trade) TweetWord(datamodel.TweetWord) Util.mapPutEvents(com.hazelcast.jet.Util.mapPutEvents) AggregateOperations.toBagsByTag(com.hazelcast.jet.aggregate.AggregateOperations.toBagsByTag) GroupAggregateBuilder(com.hazelcast.jet.pipeline.GroupAggregateBuilder) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Broker(datamodel.Broker) Pipeline(com.hazelcast.jet.pipeline.Pipeline)

Aggregations

Tag (com.hazelcast.jet.datamodel.Tag)20 ItemsByTag (com.hazelcast.jet.datamodel.ItemsByTag)15 Entry (java.util.Map.Entry)14 LongAccumulator (com.hazelcast.jet.accumulator.LongAccumulator)13 AggregateOperation (com.hazelcast.jet.aggregate.AggregateOperation)13 Tuple2 (com.hazelcast.jet.datamodel.Tuple2)13 Tuple3 (com.hazelcast.jet.datamodel.Tuple3)13 List (java.util.List)13 Assert.assertEquals (org.junit.Assert.assertEquals)10 Test (org.junit.Test)10 Util.entry (com.hazelcast.jet.Util.entry)9 Tuple2.tuple2 (com.hazelcast.jet.datamodel.Tuple2.tuple2)8 Pipeline (com.hazelcast.jet.pipeline.Pipeline)8 Collections.singletonList (java.util.Collections.singletonList)8 Category (org.junit.experimental.categories.Category)8 AggregateOperation1 (com.hazelcast.jet.aggregate.AggregateOperation1)7 AggregateOperations.coAggregateOperationBuilder (com.hazelcast.jet.aggregate.AggregateOperations.coAggregateOperationBuilder)7 AggregateOperations.counting (com.hazelcast.jet.aggregate.AggregateOperations.counting)7 CoAggregateOperationBuilder (com.hazelcast.jet.aggregate.CoAggregateOperationBuilder)7 AggregateOperations (com.hazelcast.jet.aggregate.AggregateOperations)6