Skip to content

Commit

Permalink
Merge pull request IBM#1549 from TonPC64/master
Browse files Browse the repository at this point in the history
Support headers in tools kafka-console-producer
  • Loading branch information
d1egoaz authored Jan 21, 2020
2 parents 7a7d874 + be7de1e commit ae8f056
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tools/kafka-console-producer/kafka-console-producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

var (
brokerList = flag.String("brokers", os.Getenv("KAFKA_PEERS"), "The comma separated list of brokers in the Kafka cluster. You can also set the KAFKA_PEERS environment variable")
headers = flag.String("headers", "", "The headers of the message to produce. Example: -headers=foo:bar,bar:foo")
topic = flag.String("topic", "", "REQUIRED: the topic to produce to")
key = flag.String("key", "", "The key of the message to produce. Can be empty.")
value = flag.String("value", "", "REQUIRED: the value of the message to produce. You can also provide the value on stdin.")
Expand Down Expand Up @@ -99,6 +100,25 @@ func main() {
printUsageErrorAndExit("-value is required, or you have to provide the value on stdin")
}

if *headers != "" {
hdrs := []sarama.RecordHeader{}
arrHdrs := strings.Split(*headers, ",")
for _, h := range arrHdrs {
if header := strings.Split(h, ":"); len(header) != 2 {
printUsageErrorAndExit("-header should be key:value. Example: -headers=foo:bar,bar:foo")
} else {
hdrs = append(hdrs, sarama.RecordHeader{
Key: []byte(header[0]),
Value: []byte(header[1]),
})
}
}

if len(hdrs) != 0 {
message.Headers = hdrs
}
}

producer, err := sarama.NewSyncProducer(strings.Split(*brokerList, ","), config)
if err != nil {
printErrorAndExit(69, "Failed to open Kafka producer: %s", err)
Expand Down

0 comments on commit ae8f056

Please sign in to comment.