使用kafka流的Topic1到Topic2

时间:2019-03-14 14:08:04

标签: apache-kafka apache-kafka-streams kafka-topic

我是kafka stream的新手,我想阅读一个主题,并使用kafka stream api在新主题中写一部分内容。 我的密钥是字符串,值是Avro 有没有可以使用的文档/示例?

编辑:

    final StreamsBuilder builder = new StreamsBuilder();
    final KStream<String, GenericRecord> inputStream = builder.stream("Test_CX_TEST_KAFKA_X");
    final KStream<String, String> newStream = inputStream.mapValues(value -> value.get("ID").toString());
    newStream.to("SUB_TOPIC",Produced.with(Serdes.String(),Serdes.String()));
    final KafkaStreams streams = new KafkaStreams(builder.build(), streamsConfiguration);
    streams.start();

在SUB_TOPIC中,我有:

键: {   “ ID”:“ 145” } 时间戳记: 2019年3月14日17:52:23.43 偏移量: 12 划分: 0

我的输入主题:

  

{     “ ID”:“ 145”,     “ TIMESTAMP”:1552585938545,     “ WEEK”:“ \ u0000”,     “资源”: {       “ string”:“ TMP”     },     “身体”: {       “ string”:“ {\” operation_type \“:\” INSERT \“,\” old \“:{\” ROW_ID \“:null,\” LAST_UPD \“:null,\” DENOMINATION \“:null,\ “ SIREN_SIRET \”:null},\“ new \”:{\“ ROW_ID \”:\“ 170309-******** \”,\“ LAST_UPD \”:\“ 2019-03-14T17: 52:18 \“,\” DENOMINATION \“:\” 1-****** \“,\” SIREN_SIRET \“:null}}”     },     “ TYPE_ACTION”:{       “ string”:“ INSERT”     }   }

如何在新主题的“正文”中添加其他字段? 例子:

  

{     “ ID”:“ 145”,     “ TIMESTAMP”:1552585938545,     “ WEEK”:“ \ u0000”,     “资源”: {       “ string”:“ TMP”     },     “身体”: {       “ string”:“ {\” operation_type \“:\” INSERT \“,\” old \“:{\” ROW_ID \“:null,\” LAST_UPD \“:null},\” new \“:{\ “ ROW_ID \”:\“ 170309-******** \”,\“ LAST_UPD \”:\“ 2019-03-14T17:52:18 \”}}“     },     “ TYPE_ACTION”:{       “ string”:“ INSERT”     }   }

1 个答案:

答案 0 :(得分:1)

您可以简单地将主题用作流,并使用.map()/。mapValues()函数修改值/键值。

示例:假设您要从avro记录中选择一列并发布到新的输出主题。

// If you are using Schema registry, make sure to add the schema registry url 
// in streamConfiguration. Also specify the AvroSerde for VALUE_SERDE

final Properties streamsConfiguration = new Properties();
streamsConfiguration.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, schemaRegistryUrl);
streamsConfiguration.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
streamsConfiguration.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, GenericAvroSerde.class);
final StreamsBuilder builder = new StreamsBuilder();
final KStream<String, GenericRecord> inputStream = builder.stream("inputTopic");
final KStream<String, String> newStream = userProfiles.mapValues(value -> value.get("fieldName").toString());
subStream.to("outputTopic",Produced.with(Serdes.String(),Serdes.String());
final KafkaStreams streams = new KafkaStreams(builder.build(), streamsConfiguration);

此外,您可以查看github上的示例:
https://github.com/confluentinc/kafka-streams-examples/blob/5.1.2-post/src/main/java/io/confluent/examples/streams/WikipediaFeedAvroExample.java