当前位置: 首页>>代码示例>>Java>>正文


Java KTable.foreach方法代码示例

本文整理汇总了Java中org.apache.kafka.streams.kstream.KTable.foreach方法的典型用法代码示例。如果您正苦于以下问题:Java KTable.foreach方法的具体用法?Java KTable.foreach怎么用?Java KTable.foreach使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.kafka.streams.kstream.KTable的用法示例。


在下文中一共展示了KTable.foreach方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: shouldCountSessionWindowsWithInternalStoreName

import org.apache.kafka.streams.kstream.KTable; //导入方法依赖的package包/类
@Test
public void shouldCountSessionWindowsWithInternalStoreName() throws Exception {
    final Map<Windowed<String>, Long> results = new HashMap<>();
    KTable table = groupedStream.count(SessionWindows.with(30));
    table.foreach(new ForeachAction<Windowed<String>, Long>() {
        @Override
        public void apply(final Windowed<String> key, final Long value) {
            results.put(key, value);
        }
    });
    doCountSessionWindows(results);
    assertNull(table.queryableStoreName());
}
 
开发者ID:YMCoding,项目名称:kafka-0.11.0.0-src-with-comment,代码行数:14,代码来源:KGroupedStreamImplTest.java

示例2: shouldReduceSessionWindows

import org.apache.kafka.streams.kstream.KTable; //导入方法依赖的package包/类
@Test
public void shouldReduceSessionWindows() throws Exception {
    final Map<Windowed<String>, String> results = new HashMap<>();
    KTable table = groupedStream.reduce(
            new Reducer<String>() {
                @Override
                public String apply(final String value1, final String value2) {
                    return value1 + ":" + value2;
                }
            }, SessionWindows.with(30),
            "session-store");
    table.foreach(new ForeachAction<Windowed<String>, String>() {
        @Override
        public void apply(final Windowed<String> key, final String value) {
            results.put(key, value);
        }
    });
    doReduceSessionWindows(results);
    assertEquals(table.queryableStoreName(), "session-store");
}
 
开发者ID:YMCoding,项目名称:kafka-0.11.0.0-src-with-comment,代码行数:21,代码来源:KGroupedStreamImplTest.java

示例3: shouldReduceSessionWindowsWithInternalStoreName

import org.apache.kafka.streams.kstream.KTable; //导入方法依赖的package包/类
@Test
public void shouldReduceSessionWindowsWithInternalStoreName() throws Exception {
    final Map<Windowed<String>, String> results = new HashMap<>();
    KTable table = groupedStream.reduce(
            new Reducer<String>() {
                @Override
                public String apply(final String value1, final String value2) {
                    return value1 + ":" + value2;
                }
            }, SessionWindows.with(30));
    table.foreach(new ForeachAction<Windowed<String>, String>() {
        @Override
        public void apply(final Windowed<String> key, final String value) {
            results.put(key, value);
        }
    });
    doReduceSessionWindows(results);
    assertNull(table.queryableStoreName());
}
 
开发者ID:YMCoding,项目名称:kafka-0.11.0.0-src-with-comment,代码行数:20,代码来源:KGroupedStreamImplTest.java

示例4: shouldCountSessionWindows

import org.apache.kafka.streams.kstream.KTable; //导入方法依赖的package包/类
@Test
public void shouldCountSessionWindows() throws Exception {
    final Map<Windowed<String>, Long> results = new HashMap<>();
    KTable table = groupedStream.count(SessionWindows.with(30), "session-store");
    table.foreach(new ForeachAction<Windowed<String>, Long>() {
        @Override
        public void apply(final Windowed<String> key, final Long value) {
            results.put(key, value);
        }
    });
    doCountSessionWindows(results);
    assertEquals(table.queryableStoreName(), "session-store");
}
 
开发者ID:YMCoding,项目名称:kafka-0.11.0.0-src-with-comment,代码行数:14,代码来源:KGroupedStreamImplTest.java

示例5: doShouldReduce

import org.apache.kafka.streams.kstream.KTable; //导入方法依赖的package包/类
private void doShouldReduce(final KTable<String, Integer> reduced, final String topic) throws Exception {
    final Map<String, Integer> results = new HashMap<>();
    reduced.foreach(new ForeachAction<String, Integer>() {
        @Override
        public void apply(final String key, final Integer value) {
            results.put(key, value);
        }
    });

    driver = new KStreamTestDriver(builder, TestUtils.tempDirectory(), Serdes.String(), Serdes.Integer());
    driver.setTime(10L);
    driver.process(topic, "A", 1.1);
    driver.process(topic, "B", 2.2);
    driver.flushState();

    assertEquals(Integer.valueOf(1), results.get("A"));
    assertEquals(Integer.valueOf(2), results.get("B"));

    driver.process(topic, "A", 2.6);
    driver.process(topic, "B", 1.3);
    driver.process(topic, "A", 5.7);
    driver.process(topic, "B", 6.2);
    driver.flushState();

    assertEquals(Integer.valueOf(5), results.get("A"));
    assertEquals(Integer.valueOf(6), results.get("B"));
}
 
开发者ID:YMCoding,项目名称:kafka-0.11.0.0-src-with-comment,代码行数:28,代码来源:KGroupedTableImplTest.java

示例6: testForeach

import org.apache.kafka.streams.kstream.KTable; //导入方法依赖的package包/类
@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);
    }
}
 
开发者ID:YMCoding,项目名称:kafka-0.11.0.0-src-with-comment,代码行数:46,代码来源:KTableForeachTest.java


注:本文中的org.apache.kafka.streams.kstream.KTable.foreach方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。