Search in sources :

Example 1 with ForeachAction

use of org.apache.kafka.streams.kstream.ForeachAction in project kafka by apache.

the class KStreamForeachTest method testForeach.

@Test
public void testForeach() {
    // Given
    List<KeyValue<Integer, String>> inputRecords = Arrays.asList(new KeyValue<>(0, "zero"), new KeyValue<>(1, "one"), new KeyValue<>(2, "two"), new KeyValue<>(3, "three"));
    List<KeyValue<Integer, String>> expectedRecords = Arrays.asList(new KeyValue<>(0, "ZERO"), new KeyValue<>(2, "ONE"), new KeyValue<>(4, "TWO"), new KeyValue<>(6, "THREE"));
    final List<KeyValue<Integer, String>> actualRecords = new ArrayList<>();
    ForeachAction<Integer, String> action = new ForeachAction<Integer, String>() {

        @Override
        public void apply(Integer key, String value) {
            actualRecords.add(new KeyValue<>(key * 2, value.toUpperCase(Locale.ROOT)));
        }
    };
    // When
    KStreamBuilder builder = new KStreamBuilder();
    KStream<Integer, String> stream = builder.stream(intSerde, stringSerde, topicName);
    stream.foreach(action);
    // Then
    driver = new KStreamTestDriver(builder);
    for (KeyValue<Integer, String> record : inputRecords) {
        driver.process(topicName, record.key, record.value);
    }
    assertEquals(expectedRecords.size(), actualRecords.size());
    for (int i = 0; i < expectedRecords.size(); i++) {
        KeyValue<Integer, String> expectedRecord = expectedRecords.get(i);
        KeyValue<Integer, String> actualRecord = actualRecords.get(i);
        assertEquals(expectedRecord, actualRecord);
    }
}
Also used : KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) KeyValue(org.apache.kafka.streams.KeyValue) ArrayList(java.util.ArrayList) ForeachAction(org.apache.kafka.streams.kstream.ForeachAction) KStreamTestDriver(org.apache.kafka.test.KStreamTestDriver) Test(org.junit.Test)

Example 2 with ForeachAction

use of org.apache.kafka.streams.kstream.ForeachAction in project kafka by apache.

the class KTableForeachTest method testForeach.

@Test
public void testForeach() {
    // Given
    List<KeyValue<Integer, String>> inputRecords = Arrays.asList(new KeyValue<>(0, "zero"), new KeyValue<>(1, "one"), new KeyValue<>(2, "two"), new KeyValue<>(3, "three"));
    List<KeyValue<Integer, String>> expectedRecords = Arrays.asList(new KeyValue<>(0, "ZERO"), new KeyValue<>(2, "ONE"), new KeyValue<>(4, "TWO"), new KeyValue<>(6, "THREE"));
    final List<KeyValue<Integer, String>> actualRecords = new ArrayList<>();
    ForeachAction<Integer, String> action = new ForeachAction<Integer, String>() {

        @Override
        public void apply(Integer key, String value) {
            actualRecords.add(new KeyValue<>(key * 2, value.toUpperCase(Locale.ROOT)));
        }
    };
    // When
    KStreamBuilder builder = new KStreamBuilder();
    KTable<Integer, String> table = builder.table(intSerde, stringSerde, topicName, "anyStoreName");
    table.foreach(action);
    // Then
    driver = new KStreamTestDriver(builder, stateDir);
    for (KeyValue<Integer, String> record : inputRecords) {
        driver.process(topicName, record.key, record.value);
    }
    driver.flushState();
    assertEquals(expectedRecords.size(), actualRecords.size());
    for (int i = 0; i < expectedRecords.size(); i++) {
        KeyValue<Integer, String> expectedRecord = expectedRecords.get(i);
        KeyValue<Integer, String> actualRecord = actualRecords.get(i);
        assertEquals(expectedRecord, actualRecord);
    }
}
Also used : KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) KeyValue(org.apache.kafka.streams.KeyValue) ArrayList(java.util.ArrayList) ForeachAction(org.apache.kafka.streams.kstream.ForeachAction) KStreamTestDriver(org.apache.kafka.test.KStreamTestDriver) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)2 KeyValue (org.apache.kafka.streams.KeyValue)2 ForeachAction (org.apache.kafka.streams.kstream.ForeachAction)2 KStreamBuilder (org.apache.kafka.streams.kstream.KStreamBuilder)2 KStreamTestDriver (org.apache.kafka.test.KStreamTestDriver)2 Test (org.junit.Test)2