Search in sources :

Example 1 with StreamStage

use of com.hazelcast.jet.pipeline.StreamStage 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 2 with StreamStage

use of com.hazelcast.jet.pipeline.StreamStage in project hazelcast-jet by hazelcast.

the class GrAggBuilder method buildStream.

@SuppressWarnings("unchecked")
public <A, R, OUT> StreamStage<OUT> buildStream(@Nonnull AggregateOperation<A, ? extends R> aggrOp, @Nonnull KeyedWindowResultFunction<? super K, ? super R, OUT> mapToOutputFn) {
    List<Transform> upstreamTransforms = upstreamStages.stream().map(s -> s.transform).collect(toList());
    JetEventFunctionAdapter fnAdapter = ADAPT_TO_JET_EVENT;
    // Avoided Stream API here due to static typing issues
    List<DistributedFunction<?, ? extends K>> adaptedKeyFns = new ArrayList<>();
    for (DistributedFunction keyFn : keyFns) {
        adaptedKeyFns.add(adaptKeyFn(keyFn));
    }
    Transform transform = new WindowGroupTransform<K, A, R, JetEvent<OUT>>(upstreamTransforms, wDef, adaptedKeyFns, adaptAggregateOperation(aggrOp), fnAdapter.adaptKeyedWindowResultFn(mapToOutputFn));
    pipelineImpl.connect(upstreamTransforms, transform);
    return new StreamStageImpl<>(transform, fnAdapter, pipelineImpl);
}
Also used : WindowGroupTransform(com.hazelcast.jet.impl.pipeline.transform.WindowGroupTransform) DistributedBiFunction(com.hazelcast.jet.function.DistributedBiFunction) ArrayList(java.util.ArrayList) BatchStage(com.hazelcast.jet.pipeline.BatchStage) Tag.tag(com.hazelcast.jet.datamodel.Tag.tag) KeyedWindowResultFunction(com.hazelcast.jet.function.KeyedWindowResultFunction) AggregateOperation(com.hazelcast.jet.aggregate.AggregateOperation) WindowGroupAggregateBuilder(com.hazelcast.jet.pipeline.WindowGroupAggregateBuilder) StageWithGroupingAndWindow(com.hazelcast.jet.pipeline.StageWithGroupingAndWindow) DistributedFunction(com.hazelcast.jet.function.DistributedFunction) GroupTransform(com.hazelcast.jet.impl.pipeline.transform.GroupTransform) Nonnull(javax.annotation.Nonnull) StreamStage(com.hazelcast.jet.pipeline.StreamStage) WindowDefinition(com.hazelcast.jet.pipeline.WindowDefinition) StageWithGrouping(com.hazelcast.jet.pipeline.StageWithGrouping) JetEventFunctionAdapter.adaptAggregateOperation(com.hazelcast.jet.impl.pipeline.JetEventFunctionAdapter.adaptAggregateOperation) Tag(com.hazelcast.jet.datamodel.Tag) Transform(com.hazelcast.jet.impl.pipeline.transform.Transform) ADAPT_TO_JET_EVENT(com.hazelcast.jet.impl.pipeline.ComputeStageImplBase.ADAPT_TO_JET_EVENT) List(java.util.List) ComputeStageImplBase.ensureJetEvents(com.hazelcast.jet.impl.pipeline.ComputeStageImplBase.ensureJetEvents) Collectors.toList(java.util.stream.Collectors.toList) JetEventFunctionAdapter.adaptKeyFn(com.hazelcast.jet.impl.pipeline.JetEventFunctionAdapter.adaptKeyFn) StreamStageWithGrouping(com.hazelcast.jet.pipeline.StreamStageWithGrouping) GroupAggregateBuilder(com.hazelcast.jet.pipeline.GroupAggregateBuilder) ArrayList(java.util.ArrayList) WindowGroupTransform(com.hazelcast.jet.impl.pipeline.transform.WindowGroupTransform) GroupTransform(com.hazelcast.jet.impl.pipeline.transform.GroupTransform) Transform(com.hazelcast.jet.impl.pipeline.transform.Transform) WindowGroupTransform(com.hazelcast.jet.impl.pipeline.transform.WindowGroupTransform) DistributedFunction(com.hazelcast.jet.function.DistributedFunction)

Example 3 with StreamStage

use of com.hazelcast.jet.pipeline.StreamStage in project hazelcast by hazelcast.

the class PythonServiceTest method streamStage_mapUsingPython_withCleanupScript.

