php-rdkafka
php-rdkafka copied to clipboard
Specify data type for key when producing a message
- PHP version: 7.4
- librdkafka version: 0.11.6
- php-rdkafka version:
- kafka version:
When producing a record like this:
$topic->produce(RD_KAFKA_PARTITION_UA, 0, json_encode($payload), intval($recordId));
I'd need the key ($recordId) to be an 8 byte long value. I think it is treated as string? Is there a way to tell the library to produce a long?
Hi @canique
I'm not sure that Kafka can store different key types: I believe that keys are just byte arrays.
If you pass an int to ->produce()
, it will be converted to its decimal representation as a string. You can convert that back with intval($key)
when consuming messages.
If you prefer an 8 byte long value, you can use pack('J', $key)
. You can convert that back with unpack('J', $key)
when consuming messages.