Search in sources :

Example 16 with FunctionEx

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

the class SourceBuilderTest method stream_socketSource_withTimestamps_andLateness.

@Test
public void stream_socketSource_withTimestamps_andLateness() throws IOException {
    // Given
    try (ServerSocket serverSocket = new ServerSocket(0)) {
        startServer(serverSocket);
        // When
        int localPort = serverSocket.getLocalPort();
        FunctionEx<String, Long> timestampFn = line -> Long.valueOf(line.substring(LINE_PREFIX.length()));
        int lateness = 10;
        long lastExpectedTs = itemCount - lateness;
        StreamSource<String> socketSource = SourceBuilder.timestampedStream("socket-source-with-timestamps", ctx -> socketReader(localPort)).<String>fillBufferFn((in, buf) -> {
            String line = in.readLine();
            if (line != null) {
                long ts = timestampFn.apply(line);
                buf.add(line, ts);
                if (ts >= lastExpectedTs) {
                    System.out.println(line);
                }
            }
        }).destroyFn(BufferedReader::close).build();
        // Then
        Pipeline p = Pipeline.create();
        p.readFrom(socketSource).withNativeTimestamps(lateness).window(tumbling(1)).aggregate(AggregateOperations.counting()).writeTo(sinkList());
        hz().getJet().newJob(p);
        List<WindowResult<Long>> expected = LongStream.range(1, itemCount - lateness).mapToObj(i -> new WindowResult<>(i - 1, i, 1L)).collect(toList());
        assertTrueEventually(() -> assertEquals(expected, new ArrayList<>(sinkList)), 10);
    }
}
Also used : IntStream(java.util.stream.IntStream) Socket(java.net.Socket) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) ArrayList(java.util.ArrayList) ServerSocket(java.net.ServerSocket) WindowDefinition.tumbling(com.hazelcast.jet.pipeline.WindowDefinition.tumbling) Map(java.util.Map) UuidUtil(com.hazelcast.internal.util.UuidUtil) OutputStreamWriter(java.io.OutputStreamWriter) JobStatus(com.hazelcast.jet.core.JobStatus) Job(com.hazelcast.jet.Job) IList(com.hazelcast.collection.IList) JobRepository(com.hazelcast.jet.impl.JobRepository) PrintWriter(java.io.PrintWriter) FunctionEx(com.hazelcast.function.FunctionEx) LongStream(java.util.stream.LongStream) Iterator(java.util.Iterator) EXACTLY_ONCE(com.hazelcast.jet.config.ProcessingGuarantee.EXACTLY_ONCE) JobConfig(com.hazelcast.jet.config.JobConfig) FileWriter(java.io.FileWriter) AggregateOperations(com.hazelcast.jet.aggregate.AggregateOperations) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Serializable(java.io.Serializable) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ToLongFunctionEx(com.hazelcast.function.ToLongFunctionEx) Assert.assertFalse(org.junit.Assert.assertFalse) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Assert.assertEquals(org.junit.Assert.assertEquals) WindowResult(com.hazelcast.jet.datamodel.WindowResult) ArrayList(java.util.ArrayList) ServerSocket(java.net.ServerSocket) WindowResult(com.hazelcast.jet.datamodel.WindowResult) Test(org.junit.Test)

Example 17 with FunctionEx

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

the class ManagedContextTest method testServices.

private void testServices(FunctionEx<ProcessorSupplier.Context, ? extends AnotherTestServiceContext> serviceSupplier) {
    // Given
    ServiceFactory<?, ? extends AnotherTestServiceContext> serviceFactory = ServiceFactories.sharedService(serviceSupplier);
    Pipeline p = Pipeline.create();
    p.readFrom(TestSources.items("item")).mapUsingService(serviceFactory, (c, item) -> item + c.injectedValue).writeTo(assertAnyOrder(singletonList("item" + INJECTED_VALUE)));
    // When
    hz.getJet().newJob(p).join();
}
Also used : Config(com.hazelcast.config.Config) HazelcastInstance(com.hazelcast.core.HazelcastInstance) FunctionEx(com.hazelcast.function.FunctionEx) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) ManagedContext(com.hazelcast.core.ManagedContext) BatchSource(com.hazelcast.jet.pipeline.BatchSource) Pipeline(com.hazelcast.jet.pipeline.Pipeline) QuickTest(com.hazelcast.test.annotation.QuickTest) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) Category(org.junit.experimental.categories.Category) SupplierEx(com.hazelcast.function.SupplierEx) Collections.singletonList(java.util.Collections.singletonList) Sources(com.hazelcast.jet.pipeline.Sources) AssertionSinks.assertAnyOrder(com.hazelcast.jet.pipeline.test.AssertionSinks.assertAnyOrder) TestSources(com.hazelcast.jet.pipeline.test.TestSources) HazelcastParallelClassRunner(com.hazelcast.test.HazelcastParallelClassRunner) ServiceFactory(com.hazelcast.jet.pipeline.ServiceFactory) SinkBuilder(com.hazelcast.jet.pipeline.SinkBuilder) SourceBuilder(com.hazelcast.jet.pipeline.SourceBuilder) ServiceFactories(com.hazelcast.jet.pipeline.ServiceFactories) Sink(com.hazelcast.jet.pipeline.Sink) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) Pipeline(com.hazelcast.jet.pipeline.Pipeline)

