Search in sources :

Example 1 with WindowResult

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

the class WindowGroupTransform_IntegrationTest method testSession_groupingFirst.

@Test
public void testSession_groupingFirst() {
    Pipeline p = Pipeline.create();
    p.drawFrom(Sources.<Long, String>mapJournal("source", JournalInitialPosition.START_FROM_OLDEST)).addTimestamps(Entry::getKey, 0).groupingKey(entry -> entry.getValue().charAt(0)).window(WindowDefinition.session(2)).aggregate(toSet(), WindowResult::new).drainTo(Sinks.list("sink"));
    testSession(p);
}
Also used : JetInstance(com.hazelcast.jet.JetInstance) GroupProperty(com.hazelcast.spi.properties.GroupProperty) RunWith(org.junit.runner.RunWith) EventJournalConfig(com.hazelcast.config.EventJournalConfig) TestUtil.set(com.hazelcast.jet.core.TestUtil.set) HashSet(java.util.HashSet) 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) WindowGroupAggregateBuilder(com.hazelcast.jet.pipeline.WindowGroupAggregateBuilder) StageWithGroupingAndWindow(com.hazelcast.jet.pipeline.StageWithGroupingAndWindow) IList(com.hazelcast.core.IList) Before(org.junit.Before) AggregateOperations.toThreeBags(com.hazelcast.jet.aggregate.AggregateOperations.toThreeBags) JetConfig(com.hazelcast.jet.config.JetConfig) 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) Sources(com.hazelcast.jet.pipeline.Sources) IMap(com.hazelcast.core.IMap) JournalInitialPosition(com.hazelcast.jet.pipeline.JournalInitialPosition) StreamStageWithGrouping(com.hazelcast.jet.pipeline.StreamStageWithGrouping) HazelcastParallelClassRunner(com.hazelcast.test.HazelcastParallelClassRunner) Entry(java.util.Map.Entry) TwoBags(com.hazelcast.jet.datamodel.TwoBags) Assert.assertEquals(org.junit.Assert.assertEquals) TimestampedEntry(com.hazelcast.jet.datamodel.TimestampedEntry) WindowResult(com.hazelcast.jet.datamodel.WindowResult) TestSupport.listToString(com.hazelcast.jet.core.test.TestSupport.listToString) Entry(java.util.Map.Entry) TimestampedEntry(com.hazelcast.jet.datamodel.TimestampedEntry) 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 WindowResult

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

the class SessionWindowPTest method when_batchProcessing_then_flushEverything.

@Test
public void when_batchProcessing_then_flushEverything() {
    List<Object> inbox = new ArrayList<>();
    // Given
    inbox.addAll(eventsWithKey("a"));
    // This watermark will cause the first session to be emitted, but not the second.
    // The second session will be emitted in complete()
    inbox.add(new Watermark(25));
    verifyProcessor(supplier).input(inbox).expectOutput(asList(new WindowResult(1, 22, "a", 3), new Watermark(25), new WindowResult(30, 50, "a", 3)));
}
Also used : ArrayList(java.util.ArrayList) WindowResult(com.hazelcast.jet.datamodel.WindowResult) Watermark(com.hazelcast.jet.core.Watermark) Test(org.junit.Test)

Example 3 with WindowResult

use of com.hazelcast.jet.datamodel.WindowResult 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 4 with WindowResult

use of com.hazelcast.jet.datamodel.WindowResult in project hazelcast by hazelcast.

the class SourceBuilderTest method stream_distributed_socketSource_withTimestamps.

