Java KStreams:如何获取记录的(原始)主题?

Java KStreams:如何获取记录的(原始)主题?,java,apache-kafka,apache-kafka-streams,Java,Apache Kafka,Apache Kafka Streams,我有以下几点 //配置设置 属性props=//设置 List-topicList=Arrays.asList({“A”、“B”、“C”}); StreamBuilder=新的StreamBuilder(); KStream source=builder.stream(主题列表); 来源 .map((k,v)->{ //我怎样才能在这里找到唱片的主题 }) .to((k,v,r)->{//主题路由的忙代码}); 新的KafkaStream(builder.build(),properties.s

我有以下几点

//配置设置
属性props=//设置
List-topicList=Arrays.asList({“A”、“B”、“C”});
StreamBuilder=新的StreamBuilder();
KStream source=builder.stream(主题列表);
来源
.map((k,v)->{
//我怎样才能在这里找到唱片的主题
})
.to((k,v,r)->{//主题路由的忙代码});
新的KafkaStream(builder.build(),properties.start();

您可以使用所需的主题名称。 要访问ProcessorContext,请使用KStream.process()为其提供适当的实现

您还可以使用KStream.transform():

KStream stream2=stream.transform(新TransformerSupplier(){
@凌驾
公共变压器get(){
返回新变压器(){
私有处理器上下文上下文;
@凌驾
公共void init(ProcessorContext上下文){
this.context=上下文;
}
@凌驾
公钥值转换(InputKeyType键、InputValueType值){
this.context.topic()//需要的主题名称
//这里的逻辑
返回新的KeyValue(OutputKeyType键,OutputValueType值);
}
@凌驾
公众假期结束(){
}
};
}
});
KStream<InputKeyType, InputValueType> stream2 = stream.transform(new TransformerSupplier<InputKeyType, InputValueType, KeyValue<OutputKeyType, OutputValueType>>() {
            @Override
            public Transformer<InputKeyType, InputValueType, KeyValue<OutputKeyType, OutputValueType>> get() {
                return new Transformer<InputKeyType, InputValueType, KeyValue<OutputKeyType, OutputValueType>>() {
                    private ProcessorContext context;

                    @Override
                    public void init(ProcessorContext context) {
                        this.context = context;
                    }

                    @Override
                    public KeyValue<OutputKeyType, OutputValueType> transform(InputKeyType key, InputValueType value) {

                        this.context.topic() // topic name you need
                        // logic here
                        return new KeyValue<>(OutputKeyType key, OutputValueType value);

                    }

                    @Override
                    public void close() {

                    }
                };
            }
        });