Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache kafka 卡夫卡流加窗口的关键是人类可读的_Apache Kafka_Apache Kafka Streams_Confluent Platform_Ksqldb - Fatal编程技术网

Apache kafka 卡夫卡流加窗口的关键是人类可读的

Apache kafka 卡夫卡流加窗口的关键是人类可读的,apache-kafka,apache-kafka-streams,confluent-platform,ksqldb,Apache Kafka,Apache Kafka Streams,Confluent Platform,Ksqldb,我正在卡夫卡流上进行窗口聚合。 它工作良好,并进行正确的聚合。 下面是scala中的代码。 CallRecord是一个案例类 builder .stream[String, String](input_topic) .mapValues((elm) => { parse(elm).extract[CallRecord] }) .groupBy((key, value) => { value.agentId }) .windowedBy(

我正在卡夫卡流上进行窗口聚合。 它工作良好,并进行正确的聚合。 下面是scala中的代码。
CallRecord
是一个案例类

    builder
  .stream[String, String](input_topic)
  .mapValues((elm) => {
    parse(elm).extract[CallRecord]
  })
  .groupBy((key, value) => {
    value.agentId
  })
  .windowedBy(every15Minute)
  .aggregate(CallRecordAggByAgent.empty)((_, callRecord, aggregator) => {
    CallRecordAggByAgent(
      callRecord.agentId,
      ((callRecord.durationMinutes + aggregator.durationMinutesAvg) / aggregator.count).roundD,
      ((callRecord.waitingMinutes + aggregator.waitingMinutesAvg) / aggregator.count).roundD,
      ((callRecord.customerScore + aggregator.scoreAvg) / aggregator.count).roundD,
      aggregator.count + 1
    )
  })
  .mapValues((elm) => {
    write(elm)
  })
  .toStream
  .to(output_topic)
在输出主题中,我看到类似这样的键。

当我尝试从KSQLDB中读取此内容时,当我创建关于此主题的流时,我看到rowkey的值如下
3w�H�@
我知道这是反序列化问题,但我希望能够在KSQL中直接反序列化,或者在流式传输到输出主题时使其长度达到毫秒。

我的理解是这应该很容易实现,但我想我忽略了一些细微差别。

我给出的解决方案如下。显然这并不难

import io.circe.generic.auto._
import org.json4s._
import org.json4s.native.Serialization.write

builder
      .stream[String, String](args.INPUT_TOPIC)
      .mapValues((elm) => {
        parse(elm).extract[CallRecord]
      })
      .groupBy((key, value) => {
        value.agentId
      })
      .windowedBy(every15Minute)
      .aggregate(CallRecordAggByAgent.empty)((_, callRecord, aggregator) => {
        CallRecordAggByAgent(
          callRecord.agentId,
          ((callRecord.durationMinutes + aggregator.durationMinutesAvg) / aggregator.count).roundD,
          ((callRecord.waitingMinutes + aggregator.waitingMinutesAvg) / aggregator.count).roundD,
          ((callRecord.customerScore + aggregator.scoreAvg) / aggregator.count).roundD,
          aggregator.count + 1
        )
      })
      .mapValues((elm) => {
        write(elm)
      })
      .toStream
      .selectKey((k, v) => {
        s"${k.key()}@${k.window().startTime().toEpochMilli.toString}"
      })
      .to(args.OUTPUT_TOPIC)
selectKey
提供了更改分组键的可能性,
因此,在流式传输到输出主题之前,我从键中提取时间戳,并将其设置为字符串。

您能够解决吗?@dune98抱歉,忘记发布我的答案了。现在它在那里了,请看一看。