Class ProducerRecord<K,V>
- java.lang.Object
-
- org.apache.kafka.clients.producer.ProducerRecord<K,V>
-
public class ProducerRecord<K,V> extends java.lang.Object
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
CreateTime
, the timestamp in the producer record will be used by the broker.- If the topic is configured to use
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.In either of the cases above, the timestamp that has actually been used will be returned to user in
RecordMetadata
- If the topic is configured to use
-
-
Constructor Summary
Constructors Constructor Description ProducerRecord(java.lang.String topic, java.lang.Integer partition, java.lang.Long timestamp, K key, V value)
Creates a record with a specified timestamp to be sent to a specified topic and partitionProducerRecord(java.lang.String topic, java.lang.Integer partition, java.lang.Long timestamp, K key, V value, java.lang.Iterable<Header> headers)
Creates a record with a specified timestamp to be sent to a specified topic and partitionProducerRecord(java.lang.String topic, java.lang.Integer partition, K key, V value)
Creates a record to be sent to a specified topic and partitionProducerRecord(java.lang.String topic, java.lang.Integer partition, K key, V value, java.lang.Iterable<Header> headers)
Creates a record to be sent to a specified topic and partitionProducerRecord(java.lang.String topic, K key, V value)
Create a record to be sent to KafkaProducerRecord(java.lang.String topic, V value)
Create a record with no key
-
-
-
Constructor Detail
-
ProducerRecord
public ProducerRecord(java.lang.String topic, java.lang.Integer partition, java.lang.Long timestamp, K key, V value, java.lang.Iterable<Header> headers)
Creates a record with a specified timestamp to be sent to a specified topic and partition- Parameters:
topic
- The topic the record will be appended topartition
- The partition to which the record should be senttimestamp
- The timestamp of the record, in milliseconds since epoch. If null, the producer will assign the timestamp using System.currentTimeMillis().key
- The key that will be included in the recordvalue
- The record contentsheaders
- the headers that will be included in the record
-
ProducerRecord
public ProducerRecord(java.lang.String topic, java.lang.Integer partition, java.lang.Long timestamp, K key, V value)
Creates a record with a specified timestamp to be sent to a specified topic and partition- Parameters:
topic
- The topic the record will be appended topartition
- The partition to which the record should be senttimestamp
- The timestamp of the record, in milliseconds since epoch. If null, the producer will assign the timestamp using System.currentTimeMillis().key
- The key that will be included in the recordvalue
- The record contents
-
ProducerRecord
public ProducerRecord(java.lang.String topic, java.lang.Integer partition, K key, V value, java.lang.Iterable<Header> headers)
Creates a record to be sent to a specified topic and partition- Parameters:
topic
- The topic the record will be appended topartition
- The partition to which the record should be sentkey
- The key that will be included in the recordvalue
- The record contentsheaders
- The headers that will be included in the record
-
ProducerRecord
public ProducerRecord(java.lang.String topic, java.lang.Integer partition, K key, V value)
Creates a record to be sent to a specified topic and partition- Parameters:
topic
- The topic the record will be appended topartition
- The partition to which the record should be sentkey
- The key that will be included in the recordvalue
- The record contents
-
ProducerRecord
public ProducerRecord(java.lang.String topic, K key, V value)
Create a record to be sent to Kafka- Parameters:
topic
- The topic the record will be appended tokey
- The key that will be included in the recordvalue
- The record contents
-
ProducerRecord
public ProducerRecord(java.lang.String topic, V value)
Create a record with no key- Parameters:
topic
- The topic this record should be sent tovalue
- The record contents
-
-
Method Detail
-
topic
public java.lang.String topic()
- Returns:
- The topic this record is being sent to
-
headers
public Headers headers()
- Returns:
- The headers
-
key
public K key()
- Returns:
- The key (or null if no key is specified)
-
value
public V value()
- Returns:
- The value
-
timestamp
public java.lang.Long timestamp()
- Returns:
- The timestamp, which is in milliseconds since epoch.
-
partition
public java.lang.Integer partition()
- Returns:
- The partition to which the record will be sent (or null if no partition was specified)
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
-