卡夫卡流式记录在窗口化/聚合后无法转发

时间:2019-04-30 16:43:58

标签: java aggregation apache-kafka-streams windowing

我正在使用Kafka Streams和Tumbling Window,后面是聚合步骤。但是,观察到为聚合函数发出的元组的数量正在下降。知道我要去哪里错了吗?

代码:

  Properties props = new Properties();
  props.put(StreamsConfig.APPLICATION_ID_CONFIG, "events_streams_local");
  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());
  props.put(StreamsConfig.METRIC_REPORTER_CLASSES_CONFIG, Arrays.asList(JmxReporter.class));
  props.put(StreamsConfig.STATE_DIR_CONFIG, "/tmp/kafka-streams/data/");
  props.put(StreamsConfig.NUM_STREAM_THREADS_CONFIG, 20);

  props.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, 60000);
  props.put(StreamsConfig.DEFAULT_TIMESTAMP_EXTRACTOR_CLASS_CONFIG, EventTimeExtractor.class);

  props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest");

  final StreamsBuilder builder = new StreamsBuilder();
  HashGenerator hashGenerator = new HashGenerator(1);
  builder
  .stream(inputTopics)
  .mapValues((key, value) -> {
    stats.incrInputRecords();
    Event event = jsonUtil.fromJson((String) value, Event.class);
    return event;
  })
  .filter(new UnifiedGAPingEventFilter(stats))
  .selectKey(new KeyValueMapper<Object, Event, String>() {

    @Override
    public String apply(Object key, Event event) {
      return (String) key;
    }
  })
  .groupByKey(Grouped.with(Serdes.String(), eventSerdes))
  .windowedBy(TimeWindows.of(Duration.ofSeconds(30)))
  .aggregate(new AggregateInitializer(), new UserStreamAggregator(), Materialized.with(Serdes.String(), aggrSerdes))
  .mapValues((k, v) -> {
    // update counter for aggregate records
    return v;
  })
  .toStream()
  .map(new RedisSink(stats));

  topology = builder.build();
  streams = new KafkaStreams(topology, props);

Redis每秒的操作量只是向下滑动。

1 个答案:

答案 0 :(得分:0)

Kafka Streams使用状态存储中的缓存来减少下游负载。如果要将每个更新作为下游记录获取,则可以通过StreamsConfig#CACHE_MAX_BYTES_BUFFERING_CONFIG(对于所有商店都是全局的)将缓存大小设置为零,或者通过将Materialized.as(...).withCachingDisabled()传递给相应的运算符来将每个商店的缓存大小设置为(例如aggregate())。

查看文档以获取更多详细信息:https://docs.confluent.io/current/streams/developer-guide/memory-mgmt.html