Example 18 with FunctionEx

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

the class RebalanceBatchStageTest method when_peekAndRebalanceAndMap_then_dagEdgeDistributed.

@Test
public void when_peekAndRebalanceAndMap_then_dagEdgeDistributed() {
    // Given
    List<Integer> input = sequence(itemCount);
    BatchStage<Integer> srcStage = batchStageFromList(input);
    FunctionEx<Integer, String> formatFn = i -> String.format("%04d-string", i);
    // When
    BatchStage<String> mapped = srcStage.peek().rebalance().map(formatFn);
    // Then
    mapped.writeTo(sink);
    DAG dag = p.toDag();
    Edge srcToMap = dag.getInboundEdges("map").get(0);
    assertTrue("Rebalancing should make the edge distributed", srcToMap.isDistributed());
    assertNull("Didn't rebalance by key, the edge must not be partitioned", srcToMap.getPartitioner());
    execute();
    assertEquals(streamToString(input.stream(), formatFn), streamToString(sinkStreamOf(String.class), identity()));
}
Also used : Spliterators.spliteratorUnknownSize(java.util.Spliterators.spliteratorUnknownSize) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AggregateOperations.coAggregateOperationBuilder(com.hazelcast.jet.aggregate.AggregateOperations.coAggregateOperationBuilder) Collections.singletonList(java.util.Collections.singletonList) AggregateOperation(com.hazelcast.jet.aggregate.AggregateOperation) Map(java.util.Map) DAG(com.hazelcast.jet.core.DAG) Collector(java.util.stream.Collector) FunctionEx(com.hazelcast.function.FunctionEx) Tag(com.hazelcast.jet.datamodel.Tag) AggregateOperations(com.hazelcast.jet.aggregate.AggregateOperations) Category(org.junit.experimental.categories.Category) Tuple3.tuple3(com.hazelcast.jet.datamodel.Tuple3.tuple3) List(java.util.List) Stream(java.util.stream.Stream) StreamSupport.stream(java.util.stream.StreamSupport.stream) ItemsByTag(com.hazelcast.jet.datamodel.ItemsByTag) Entry(java.util.Map.Entry) Function.identity(java.util.function.Function.identity) IntStream(java.util.stream.IntStream) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Function(java.util.function.Function) FORMAT_FN_3(com.hazelcast.jet.pipeline.BatchAggregateTest.FORMAT_FN_3) JoinClause.joinMapEntries(com.hazelcast.jet.pipeline.JoinClause.joinMapEntries) ArrayList(java.util.ArrayList) JetException(com.hazelcast.jet.JetException) JetAssert.assertFalse(com.hazelcast.jet.core.test.JetAssert.assertFalse) FORMAT_FN(com.hazelcast.jet.pipeline.BatchAggregateTest.FORMAT_FN) AssertionSinks.assertAnyOrder(com.hazelcast.jet.pipeline.test.AssertionSinks.assertAnyOrder) FORMAT_FN_2(com.hazelcast.jet.pipeline.BatchAggregateTest.FORMAT_FN_2) Collectors.summingLong(java.util.stream.Collectors.summingLong) Util.entry(com.hazelcast.jet.Util.entry) Edge(com.hazelcast.jet.core.Edge) Nonnull(javax.annotation.Nonnull) Tuple2(com.hazelcast.jet.datamodel.Tuple2) Tuple3(com.hazelcast.jet.datamodel.Tuple3) IList(com.hazelcast.collection.IList) Iterator(java.util.Iterator) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) AggregateOperation1(com.hazelcast.jet.aggregate.AggregateOperation1) CoAggregateOperationBuilder(com.hazelcast.jet.aggregate.CoAggregateOperationBuilder) TestSources(com.hazelcast.jet.pipeline.test.TestSources) Collectors.toList(java.util.stream.Collectors.toList) Tuple2.tuple2(com.hazelcast.jet.datamodel.Tuple2.tuple2) Assert.assertNull(org.junit.Assert.assertNull) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) Functions.wholeItem(com.hazelcast.function.Functions.wholeItem) Util(com.hazelcast.jet.Util) Assert.assertEquals(org.junit.Assert.assertEquals) DAG(com.hazelcast.jet.core.DAG) Edge(com.hazelcast.jet.core.Edge) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 19 with FunctionEx

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