@Test
public void stream_distributed_socketSource_withTimestamps() throws IOException {
    // Given
    try (ServerSocket serverSocket = new ServerSocket(0)) {
        startServer(serverSocket);
        // When
        int localPort = serverSocket.getLocalPort();
        ToLongFunctionEx<String> timestampFn = line -> Long.valueOf(line.substring(LINE_PREFIX.length()));
        BatchSource<String> socketSource = SourceBuilder.batch("socket-source-with-timestamps", ctx -> socketReader(localPort)).<String>fillBufferFn((in, buf) -> {
            String line = in.readLine();
            if (line != null) {
                buf.add(line);
            } else {
                buf.close();
            }
        }).destroyFn(BufferedReader::close).distributed(PREFERRED_LOCAL_PARALLELISM).build();
        // Then
        Pipeline p = Pipeline.create();
        p.readFrom(socketSource).addTimestamps(timestampFn, 1000).window(tumbling(1)).aggregate(AggregateOperations.counting()).writeTo(sinkList());
        hz().getJet().newJob(p).join();
        List<WindowResult<Long>> expected = LongStream.range(1, itemCount + 1).mapToObj(i -> new WindowResult<>(i - 1, i, (long) PREFERRED_LOCAL_PARALLELISM * MEMBER_COUNT)).collect(toList());
        assertEquals(expected, new ArrayList<>(sinkList));
    }
}
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) BufferedReader(java.io.BufferedReader) ServerSocket(java.net.ServerSocket) WindowResult(com.hazelcast.jet.datamodel.WindowResult) Test(org.junit.Test)

Example 5 with WindowResult

use of com.hazelcast.jet.datamodel.WindowResult in project hazelcast by hazelcast.

the class SourceBuilderTest method stream_socketSource_withTimestamps.

@Test
public void stream_socketSource_withTimestamps() throws IOException {
    // Given
    try (ServerSocket serverSocket = new ServerSocket(0)) {
        startServer(serverSocket);
        // When
        int localPort = serverSocket.getLocalPort();
        ToLongFunctionEx<String> timestampFn = line -> Long.valueOf(line.substring(LINE_PREFIX.length()));
        BatchSource<String> socketSource = SourceBuilder.batch("socket-source-with-timestamps", ctx -> socketReader(localPort)).<String>fillBufferFn((in, buf) -> {
            String line = in.readLine();
            if (line != null) {
                buf.add(line);
            } else {
                buf.close();
            }
        }).destroyFn(BufferedReader::close).build();
        // Then
        Pipeline p = Pipeline.create();
        p.readFrom(socketSource).addTimestamps(timestampFn, 0).window(tumbling(1)).aggregate(AggregateOperations.counting()).writeTo(sinkList());
        hz().getJet().newJob(p).join();
        List<WindowResult<Long>> expected = LongStream.range(1, itemCount + 1).mapToObj(i -> new WindowResult<>(i - 1, i, 1L)).collect(toList());
        assertEquals(expected, new ArrayList<>(sinkList));
    }
}
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) ServerSocket(java.net.ServerSocket) WindowResult(com.hazelcast.jet.datamodel.WindowResult) Test(org.junit.Test)

Aggregations

WindowResult (com.hazelcast.jet.datamodel.WindowResult)13 Test (org.junit.Test)12 Job (com.hazelcast.jet.Job)9 Assert.assertEquals (org.junit.Assert.assertEquals)7 JobConfig (com.hazelcast.jet.config.JobConfig)6 WindowDefinition.tumbling (com.hazelcast.jet.pipeline.WindowDefinition.tumbling)5 IList (com.hazelcast.collection.IList)4 UuidUtil (com.hazelcast.internal.util.UuidUtil)4 AggregateOperations (com.hazelcast.jet.aggregate.AggregateOperations)4 EXACTLY_ONCE (com.hazelcast.jet.config.ProcessingGuarantee.EXACTLY_ONCE)4 JobStatus (com.hazelcast.jet.core.JobStatus)4 ArrayList (java.util.ArrayList)4 Category (org.junit.experimental.categories.Category)4 FunctionEx (com.hazelcast.function.FunctionEx)3 ToLongFunctionEx (com.hazelcast.function.ToLongFunctionEx)3 JobRepository (com.hazelcast.jet.impl.JobRepository)3 BufferedReader (java.io.BufferedReader)3 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 FileReader (java.io.FileReader)3