Search in sources :

Example 11 with OutgoingMessageEnvelope

use of org.apache.samza.system.OutgoingMessageEnvelope in project samza by apache.

the class Emitter method window.

public void window(MessageCollector collector, TaskCoordinator coordinator) {
    Integer epoch = getInt(EPOCH);
    if (epoch == null) {
        resetEpoch();
        return;
    }
    int counter = getInt(COUNT);
    if (counter < max) {
        logger.info("Emitting: " + counter + ", epoch = " + epoch + ", task = " + taskName);
        OutgoingMessageEnvelope envelope = new OutgoingMessageEnvelope(new SystemStream("kafka", "emitted"), Integer.toString(counter), epoch + "-" + taskName.toString());
        collector.send(envelope);
        this.state.put(COUNT, Integer.toString(getInt(COUNT) + 1));
    }
}
Also used : SystemStream(org.apache.samza.system.SystemStream) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope)

Example 12 with OutgoingMessageEnvelope

use of org.apache.samza.system.OutgoingMessageEnvelope in project samza by apache.

the class NegateNumberTask method process.

@Override
public void process(IncomingMessageEnvelope envelope, MessageCollector collector, TaskCoordinator coordinator) {
    messagesProcessed += 1;
    String input = (String) envelope.getMessage();
    Integer number = Integer.valueOf(input);
    Integer output = number.intValue() * -1;
    collector.send(new OutgoingMessageEnvelope(outputSystemStream, output.toString()));
    if (messagesProcessed >= maxMessages) {
        coordinator.shutdown(RequestScope.ALL_TASKS_IN_CONTAINER);
    }
}
Also used : OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope)

Example 13 with OutgoingMessageEnvelope

use of org.apache.samza.system.OutgoingMessageEnvelope in project samza by apache.

the class TestOperatorSpecs method testCreateSinkOperator.

@Test
public void testCreateSinkOperator() {
    SystemStream testStream = new SystemStream("test-sys", "test-stream");
    SinkFunction<TestMessageEnvelope> sinkFn = (TestMessageEnvelope message, MessageCollector messageCollector, TaskCoordinator taskCoordinator) -> {
        messageCollector.send(new OutgoingMessageEnvelope(testStream, message.getKey(), message.getMessage()));
    };
    SinkOperatorSpec<TestMessageEnvelope> sinkOp = OperatorSpecs.createSinkOperatorSpec(sinkFn, 1);
    assertEquals(sinkOp.getSinkFn(), sinkFn);
    TestMessageEnvelope mockInput = mock(TestMessageEnvelope.class);
    when(mockInput.getKey()).thenReturn("my-test-msg-key");
    MessageType mockMsgBody = mock(MessageType.class);
    when(mockInput.getMessage()).thenReturn(mockMsgBody);
    final List<OutgoingMessageEnvelope> outputMsgs = new ArrayList<>();
    MessageCollector mockCollector = mock(MessageCollector.class);
    doAnswer(invocation -> {
        outputMsgs.add((OutgoingMessageEnvelope) invocation.getArguments()[0]);
        return null;
    }).when(mockCollector).send(any());
    sinkOp.getSinkFn().apply(mockInput, mockCollector, null);
    assertEquals(1, outputMsgs.size());
    assertEquals(outputMsgs.get(0).getKey(), "my-test-msg-key");
    assertEquals(outputMsgs.get(0).getMessage(), mockMsgBody);
    assertEquals(sinkOp.getOpCode(), OperatorSpec.OpCode.SINK);
    assertEquals(sinkOp.getNextStream(), null);
}
Also used : TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) SystemStream(org.apache.samza.system.SystemStream) MessageCollector(org.apache.samza.task.MessageCollector) ArrayList(java.util.ArrayList) TaskCoordinator(org.apache.samza.task.TaskCoordinator) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) MessageType(org.apache.samza.operators.data.MessageType) Test(org.junit.Test)

Example 14 with OutgoingMessageEnvelope

use of org.apache.samza.system.OutgoingMessageEnvelope in project samza by apache.

the class TestCoordinatorStreamWriter method testSendMessage.

