Java 如何在spring cloud stream活页夹kafka streams中使用函数方法检索/设置标题:3.1.1
我正在使用带有函数式编程的Java 如何在spring cloud stream活页夹kafka streams中使用函数方法检索/设置标题:3.1.1,java,apache-kafka-streams,spring-cloud-stream,spring-cloud-stream-binder-kafka,Java,Apache Kafka Streams,Spring Cloud Stream,Spring Cloud Stream Binder Kafka,我正在使用带有函数式编程的spring cloud stream活页夹kafka streams:3.1.1。如何在processor函数中检索所有标题 Java代码 @SpringBootApplication public class KafkaMessageApplication { public static void main(String args[]) { SpringApplication.run(KafkaMessageApplication.class
spring cloud stream活页夹kafka streams:3.1.1
。如何在processor函数中检索所有标题
Java代码
@SpringBootApplication
public class KafkaMessageApplication {
public static void main(String args[]) {
SpringApplication.run(KafkaMessageApplication.class, args);
}
@Bean
public Function<KStream<String, String>, KStream<String, String>> process() {
// TODO investigate headers on the incoming message
// For example, find partition key on which message was received and publish to same partition key on destination topic
return input -> input;
}
}
@springboot应用程序
公共类Kafka消息应用程序{
公共静态void main(字符串参数[]){
run(KafkaMessageApplication.class,args);
}
@豆子
公共职能流程(){
//TODO调查传入消息的标题
//例如,查找接收消息的分区键并发布到目标主题上的同一分区键
返回输入->输入;
}
}
为了访问这样的头文件,您需要在Kafka Streams中使用低级处理器/转换器API。您可以混合使用低级处理器API和DSL,同时仍将其用作Spring云流应用程序。有关更多详细信息,请参阅。基本上,您需要在消费者的情况下使用处理器,在函数的情况下使用转换器。处理器是终端API,不允许您继续。另一方面,在使用transformer时,您可以在检查标头后将其作为KStream
继续。例如,这里有一个想法:
input -> input
.transform(new TransformerSupplier<String, String, KeyValue<String, String>>() {
@Override
public Transformer<String, String, KeyValue<String, String>> get() {
return new Transformer<Object, String, KeyValue<Object, String>>() {
ProcessorContext context;
@Override
public void init(ProcessorContext context) {
this.context = context;
}
@Override
public KeyValue<Object, String> transform(Object key, String value) {
// Here you can access the headers using this.context.headers()
return new KeyValue<>(key, value);
}
@Override
public void close() {
}
};
}
})
.map(...)
.groupBy(...)
...
input->input
.transform(新的TransformerSupplier(){
@凌驾
公共变压器get(){
返回新变压器(){
处理上下文上下文;
@凌驾
公共void init(ProcessorContext上下文){
this.context=上下文;
}
@凌驾
公钥值转换(对象键、字符串值){
//在这里,您可以使用此.context.headers()访问标题
返回新的KeyValue(key,value);
}
@凌驾
公众假期结束(){
}
};
}
})
.map(…)
.groupBy(…)
...
查看transform
方法中的注释。在那里,您可以访问每个传入记录的标题
通过查看您的问题,我看到您正在尝试获取传入记录的分区id。为此,可以直接调用context.partition()。我认为你不需要访问标题
这里有一个访问标题的线程。这非常有用。非常感谢你!