Search in sources :

Example 1 with Window

use of org.apache.samza.operators.windows.Window in project samza by apache.

the class TestMessageStreamImpl method testWindowWithRelaxedTypes.

@Test
public void testWindowWithRelaxedTypes() throws Exception {
    StreamApplicationDescriptorImpl mockGraph = mock(StreamApplicationDescriptorImpl.class);
    OperatorSpec mockOpSpec = mock(OperatorSpec.class);
    MessageStream<TestInputMessageEnvelope> inputStream = new MessageStreamImpl<>(mockGraph, mockOpSpec);
    MapFunction<TestMessageEnvelope, String> keyExtractor = m -> m.getKey();
    FoldLeftFunction<TestMessageEnvelope, Integer> aggregator = (m, c) -> c + 1;
    SupplierFunction<Integer> initialValue = () -> 0;
    // should compile since TestMessageEnvelope (input for functions) is base class of TestInputMessageEnvelope (M)
    Window<TestInputMessageEnvelope, String, Integer> window = Windows.keyedTumblingWindow(keyExtractor, Duration.ofHours(1), initialValue, aggregator, null, mock(Serde.class));
    MessageStream<WindowPane<String, Integer>> windowedStream = inputStream.window(window, "w1");
    ArgumentCaptor<OperatorSpec> registeredOpCaptor = ArgumentCaptor.forClass(OperatorSpec.class);
    verify(mockOpSpec).registerNextOperatorSpec(registeredOpCaptor.capture());
    OperatorSpec<?, TestMessageEnvelope> registeredOpSpec = registeredOpCaptor.getValue();
    assertTrue(registeredOpSpec instanceof WindowOperatorSpec);
    assertEquals(OpCode.WINDOW, registeredOpSpec.getOpCode());
    assertEquals(window, ((WindowOperatorSpec) registeredOpSpec).getWindow());
}
Also used : StreamOperatorSpec(org.apache.samza.operators.spec.StreamOperatorSpec) PartitionByOperatorSpec(org.apache.samza.operators.spec.PartitionByOperatorSpec) StreamApplicationDescriptorImpl(org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl) IntermediateMessageStreamImpl(org.apache.samza.operators.stream.IntermediateMessageStreamImpl) JoinOperatorSpec(org.apache.samza.operators.spec.JoinOperatorSpec) Serde(org.apache.samza.serializers.Serde) SendToTableOperatorSpec(org.apache.samza.operators.spec.SendToTableOperatorSpec) MapFunction(org.apache.samza.operators.functions.MapFunction) OutputStreamImpl(org.apache.samza.operators.spec.OutputStreamImpl) WindowPane(org.apache.samza.operators.windows.WindowPane) OperatorSpec(org.apache.samza.operators.spec.OperatorSpec) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) SinkOperatorSpec(org.apache.samza.operators.spec.SinkOperatorSpec) Matchers.eq(org.mockito.Matchers.eq) Duration(java.time.Duration) Matchers.anyObject(org.mockito.Matchers.anyObject) TestOutputMessageEnvelope(org.apache.samza.operators.data.TestOutputMessageEnvelope) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) OpCode(org.apache.samza.operators.spec.OperatorSpec.OpCode) FilterFunction(org.apache.samza.operators.functions.FilterFunction) Windows(org.apache.samza.operators.windows.Windows) StreamTableJoinFunction(org.apache.samza.operators.functions.StreamTableJoinFunction) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) OutputOperatorSpec(org.apache.samza.operators.spec.OutputOperatorSpec) WindowOperatorSpec(org.apache.samza.operators.spec.WindowOperatorSpec) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) JoinFunction(org.apache.samza.operators.functions.JoinFunction) FlatMapFunction(org.apache.samza.operators.functions.FlatMapFunction) Mockito.verify(org.mockito.Mockito.verify) StreamTableJoinOperatorSpec(org.apache.samza.operators.spec.StreamTableJoinOperatorSpec) Window(org.apache.samza.operators.windows.Window) SinkFunction(org.apache.samza.operators.functions.SinkFunction) KVSerde(org.apache.samza.serializers.KVSerde) Collections(java.util.Collections) FoldLeftFunction(org.apache.samza.operators.functions.FoldLeftFunction) SupplierFunction(org.apache.samza.operators.functions.SupplierFunction) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Serde(org.apache.samza.serializers.Serde) KVSerde(org.apache.samza.serializers.KVSerde) IntermediateMessageStreamImpl(org.apache.samza.operators.stream.IntermediateMessageStreamImpl) WindowOperatorSpec(org.apache.samza.operators.spec.WindowOperatorSpec) WindowPane(org.apache.samza.operators.windows.WindowPane) StreamOperatorSpec(org.apache.samza.operators.spec.StreamOperatorSpec) PartitionByOperatorSpec(org.apache.samza.operators.spec.PartitionByOperatorSpec) JoinOperatorSpec(org.apache.samza.operators.spec.JoinOperatorSpec) SendToTableOperatorSpec(org.apache.samza.operators.spec.SendToTableOperatorSpec) OperatorSpec(org.apache.samza.operators.spec.OperatorSpec) SinkOperatorSpec(org.apache.samza.operators.spec.SinkOperatorSpec) OutputOperatorSpec(org.apache.samza.operators.spec.OutputOperatorSpec) WindowOperatorSpec(org.apache.samza.operators.spec.WindowOperatorSpec) StreamTableJoinOperatorSpec(org.apache.samza.operators.spec.StreamTableJoinOperatorSpec) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) StreamApplicationDescriptorImpl(org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl) Test(org.junit.Test)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 IOException (java.io.IOException)1 Duration (java.time.Duration)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 StreamApplicationDescriptorImpl (org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl)1 TestMessageEnvelope (org.apache.samza.operators.data.TestMessageEnvelope)1 TestOutputMessageEnvelope (org.apache.samza.operators.data.TestOutputMessageEnvelope)1 FilterFunction (org.apache.samza.operators.functions.FilterFunction)1 FlatMapFunction (org.apache.samza.operators.functions.FlatMapFunction)1 FoldLeftFunction (org.apache.samza.operators.functions.FoldLeftFunction)1 JoinFunction (org.apache.samza.operators.functions.JoinFunction)1 MapFunction (org.apache.samza.operators.functions.MapFunction)1 SinkFunction (org.apache.samza.operators.functions.SinkFunction)1 StreamTableJoinFunction (org.apache.samza.operators.functions.StreamTableJoinFunction)1 SupplierFunction (org.apache.samza.operators.functions.SupplierFunction)1 JoinOperatorSpec (org.apache.samza.operators.spec.JoinOperatorSpec)1 OperatorSpec (org.apache.samza.operators.spec.OperatorSpec)1 OpCode (org.apache.samza.operators.spec.OperatorSpec.OpCode)1 OutputOperatorSpec (org.apache.samza.operators.spec.OutputOperatorSpec)1