@SuppressWarnings({ "unchecked", "rawtypes" })
public void testSendMessage() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    //check a correct message
    assertEquals(0, systemProducer.getEnvelopes().size());
    coordinatorStreamWriter.sendMessage("set-config", "key0", "value0");
    assertEquals(1, systemProducer.getEnvelopes().size());
    //check invalid input is handled
    boolean exceptionHappened = false;
    try {
        coordinatorStreamWriter.sendMessage("invalid-type", "key-invalid", "value-invalid");
    } catch (IllegalArgumentException e) {
        exceptionHappened = true;
    }
    assertTrue(exceptionHappened);
    assertEquals(1, systemProducer.getEnvelopes().size());
    //check sendSetConfigMessage method works correctly
    Class[] sendArgs = { String.class, String.class };
    Method sendSetConfigMethod = coordinatorStreamWriter.getClass().getDeclaredMethod("sendSetConfigMessage", sendArgs);
    sendSetConfigMethod.setAccessible(true);
    sendSetConfigMethod.invoke(coordinatorStreamWriter, "key1", "value1");
    assertEquals(2, systemProducer.getEnvelopes().size());
    //check the messages are correct
    List<OutgoingMessageEnvelope> envelopes = systemProducer.getEnvelopes();
    OutgoingMessageEnvelope envelope0 = envelopes.get(0);
    OutgoingMessageEnvelope envelope1 = envelopes.get(1);
    TypeReference<Object[]> keyRef = new TypeReference<Object[]>() {
    };
    TypeReference<Map<String, Object>> msgRef = new TypeReference<Map<String, Object>>() {
    };
    assertEquals(2, envelopes.size());
    assertEquals("key0", deserialize((byte[]) envelope0.getKey(), keyRef)[CoordinatorStreamMessage.KEY_INDEX]);
    Map<String, String> values = (Map<String, String>) deserialize((byte[]) envelope0.getMessage(), msgRef).get("values");
    assertEquals("value0", values.get("value"));
    assertEquals("key1", deserialize((byte[]) envelope1.getKey(), keyRef)[CoordinatorStreamMessage.KEY_INDEX]);
    values = (Map<String, String>) deserialize((byte[]) envelope1.getMessage(), msgRef).get("values");
    assertEquals("value1", values.get("value"));
}
Also used : Method(java.lang.reflect.Method) TypeReference(org.codehaus.jackson.type.TypeReference) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) HashMap(java.util.HashMap) Map(java.util.Map)

Example 15 with OutgoingMessageEnvelope

use of org.apache.samza.system.OutgoingMessageEnvelope in project samza by apache.

the class DefaultIndexRequestFactoryTest method testGetIndexRequestNoId.

@Test
public void testGetIndexRequestNoId() throws Exception {
    IndexRequest indexRequest = indexRequestFactory.getIndexRequest(new OutgoingMessageEnvelope(SYSTEM, EMPTY_MSG));
    assertNull(indexRequest.id());
}
Also used : IndexRequest(org.elasticsearch.action.index.IndexRequest) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) Test(org.junit.Test)

Aggregations

OutgoingMessageEnvelope (org.apache.samza.system.OutgoingMessageEnvelope)20 Test (org.junit.Test)12 IndexRequest (org.elasticsearch.action.index.IndexRequest)8 SystemStream (org.apache.samza.system.SystemStream)7 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 SamzaException (org.apache.samza.SamzaException)2 MessageType (org.apache.samza.operators.data.MessageType)2 TestMessageEnvelope (org.apache.samza.operators.data.TestMessageEnvelope)2 MessageCollector (org.apache.samza.task.MessageCollector)2 TaskCoordinator (org.apache.samza.task.TaskCoordinator)2 TypeReference (org.codehaus.jackson.type.TypeReference)2 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 CoordinatorStreamMessage (org.apache.samza.coordinator.stream.messages.CoordinatorStreamMessage)1 Delete (org.apache.samza.coordinator.stream.messages.Delete)1 SetConfig (org.apache.samza.coordinator.stream.messages.SetConfig)1 OperatorSpec (org.apache.samza.operators.spec.OperatorSpec)1 PartialJoinOperatorSpec (org.apache.samza.operators.spec.PartialJoinOperatorSpec)1