@Test
@Category(NightlyTest.class)
public void streamStage_mapUsingPython_withCleanupScript() throws Exception {
    // Given
    String baseDirStr = baseDir.toString();
    String outcomeFilename = "cleanup_outcome.txt";
    installFileToBaseDir(String.format("echo 'Cleanup script running'%n" + "echo 'cleanup.sh' executed > %s/%s%n", baseDirStr, outcomeFilename), "cleanup.sh");
    PythonServiceConfig cfg = new PythonServiceConfig().setBaseDir(baseDirStr).setHandlerModule("echo").setHandlerFunction("handle");
    List<String> items = singletonList("item-0");
    Pipeline p = Pipeline.create();
    StreamStage<String> stage = p.readFrom(TestSources.items(items)).addTimestamps(x -> 0, 0);
    // When
    StreamStage<String> mapped = stage.apply(mapUsingPython(cfg)).setLocalParallelism(2);
    // Then
    mapped.writeTo(AssertionSinks.assertAnyOrder("Python didn't map the items correctly", items.stream().map(i -> "echo-" + i).collect(toList())));
    instance().getJet().newJob(p).join();
    assertTrue("Cleanup script didn't run", new File(baseDir, outcomeFilename).isFile());
}
Also used : AssertionSinks(com.hazelcast.jet.pipeline.test.AssertionSinks) IntStream(java.util.stream.IntStream) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) BeforeClass(org.junit.BeforeClass) QuickTest(com.hazelcast.test.annotation.QuickTest) PythonTransforms.mapUsingPython(com.hazelcast.jet.python.PythonTransforms.mapUsingPython) Collections.singletonList(java.util.Collections.singletonList) BatchStage(com.hazelcast.jet.pipeline.BatchStage) ByteArrayInputStream(java.io.ByteArrayInputStream) After(org.junit.After) Assert.fail(org.junit.Assert.fail) PythonTransforms.mapUsingPythonBatch(com.hazelcast.jet.python.PythonTransforms.mapUsingPythonBatch) Before(org.junit.Before) SimpleTestInClusterSupport(com.hazelcast.jet.SimpleTestInClusterSupport) Config(com.hazelcast.config.Config) StreamStage(com.hazelcast.jet.pipeline.StreamStage) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Sinks(com.hazelcast.jet.pipeline.Sinks) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) Category(org.junit.experimental.categories.Category) File(java.io.File) TestSources(com.hazelcast.jet.pipeline.test.TestSources) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) InputStream(java.io.InputStream) File(java.io.File) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Category(org.junit.experimental.categories.Category) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 4 with StreamStage

use of com.hazelcast.jet.pipeline.StreamStage in project hazelcast by hazelcast.

the class PythonServiceTest method streamStage_mapUsingPython_withInitScript.

@Test
@Category(NightlyTest.class)
public void streamStage_mapUsingPython_withInitScript() throws Exception {
    // Given
    String baseDirStr = baseDir.toString();
    String outcomeFilename = "init_outcome.txt";
    installFileToBaseDir(String.format("echo 'Init script running'%n" + "echo 'init.sh' executed > %s/%s%n", baseDirStr, outcomeFilename), "init.sh");
    PythonServiceConfig cfg = new PythonServiceConfig().setBaseDir(baseDirStr).setHandlerModule("echo").setHandlerFunction("handle");
    List<String> items = singletonList("item-0");
    Pipeline p = Pipeline.create();
    StreamStage<String> stage = p.readFrom(TestSources.items(items)).addTimestamps(x -> 0, 0);
    // When
    StreamStage<String> mapped = stage.apply(mapUsingPython(cfg)).setLocalParallelism(2);
    // Then
    mapped.writeTo(AssertionSinks.assertAnyOrder("Python didn't map the items correctly", items.stream().map(i -> "echo-" + i).collect(toList())));
    instance().getJet().newJob(p).join();
    assertTrue("Init script didn't run", new File(baseDir, outcomeFilename).isFile());
}
Also used : AssertionSinks(com.hazelcast.jet.pipeline.test.AssertionSinks) IntStream(java.util.stream.IntStream) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) BeforeClass(org.junit.BeforeClass) QuickTest(com.hazelcast.test.annotation.QuickTest) PythonTransforms.mapUsingPython(com.hazelcast.jet.python.PythonTransforms.mapUsingPython) Collections.singletonList(java.util.Collections.singletonList) BatchStage(com.hazelcast.jet.pipeline.BatchStage) ByteArrayInputStream(java.io.ByteArrayInputStream) After(org.junit.After) Assert.fail(org.junit.Assert.fail) PythonTransforms.mapUsingPythonBatch(com.hazelcast.jet.python.PythonTransforms.mapUsingPythonBatch) Before(org.junit.Before) SimpleTestInClusterSupport(com.hazelcast.jet.SimpleTestInClusterSupport) Config(com.hazelcast.config.Config) StreamStage(com.hazelcast.jet.pipeline.StreamStage) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Sinks(com.hazelcast.jet.pipeline.Sinks) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) Category(org.junit.experimental.categories.Category) File(java.io.File) TestSources(com.hazelcast.jet.pipeline.test.TestSources) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) InputStream(java.io.InputStream) File(java.io.File) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Category(org.junit.experimental.categories.Category) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 5 with StreamStage

