繁体   English   中英

使用 Kafka Streams 按 ID 和时间连接日志 - 无法刷新 state 存储

[英]Concatenate logs by ID and time using Kafka Streams - Failed to flush state store

我想使用 Kafka Streams 在 window 时间内按 ID 连接日志。

目前,我可以成功统计具有相同 ID 的日志数量(注释代码)。

但是,当我用.aggregate替换.count方法时,我会遇到以下错误:

"Failed to flush state store time-windowed-aggregation-stream-store"
Caused by: java.lang.ClassCastException: org.apache.kafka.streams.kstream.Windowed cannot be cast to java.lang.String

我是新手,无法弄清楚这个错误的原因,我认为拥有.withValueSerde(Serdes.String())应该可以防止这种情况发生。

在我的代码下面:

package myapps;

import java.time.Duration;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.common.utils.Bytes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.Topology;
import org.apache.kafka.streams.kstream.*;
import org.apache.kafka.streams.kstream.KStream;
import org.apache.kafka.streams.kstream.Suppressed.*;
import org.apache.kafka.streams.state.WindowStore;

public class MyCode {

    public static void main(String[] args) throws Exception {
        Properties props = new Properties();
        props.put(StreamsConfig.APPLICATION_ID_CONFIG, "streams-mycode");
        props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
        props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());

        final StreamsBuilder builder = new StreamsBuilder();

        KStream<String, String> source = builder.stream("streams-plaintext-input");
        KStream<String, String> changedKeyStream = source.selectKey((k, v)
         -> v.substring(v.indexOf("mid="),v.indexOf("mid=")+8));

        /* // Working code for count
        changedKeyStream
        .groupByKey()
        .windowedBy(TimeWindows.of(Duration.ofSeconds(3))
        .grace(Duration.ofSeconds(2)))
        .count(Materialized.with(Serdes.String(), Serdes.Long())) // could be replaced with an aggregator (reducer?) ? 
        .suppress(Suppressed.untilWindowCloses(BufferConfig.unbounded()))
        .toStream()
        .print(Printed.toSysOut());
        */

        changedKeyStream
        .groupByKey()
        .windowedBy(TimeWindows.of(Duration.ofSeconds(3)))

        .aggregate(
        String::new, (String k, String v, String Result) -> { return Result+"\n"+v; },
        Materialized.<String, String, WindowStore<Bytes, byte[]>>as("time-windowed-aggregated-stream-store") /* state store name */
        .withValueSerde(Serdes.String())) /* serde for aggregate value */
        .suppress(Suppressed.untilWindowCloses(BufferConfig.unbounded()))
        .toStream()
        .print(Printed.toSysOut());

        changedKeyStream.to("streams-mycode-output", Produced.with(Serdes.String(), Serdes.String()));

        final Topology topology = builder.build();
        final KafkaStreams streams = new KafkaStreams(topology, props);
        final CountDownLatch latch = new CountDownLatch(1);

        // attach shutdown handler to catch control-c
        Runtime.getRuntime().addShutdownHook(new Thread("streams-shutdown-hook") {
            @Override
            public void run() {
                streams.close();
                latch.countDown();
            }
        });

        // launch until control+c
        try {
            streams.start();
            latch.await();
        } catch (Throwable e) {
            System.out.print("Something went wrong!"); 
            System.exit(1);
        }
        System.exit(0);
    }
}

预先感谢您的帮助。

有两个选项可以修复它:

  1. org.apache.kafka.streams.kstream.Grouped传递给KStream::groupByKey
  2. org.apache.kafka.common.serialization.Serde设置为 Materialized - Materialized::withKeySerde(...)

示例代码如下:

广告 1。

changedKeyStream
  .groupByKey(Grouped.with(Serdes.String(), Serdes.String()))
  .windowedBy(TimeWindows.of(Duration.ofSeconds(3)))

广告 2。

changedKeyStream
  .groupByKey()
  .windowedBy(TimeWindows.of(Duration.ofSeconds(3)))
  .aggregate(
    String::new, (String k, String v, String Result) -> { return Result+"_"+v; },
    Materialized.<String, String, WindowStore<Bytes, byte[]>>as("time-windowed-aggregated-stream-store") /* state store name */
      .withValueSerde(Serdes.String())
      .withKeySerde(Serdes.String())
   )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM