php-rdkafka icon indicating copy to clipboard operation
php-rdkafka copied to clipboard

Specify data type for key when producing a message

Open canique opened this issue 3 years ago • 1 comments

  • 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?

canique avatar Aug 29 '21 14:08 canique

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.

arnaud-lb avatar Nov 15 '21 12:11 arnaud-lb