use of com.hazelcast.jet.pipeline.StreamStage in project hazelcast by hazelcast.

the class PythonServiceTest method streamStage_mapUsingPython_withBaseDir.

@Test
@Category(NightlyTest.class)
public void streamStage_mapUsingPython_withBaseDir() {
    // Given
    PythonServiceConfig cfg = new PythonServiceConfig().setBaseDir(baseDir.toString()).setHandlerModule("echo").setHandlerFunction("handle");
    List<String> items = IntStream.range(0, ITEM_COUNT).mapToObj(Integer::toString).collect(toList());
    Pipeline p = Pipeline.create();
    StreamStage<String> stage = p.readFrom(TestSources.items(items)).addTimestamps(x -> 0, 0);
    // When
    StreamStage<String> mapped = stage.apply(mapUsingPython(cfg)).setLocalParallelism(2);
    // Then
    mapped.writeTo(AssertionSinks.assertAnyOrder("Python didn't map the items correctly", items.stream().map(i -> "echo-" + i).collect(toList())));
    instance().getJet().newJob(p).join();
}
Also used : AssertionSinks(com.hazelcast.jet.pipeline.test.AssertionSinks) IntStream(java.util.stream.IntStream) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) BeforeClass(org.junit.BeforeClass) QuickTest(com.hazelcast.test.annotation.QuickTest) PythonTransforms.mapUsingPython(com.hazelcast.jet.python.PythonTransforms.mapUsingPython) Collections.singletonList(java.util.Collections.singletonList) BatchStage(com.hazelcast.jet.pipeline.BatchStage) ByteArrayInputStream(java.io.ByteArrayInputStream) After(org.junit.After) Assert.fail(org.junit.Assert.fail) PythonTransforms.mapUsingPythonBatch(com.hazelcast.jet.python.PythonTransforms.mapUsingPythonBatch) Before(org.junit.Before) SimpleTestInClusterSupport(com.hazelcast.jet.SimpleTestInClusterSupport) Config(com.hazelcast.config.Config) StreamStage(com.hazelcast.jet.pipeline.StreamStage) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Sinks(com.hazelcast.jet.pipeline.Sinks) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) Category(org.junit.experimental.categories.Category) File(java.io.File) TestSources(com.hazelcast.jet.pipeline.test.TestSources) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) InputStream(java.io.InputStream) Pipeline(com.hazelcast.jet.pipeline.Pipeline) Category(org.junit.experimental.categories.Category) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Aggregations

StreamStage (com.hazelcast.jet.pipeline.StreamStage)8 Pipeline (com.hazelcast.jet.pipeline.Pipeline)7 Sinks (com.hazelcast.jet.pipeline.Sinks)7 List (java.util.List)7 Test (org.junit.Test)7 Category (org.junit.experimental.categories.Category)7 BatchStage (com.hazelcast.jet.pipeline.BatchStage)6 NightlyTest (com.hazelcast.test.annotation.NightlyTest)6 Config (com.hazelcast.config.Config)5 SimpleTestInClusterSupport (com.hazelcast.jet.SimpleTestInClusterSupport)5 AssertionSinks (com.hazelcast.jet.pipeline.test.AssertionSinks)5 TestSources (com.hazelcast.jet.pipeline.test.TestSources)5 PythonTransforms.mapUsingPython (com.hazelcast.jet.python.PythonTransforms.mapUsingPython)5 PythonTransforms.mapUsingPythonBatch (com.hazelcast.jet.python.PythonTransforms.mapUsingPythonBatch)5 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)5 QuickTest (com.hazelcast.test.annotation.QuickTest)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 File (java.io.File)5 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5