the class RebalanceBatchStageTest method when_rebalanceAndGroupAggregateBuilderWithComplexAggrOp_then_singleStageAggregation.

@Test
public void when_rebalanceAndGroupAggregateBuilderWithComplexAggrOp_then_singleStageAggregation() {
    // Given
    FunctionEx<Integer, Integer> keyFn = i -> i % 10;
    Iterator<Integer> sequence = IntStream.iterate(0, i -> i + 1).boxed().iterator();
    List<Integer> input0 = streamFromIterator(sequence).limit(itemCount).collect(toList());
    List<Integer> input1 = streamFromIterator(sequence).limit(itemCount).collect(toList());
    List<Integer> input2 = streamFromIterator(sequence).limit(itemCount).collect(toList());
    BatchStageWithKey<Integer, Integer> stage0 = batchStageFromList(input0).groupingKey(keyFn);
    BatchStageWithKey<Integer, Integer> stage1 = batchStageFromList(input1).groupingKey(keyFn);
    BatchStageWithKey<Integer, Integer> stage2Rebalanced = batchStageFromList(input2).rebalance().groupingKey(keyFn);
    // When
    GroupAggregateBuilder1<Integer, Integer> b = stage0.aggregateBuilder();
    Tag<Integer> tag0_in = b.tag0();
    Tag<Integer> tag1_in = b.add(stage1);
    Tag<Integer> tag2_in = b.add(stage2Rebalanced);
    CoAggregateOperationBuilder agb = coAggregateOperationBuilder();
    Tag<Long> tag0 = agb.add(tag0_in, SUMMING);
    Tag<Long> tag1 = agb.add(tag1_in, SUMMING);
    Tag<Long> tag2 = agb.add(tag2_in, SUMMING);
    AggregateOperation<Object[], ItemsByTag> aggrOp = agb.build();
    BatchStage<Entry<Integer, ItemsByTag>> aggregated = b.build(aggrOp);
    // Then
    aggregated.writeTo(sink);
    assertSingleStageAggregation();
    execute();
    Collector<Integer, ?, Map<Integer, Long>> groupAndSum = groupingBy(keyFn, summingLong(i -> i));
    Map<Integer, Long> expectedMap0 = input0.stream().collect(groupAndSum);
    Map<Integer, Long> expectedMap1 = input1.stream().collect(groupAndSum);
    Map<Integer, Long> expectedMap2 = input2.stream().collect(groupAndSum);
    assertEquals(streamToString(expectedMap0.entrySet().stream(), e -> FORMAT_FN_3.apply(e.getKey(), tuple3(e.getValue(), expectedMap1.get(e.getKey()), expectedMap2.get(e.getKey())))), streamToString(this.<Integer, ItemsByTag>sinkStreamOfEntry(), e -> FORMAT_FN_3.apply(e.getKey(), tuple3(e.getValue().get(tag0), e.getValue().get(tag1), e.getValue().get(tag2)))));
}
Also used : Spliterators.spliteratorUnknownSize(java.util.Spliterators.spliteratorUnknownSize) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AggregateOperations.coAggregateOperationBuilder(com.hazelcast.jet.aggregate.AggregateOperations.coAggregateOperationBuilder) Collections.singletonList(java.util.Collections.singletonList) AggregateOperation(com.hazelcast.jet.aggregate.AggregateOperation) Map(java.util.Map) DAG(com.hazelcast.jet.core.DAG) Collector(java.util.stream.Collector) FunctionEx(com.hazelcast.function.FunctionEx) Tag(com.hazelcast.jet.datamodel.Tag) AggregateOperations(com.hazelcast.jet.aggregate.AggregateOperations) Category(org.junit.experimental.categories.Category) Tuple3.tuple3(com.hazelcast.jet.datamodel.Tuple3.tuple3) List(java.util.List) Stream(java.util.stream.Stream) StreamSupport.stream(java.util.stream.StreamSupport.stream) ItemsByTag(com.hazelcast.jet.datamodel.ItemsByTag) Entry(java.util.Map.Entry) Function.identity(java.util.function.Function.identity) IntStream(java.util.stream.IntStream) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Function(java.util.function.Function) FORMAT_FN_3(com.hazelcast.jet.pipeline.BatchAggregateTest.FORMAT_FN_3) JoinClause.joinMapEntries(com.hazelcast.jet.pipeline.JoinClause.joinMapEntries) ArrayList(java.util.ArrayList) JetException(com.hazelcast.jet.JetException) JetAssert.assertFalse(com.hazelcast.jet.core.test.JetAssert.assertFalse) FORMAT_FN(com.hazelcast.jet.pipeline.BatchAggregateTest.FORMAT_FN) AssertionSinks.assertAnyOrder(com.hazelcast.jet.pipeline.test.AssertionSinks.assertAnyOrder) FORMAT_FN_2(com.hazelcast.jet.pipeline.BatchAggregateTest.FORMAT_FN_2) Collectors.summingLong(java.util.stream.Collectors.summingLong) Util.entry(com.hazelcast.jet.Util.entry) Edge(com.hazelcast.jet.core.Edge) Nonnull(javax.annotation.Nonnull) Tuple2(com.hazelcast.jet.datamodel.Tuple2) Tuple3(com.hazelcast.jet.datamodel.Tuple3) IList(com.hazelcast.collection.IList) Iterator(java.util.Iterator) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) AggregateOperation1(com.hazelcast.jet.aggregate.AggregateOperation1) CoAggregateOperationBuilder(com.hazelcast.jet.aggregate.CoAggregateOperationBuilder) TestSources(com.hazelcast.jet.pipeline.test.TestSources) Collectors.toList(java.util.stream.Collectors.toList) Tuple2.tuple2(com.hazelcast.jet.datamodel.Tuple2.tuple2) Assert.assertNull(org.junit.Assert.assertNull) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) Functions.wholeItem(com.hazelcast.function.Functions.wholeItem) Util(com.hazelcast.jet.Util) Assert.assertEquals(org.junit.Assert.assertEquals) ItemsByTag(com.hazelcast.jet.datamodel.ItemsByTag) Entry(java.util.Map.Entry) CoAggregateOperationBuilder(com.hazelcast.jet.aggregate.CoAggregateOperationBuilder) Collectors.summingLong(java.util.stream.Collectors.summingLong) Map(java.util.Map) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 20 with FunctionEx

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

