Kafka---窗口函数

该博客展示了如何使用Kafka Streams处理时间窗口,包括跳跃窗口、滑动窗口和会话窗口的实现。通过Java代码示例,演示了从输入流中读取数据,进行值的拆分、映射、分组,然后应用不同类型的窗口进行操作,最后对窗口计数并打印结果。这为实时数据处理和分析提供了基础。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 跳跃窗口
  • 滑动窗口
  • 会话窗口
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.*;
import org.apache.kafka.streams.kstream.*;

import java.time.Duration;
import java.util.Arrays;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;

/**
 * @Author shall潇
 * @Date 2021/5/28
 * @Description
 */
public class WindowStreamDemo {
    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.put(StreamsConfig.APPLICATION_ID_CONFIG,"windowdemo");
        properties.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.159.100:9092");
        properties.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
        properties.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG,Serdes.String().getClass());
        properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");
        properties.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG,3000);
        properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,false);

        StreamsBuilder builder = new StreamsBuilder();

//        TimeWindowedKStream<String, String> windowsdemo = builder.stream("windowsdemo")
//                .flatMapValues((value) -> {
//                    String[] split = value.toString().split("\\s");
//                    return Arrays.asList(split);
//                }).map((key, value) -> {
//                    return new KeyValue<String, String>(value, "1");
//                }).groupByKey()
                //第一种 跳跃窗口 ,每次移动15秒
//                .windowedBy(TimeWindows.of(Duration.ofSeconds(15).toMillis()));
                //第二种 滑动窗口,窗口大小为 15秒,每次移动5秒
//        .windowedBy(TimeWindows.of(Duration.ofSeconds(15).toMillis()).advanceBy(Duration.ofSeconds(5).toMillis()));

                //第三种 会话窗口,固定时间间隔sessionWindow 返回值 SessionWindowedKStream
        SessionWindowedKStream<String, String> windowsdemo = builder.stream("windowsdemo")
                .flatMapValues((value) -> {
                    String[] split = value.toString().split("\\s");
                    return Arrays.asList(split);
                }).map((key, value) -> {
                    return new KeyValue<String, String>(value, "1");
                }).groupByKey()
                .windowedBy(SessionWindows.with(Duration.ofSeconds(15).toMillis()));

        KStream<Windowed<String>, Long> wlks = windowsdemo.count().toStream();
        wlks.foreach((key,value)->{
            System.out.println(key+"-->"+value);
        });

        Topology topo = builder.build();
        //将 拓扑结构 和 配置信息 放入 KafkaStream
        KafkaStreams kafkaStreams = new KafkaStreams(topo, properties);

        CountDownLatch latch = new CountDownLatch(1);
        Runtime.getRuntime().addShutdownHook(new Thread("线程1"){
            @Override
            public void run() {
                kafkaStreams.close();
                latch.countDown();
            }
        });

        kafkaStreams.start();
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.exit(0);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值