# Kafka的消息key有啥特殊的？

A key/value pair to be sent to Kafka. This consists of a topic name to which the record is being sent, an optional partition number, and an optional key and value.

If a valid partition number is specified that partition will be used when sending the record. If no partition is specified but a key is present a partition will be chosen using a hash of the key. If neither key nor partition is present a partition will be assigned in a round-robin fashion.

The record also has an associated timestamp. If the user did not provide a timestamp, the producer will stamp the record with its current time. The timestamp eventually used by Kafka depends on the timestamp type configured for the topic.

• If the topic is configured to use {@link org.apache.kafka.common.record.TimestampType#CREATE_TIME CreateTime}, the timestamp in the producer record will be used by the broker.

• 如果主题配置为使用 CREATE_TIME，producer 记录中的时间戳将会被broker应用。

• If the topic is configured to use {@link org.apache.kafka.common.record.TimestampType#LOG_APPEND_TIME LogAppendTime}, the timestamp in the producer record will be overwritten by the broker with the broker local time when it appends the message to its log.

• 如果主题配置为使用 LOG_APPEND_TIME，producer记录中的时间戳将会被broker本地时间覆盖掉，覆盖成以broker本地的时间为准。

In either of the cases above, the timestamp that has actually been used will be returned to user in {@link RecordMetadata}

from kafka import KafkaProducer
from kafka.errors import KafkaError

producer = KafkaProducer(bootstrap_servers=['broker1:9091'])

producer.send('my-topic', b'raw_bytes')
producer.send('my-topic', key=b'foo', value=b'bar')
producer.send('my-topic', value=data, partition=0, timestamp_ms='')