简体   繁体   English

Kafka中的LongSerializer vs ByteArraySerializer

[英]LongSerializer vs ByteArraySerializer in kafka

I am a new to Kafka and trying to understand best way to use few things. 我是Kafka的新手,正在尝试了解使用少量事物的最佳方法。 I have a topic which will have 10 partitions and I am sending data to it using kafka producer. 我有一个主题,它将有10个分区,我正在使用kafka生产者向其发送数据。 My key will be client_id which is Long data type and value will be byte array. 我的密钥将是client_id ,它是Long数据类型,值将是字节数组。 So should I use LongSerializer for my key serializer (key.serializer) or I should convert my client_id to byte array in my code itself and then use ByteArraySerializer for key.serializer instead? 因此,我应该将LongSerializer用于密钥序列化程序(key.serializer)还是应该将我的client_id转换为代码本身的字节数组,然后将ByteArraySerializer用于key.serializer

So question is what is the difference between directly using LongSerializer vs using ByteArraySerializer . 因此,问题是直接使用LongSerializer与使用ByteArraySerializer有什么区别? In both the case, it converts long to byte array? 在两种情况下,它都将long转换为字节数组吗?

I am running Kafka 0.10.0.0 version. 我正在运行Kafka 0.10.0.0版本。

LongSerializer's serialize method only accepts a Long/long type parameter, which converts the long-typed integer into a byte array internally. LongSerializer的serialize方法仅接受Long / long类型的参数,该参数在内部将long型整数转换为字节数组。 If you use ByteArraySerializer, you have to do this conversion before passing around to ByteArraySerializer. 如果使用ByteArraySerializer,则必须先进行此转换,然后再传递给ByteArraySerializer。

The different is that the send() on the producer takes a ProducerRecord with a Long vs a byte array. 不同之处在于生产者上的send()接受一个带有Long与字节数组的ProducerRecord。 You're correct in that at some level it's all the same thing, but usually you like the ProducerRecord types to match what your user's code is using without additional conversions. 您在某种程度上是对的,这是完全正确的,但是通常您希望ProducerRecord类型与用户代码使用的类型匹配,而无需进行其他转换。

My key will be client_id which is Long data type and value will be byte array 我的密钥是client_id,它是Long数据类型,值是字节数组

It is clear, that for key you should be using LongSerializer and for value ByteArraySerializer 很明显,对于键,您应该使用LongSerializer,对于值ByteArraySerializer

LongSerializer vs using ByteArraySerializer LongSerializer与使用ByteArraySerializer

As said, in the above answers, any object can (and will) be serialized to a byte[] , including your Long object. 如上所述,在以上答案中,任何对象都可以(并且将)序列化为一个byte[] ,包括您的Long对象。 So, even the LongSerializer internally does that only. 因此,即使LongSerializer内部也只能这样做。

public class LongSerializer implements Serializer<Long> {

    public void configure(Map<String, ?> configs, boolean isKey) {
        // nothing to do
    }

    public byte[] serialize(String topic, Long data) {
        if (data == null)
            return null;

        return new byte[] {
            (byte) (data >>> 56),
            (byte) (data >>> 48),
            (byte) (data >>> 40),
            (byte) (data >>> 32),
            (byte) (data >>> 24),
            (byte) (data >>> 16),
            (byte) (data >>> 8),
            data.byteValue()
        };
    }

    public void close() {
        // nothing to do
    }
}

So, better go with LongSerializer for you key (so that you need not worry about conversion). 因此,最好为您的密钥使用LongSerializer(这样就不必担心转换了)。 Why re-invent the wheel? 为什么要重新发明轮子?

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

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