scala - 如何让 KTable 只发出最新的更新?
问题描述
MyKTable
在每次更新时都会发出,而不仅仅是最新的更新。
请参阅下面的代码(在 Scala 中):
object SimpleTable extends App {
val topic = "simple-table"
val prodProps = new Properties()
prodProps.put("bootstrap.servers", "localhost:9092")
prodProps.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
prodProps.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")
prodProps.put("acks", "1")
prodProps.put("retries", "3")
val producer = new KafkaProducer[String, String](prodProps)
producer.send(new ProducerRecord[String, String](topic, "key1", "value1"))
producer.send(new ProducerRecord[String, String](topic, "key2", "value2"))
producer.send(new ProducerRecord[String, String](topic, "key3", "value3"))
producer.send(new ProducerRecord[String, String](topic, "key1", "value11"))
producer.send(new ProducerRecord[String, String](topic, "key2", "value22"))
producer.send(new ProducerRecord[String, String](topic, "key3", "value33"))
producer.close()
val streamProps = new Properties()
streamProps.put(StreamsConfig.APPLICATION_ID_CONFIG, "simple-table-app1")
streamProps.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092")
//streamProps.put(ConsumerConfig.GROUP_ID_CONFIG, "group11")
//streamProps.put(ConsumerConfig.CLIENT_ID_CONFIG, "client11")
//streamProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest")
//streamProps.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, "18000")
//streamProps.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, "18000")
//streamProps.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, "10485760")
//streamProps.put(StreamsConfig.NUM_STREAM_THREADS_CONFIG, "1")
//streamProps.put(ConsumerConfig.METADATA_MAX_AGE_CONFIG, "10000")
//streamProps.put(StreamsConfig.REPLICATION_FACTOR_CONFIG, 1)
//streamProps.put(StreamsConfig.DEFAULT_TIMESTAMP_EXTRACTOR_CLASS_CONFIG, classOf[WallclockTimestampExtractor])
import org.apache.kafka.streams.scala.Serdes._
implicit val consumeSerdes: Consumed[String, String] = Consumed.`with`[String, String]
val builder = new StreamsBuilder()
val simpleTable: KTable[String, String] = builder.table[String, String](topic)
simpleTable.toStream.print(Printed.toSysOut[String, String].withLabel("simple-table"))
val streams = new KafkaStreams(builder.build(), streamProps)
streams.start()
Thread.sleep(10000)
streams.close()
}
这个应用程序正在显示:
[simple-table]: key1, value1
[simple-table]: key2, value2
[simple-table]: key3, value3
[simple-table]: key1, value11
[simple-table]: key2, value22
[simple-table]: key3, value33
我应该只有最新的 3 行。请帮忙。
更新
根据下面的解决方案,当我这样创建 KTable 时,一切正常:
val simpleTable: KTable[String, String] =
builder.table[String, String](topic, Materialized.as[String, String, KeyValueStore[Bytes, Array[Byte]]]("simple-table-store"))
解决方案
我从这个问题得到了答案。
该代码用于使用旧版本的 kafka-streams,早于 2.2。
复制粘贴
在 Kafka 2.2 中,引入了优化以减少 Kafka Streams 的资源占用。如果计算不需要 KTable,则不一定要实现它。这适用于您的情况,因为 mapValues() 可以即时计算。因为 KTable 没有具体化,所以没有缓存,因此每个输入记录都会产生一个输出记录。
比较:https ://issues.apache.org/jira/browse/KAFKA-6036
如果要强制执行 KTable 物化,可以将 Matrilized.as("someStoreName") 传入 StreamsBuilder#table() 方法。
推荐阅读
- github - 我如何在 GitHub 中请求组织批准 OAuth 应用程序,真的吗?
- sql-server - JPA - 仅当当前为空时才允许更新列
- html - CSS 背景滤镜模糊在 Windows 上没有正确模糊边缘(适用于 macOS)
- arrays - 如何将 GitLab CI 变量设置为 gitlab-ci 中的数组?
- typescript - 类扩展 Number 在通过 RPC 发送后不保留函数
- delphi - 如何构建一个通用类来管理对象类型的不同过程
- unity3d - (统一)移动平台奇怪地移动玩家
- python - 将现有 Python 类连接到 GUI 界面
- react-native - react-native-web 更新:TypeError:无法读取未定义的属性“样式”
- angular - primeng MenuItem:“从不”类型上不存在属性“命令”