简体   繁体   English

Confluent Kafka中Consumer class中密钥的反序列化是什么意思?

[英]What does Deserialization of key mean in Consumer class in Confluent Kafka?

The confluent kafka documentation says, a Consumer class is defined as follows:融合的 kafka 文档说,消费者 class 定义如下:

Class Consumer<TKey, TValue> 

The consumer class above implements a high-level Apache Kafka consumer (with key and value deserialization).上面的消费者 class 实现了高级别的 Apache Kafka 消费者(带有键和值反序列化)。

I understand the TKey and TValue are for deserializing the key, which is sent in from the producer.我知道 TKey 和 TValue 用于反序列化从生产者发送的密钥。 For example, something like例如,像

Sending in a key from the producer would look as从生产者发送密钥看起来像

var deliveryReport = producer.ProduceAsync(topicName, key, val);

Receiving the string key on the consumer end would look as在消费者端接收字符串键看起来像

using (var consumer = new Consumer<Ignore, string>(constructConfig(brokerList, false), null, new StringDeserializer(Encoding.UTF8)))
{
    consumer.Subscribe(topics);

    Console.WriteLine($"Started consumer, Ctrl-C to stop consuming");

    var cancelled = false;
    Console.CancelKeyPress += (_, e) => {
        e.Cancel = true; // prevent the process from terminating.
        cancelled = true;
    };

    while (!cancelled)
    {
        Message<Ignore, string> msg;
        if (!consumer.Consume(out msg, TimeSpan.FromMilliseconds(100)))
        {
            continue;
        }

        Console.WriteLine($"Topic: {msg.Topic} Partition: {msg.Partition} Offset: {msg.Offset} {msg.Value}");
    }
}

Since we are passing in a key, the Consumer is initialized as由于我们传入一个密钥,因此消费者被初始化为

Consumer<Ignore, string>

and the message is initialized as并且消息被初始化为

Message<Ignore, String>

After all that, my question is, what does deserialization of the key really mean ?毕竟,我的问题是,密钥的反序列化到底意味着什么? And why do we need to do that?为什么我们需要这样做? Also, why do we need to pass in a key-value pair Ignore, String for performing deserialization?另外,为什么我们需要传入一个键值对Ignore, String来执行反序列化?

why do we need to pass in a key-value pair Ignore, String for performing deserialization?为什么我们需要传入一个键值对 Ignore, String 来执行反序列化?

You don't need to pass those particular settings.您不需要传递这些特定设置。 You need to match the settings of the producer.您需要匹配生产者的设置。 Or, if you're unsure, you would give byte array object for both key and value.或者,如果您不确定,您可以为键和值提供字节数组 object。

If the producer didn't send a key, such as null, there is nothing to deserialize.如果生产者没有发送密钥,例如 null,则没有什么可以反序列化。 I assume that's what the Ignore class is for.我认为这就是 Ignore class 的用途。 Notice you didn't provide a key Deserializer class, but did for the value请注意,您没有提供密钥解串器 class,但为值提供了

null, new StringDeserializer(Encoding.UTF8))

All Kafka messages contain key, value pairs only as bytes.所有 Kafka 消息都包含键值对,仅作为字节。 The Producers use serializers, and as a consumer, you need to deserialize.生产者使用序列化器,作为消费者,您需要反序列化。 Ideally, you deserialize messages into actual objects, such as strings or JSON objects or Avro, Protobuf, etc. whatever.理想情况下,您将消息反序列化为实际对象,例如字符串或 JSON 对象或 Avro、Protobuf 等。

By default, the keys are what determines what partitions of a topic the messages you'll be consuming from originated.默认情况下,键决定了您将从哪个主题分区中消费消息。 A null key will be equally distributed across the topic. null 密钥将平均分布在主题中。 Otherwise, the producer application can define their own partitioners and send data wherever their logic decides否则,生产者应用程序可以定义自己的分区器并在其逻辑决定的任何地方发送数据

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

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