the class RebalanceStreamStageTest method when_peekAndRebalanceByKeyAndMap_then_dagEdgePartitionedDistributed.

@Test
public void when_peekAndRebalanceByKeyAndMap_then_dagEdgePartitionedDistributed() {
    // Given
    List<Integer> input = sequence(itemCount);
    StreamStage<Integer> srcStage = streamStageFromList(input);
    FunctionEx<Integer, String> formatFn = i -> String.format("%04d-string", i);
    // When
    StreamStage<String> mapped = srcStage.peek().rebalance(i -> i).map(formatFn);
    // Then
    mapped.writeTo(sink);
    DAG dag = p.toDag();
    Edge srcToMap = dag.getInboundEdges("map").get(0);
    assertTrue("Rebalancing should make the edge distributed", srcToMap.isDistributed());
    assertNotNull("Rebalanced by key, the edge must be partitioned", srcToMap.getPartitioner());
}
Also used : FunctionEx(com.hazelcast.function.FunctionEx) KeyedWindowResult(com.hazelcast.jet.datamodel.KeyedWindowResult) Assert.assertNotNull(org.junit.Assert.assertNotNull) AggregateOperations(com.hazelcast.jet.aggregate.AggregateOperations) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) AggregateOperation1(com.hazelcast.jet.aggregate.AggregateOperation1) JetException(com.hazelcast.jet.JetException) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) WindowDefinition.tumbling(com.hazelcast.jet.pipeline.WindowDefinition.tumbling) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) Function.identity(java.util.function.Function.identity) DAG(com.hazelcast.jet.core.DAG) Edge(com.hazelcast.jet.core.Edge) Assert.assertEquals(org.junit.Assert.assertEquals) WindowResult(com.hazelcast.jet.datamodel.WindowResult) DAG(com.hazelcast.jet.core.DAG) Edge(com.hazelcast.jet.core.Edge) Test(org.junit.Test)

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