Modern C++ Kafka API
ProducerRecord.h
1 #pragma once
2 
3 #include <kafka/Project.h>
4 
5 #include <kafka/Header.h>
6 #include <kafka/Types.h>
7 
8 #include <librdkafka/rdkafka.h>
9 
10 
11 namespace KAFKA_API { namespace clients { namespace producer {
12 
19 {
20 public:
21  using Id = std::uint64_t;
22 
23  ProducerRecord(Topic topic, Partition partition, const Key& key, const Value& value)
24  : _topic(std::move(topic)), _partition(partition), _key(key), _value(value) {}
25 
26  ProducerRecord(const Topic& topic, Partition partition, const Key& key, const Value& value, Id id)
27  : ProducerRecord(topic, partition, key, value) { _id = id; }
28 
29  ProducerRecord(const Topic& topic, const Key& key, const Value& value)
30  : ProducerRecord(topic, RD_KAFKA_PARTITION_UA, key, value) {}
31 
32  ProducerRecord(const Topic& topic, const Key& key, const Value& value, Id id)
33  : ProducerRecord(topic, key, value) { _id = id; }
34 
38  const Topic& topic() const { return _topic; }
39 
43  Partition partition() const { return _partition; }
44 
48  Key key() const { return _key; }
49 
53  Value value() const { return _value; }
54 
58  Optional<Id> id() const { return _id; }
59 
63  const Headers& headers() const { return _headers; }
64 
69  Headers& headers() { return _headers; }
70 
74  void setPartition(Partition partition) { _partition = partition; }
75 
79  void setKey(const Key& key) { _key = key; }
80 
84  void setValue(const Value& value) { _value = value; }
85 
89  void setId(Id id) { _id = id; }
90 
91  std::string toString() const
92  {
93  return _topic + "-" + (_partition == RD_KAFKA_PARTITION_UA ? "NA" : std::to_string(_partition)) + std::string(":")
94  + (_id ? (std::to_string(*_id) + std::string(", ")) : " ")
95  + (_headers.empty() ? "" : ("headers[" + KAFKA_API::toString(_headers) + "], "))
96  + _key.toString() + std::string("/") + _value.toString();
97  }
98 
99 private:
100  Topic _topic;
101  Partition _partition;
102  Key _key;
103  Value _value;
104  Headers _headers;
105  Optional<Id> _id;
106 };
107 
108 } } } // end of KAFKA_API::clients::producer
109 
A key/value pair to be sent to Kafka.
Definition: ProducerRecord.h:19
Key key() const
The key (or null if no key is specified).
Definition: ProducerRecord.h:48
Partition partition() const
The partition to which the record will be sent (or UNKNOWN_PARTITION if no partition was specified).
Definition: ProducerRecord.h:43
void setKey(const Key &key)
Set the key.
Definition: ProducerRecord.h:79
Headers & headers()
The headers.
Definition: ProducerRecord.h:69
const Topic & topic() const
The topic this record is being sent to.
Definition: ProducerRecord.h:38
void setPartition(Partition partition)
Set the partition.
Definition: ProducerRecord.h:74
void setId(Id id)
Set the record id.
Definition: ProducerRecord.h:89
void setValue(const Value &value)
Set the value.
Definition: ProducerRecord.h:84
Optional< Id > id() const
The id to identify the message (consistent with Producer::Metadata::recordId()).
Definition: ProducerRecord.h:58
Value value() const
The value.
Definition: ProducerRecord.h:53
const Headers & headers() const
The headers.
Definition: ProducerRecord.h:63