这是我的应用程序,它仅从客户主题(输入绑定)获取一个KStream的引用,而从订单主题(订单绑定)获取另一个的KStream。然后,它根据客户主题创建一个KTable,并按订单KStream执行联接:
@Configuration
class ShippingKStreamConfiguration {
@StreamListener
@SendTo("output")
fun process(@Input("input") input: KStream<Int, Customer>, @Input("order") order: KStream<Int, Order>): KStream<Int, OrderShipped> {
val intSerde = Serdes.IntegerSerde()
val customerSerde = JsonSerde<Customer>(Customer::class.java)
val orderSerde = JsonSerde<Order>(Order::class.java)
val stateStore: Materialized<Int, Customer, KeyValueStore<Bytes, ByteArray>> =
Materialized.`as`<Int, Customer, KeyValueStore<Bytes, ByteArray>>("customer-store")
.withKeySerde(intSerde)
.withValueSerde(customerSerde)
val customerTable: KTable<Int, Customer> = input.groupByKey(Serialized.with(intSerde, customerSerde))
.reduce({ _, y -> y }, stateStore)
return (order.selectKey { key, value -> value.customerId } as KStream<Int, Order>)
.join(customerTable, { orderIt, customer ->
OrderShipped(orderIt.id)
},
Joined.with(intSerde, orderSerde, customerSerde))
}
}
假设这应该写入到输出绑定(@SendTo("output")
),并指向订购主题。但是,没有消息写到该主题。
处理器配置:
interface ShippingKStreamProcessor {
@Input("input")
fun input(): KStream<Int, Customer>
@Input("order")
fun order(): KStream<String, Order>
@Input("output")
fun output(): KStream<String, OrderShipped>
}
**application.yml**
spring:
application:
name: spring-boot-shipping-service
cloud:
stream:
kafka:
streams:
binder:
configuration:
default:
key:
serde: org.apache.kafka.common.serialization.Serdes$IntegerSerde
value:
serde: org.apache.kafka.common.serialization.Serdes$StringSerde
bindings:
input:
destination: customer
contentType: application/json
order:
destination: order
contentType: application/json
output:
destination: ordershipments
contentType: application/json
答案 0 :(得分:0)
处理器定义错误,这是使用@Output
而非@Input
的好方法:
interface ShippingKStreamProcessor {
@Input("input")
fun input(): KStream<Int, Customer>
@Input("order")
fun order(): KStream<String, Order>
@Output("output")
fun output(): KStream<String, OrderShipped>
}