简体   繁体   English

每个 kafka 主题的火花流不同值解码器

[英]spark streaming different value decoder per kafka topic

I need to create a Spark streaming that reads from several topic, and uses a different decoder per each topic (each topic contains a different avro-encoded obect):我需要创建一个从多个主题读取的 Spark 流,并为每个主题使用不同的解码器(每个主题包含不同的 avro 编码对象):

def decode_avro(message):
    schem = avro.schema.parse(open("error_list.avsc").read())
    bytes_reader = io.BytesIO(message)
    decoder = avro.io.BinaryDecoder(bytes_reader)
    reader = avro.io.DatumReader(schem)
    return reader.read(decoder)

ssc = StreamingContext(sc, 2)
kvs = KafkaUtils.createDirectStream(ssc, [topic, topic2], {
    "metadata.broker.list": brokers}, valueDecoder = decode_avro)

I wan't to know if it is possible to specify different decoder callbacks per topic, or if it is possible to know the topic name on the decoder function (on this way I could used the topic name for the avro schema file and decode all messages in the same function)我不知道是否可以为每个主题指定不同的解码器回调,或者是否可以知道解码器函数上的主题名称(这样我可以使用 avro 模式文件的主题名称并解码所有同一函数中的消息)

Thank you谢谢

We also have this case where we read from different topics, with different message formats, and then process each topic and store the output into dedicated storage per source topic.我们也有这种情况,我们从不同的主题中读取不同的消息格式,然后处理每个主题并将输出存储到每个源主题的专用存储中。 The right way to go here, is to create multiple streams.正确的方法是创建多个流。 Stream per topic, in the same application, with the same Spark context.在同一个应用程序中使用相同的 Spark 上下文按主题流式传输。 Each stream will get the relevant ValueDecoder, and you may still read from multiple topics if they share the same format.每个流都会获得相关的 ValueDecoder,如果它们共享相同的格式,您仍然可以从多个主题中读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM