kafka流中的自定义时间戳提取器

时间:2018-01-03 20:06:05

标签: apache-kafka apache-kafka-streams confluent

我正在尝试使用kafka流来处理来自kafka主题的一些数据。数据来自kafka主题,由kafka 0.11.0写入,没有嵌入时间戳。在互联网上阅读了一些内容后,我开始明白我可以通过在自定义类中扩展TimestampExtractor类并将其传递到StreamsConfig来解决此问题。

我是这样做的 -

class MyEventTimestampExtractor extends TimestampExtractor {
  override def extract(record: ConsumerRecord[AnyRef, AnyRef], prev: Long) = {
    record.value() match {
        case w: String => 1000L
        case _ => throw new RuntimeException(s"Called for $record")
    }
  }
}

我的基础是this code on github

但是,当我执行sbt run

时,我收到此错误
[error] /home/someuser/app/blahblah/src/main/scala/main.scala:34: class MyEventTimestampExtractor needs to be abstract, since method extract in trait TimestampExtractor of type (x$1: org.apache.kafka.clients.consumer.ConsumerRecord[Object,Object], x$2: Long)Long is not defined
[error] (Note that Long does not match Long)
[error] class MyEventTimestampExtractor extends TimestampExtractor {
[error]       ^
[error] /home/someuser/app/blahblah/src/main/scala/main.scala:35: method extract overrides nothing.
[error] Note: the super classes of class MyEventTimestampExtractor contain the following, non final members named extract:
[error] def extract(x$1: org.apache.kafka.clients.consumer.ConsumerRecord[Object,Object],x$2: Long): Long
[error]   override def extract(record: ConsumerRecord[AnyRef, AnyRef], prev: Long): Long = {
[error]                ^
[error] two errors found
[error] (compile:compileIncremental) Compilation failed

我的build.sbt文件就是这个 -

name := "kafka streams experiment"
version := "1.0"
scalaVersion := "2.12.4"

libraryDependencies ++= Seq(
  "org.apache.kafka" % "kafka-streams" % "1.0.0"
)

我真的不明白这个错误。特别是围绕Note that Long does not match Long的部分。我能做错什么?谢谢!

2 个答案:

答案 0 :(得分:2)

你需要java.Long,因为这个API是用Java定义的,而你使用的是Scala Long

答案 1 :(得分:0)

尝试(观察prev函数参数的类型:

  override def extract(record: ConsumerRecord[AnyRef, AnyRef], prev: java.lang.Long) = {