Debian Codegraph @

tools/kafka-console-producer/kafka-console-producer.go

raw
1 package main
2
3 import (
4 "flag"
5 "fmt"
6 "io/ioutil"
7 "log"
8 "os"
9 "strings"
10
11 "github.com/Shopify/sarama"
12 "github.com/Shopify/sarama/tools/tls"
13 "github.com/rcrowley/go-metrics"
14 )
15
16 var (
17 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")
18 topic = flag.String("topic", "", "REQUIRED: the topic to produce to")
19 key = flag.String("key", "", "The key of the message to produce. Can be empty.")
20 value = flag.String("value", "", "REQUIRED: the value of the message to produce. You can also provide the value on stdin.")
21 partitioner = flag.String("partitioner", "", "The partitioning scheme to use. Can be `hash`, `manual`, or `random`")
22 partition = flag.Int("partition", -1, "The partition to produce to.")
23 verbose = flag.Bool("verbose", false, "Turn on sarama logging to stderr")
24 showMetrics = flag.Bool("metrics", false, "Output metrics on successful publish to stderr")
25 silent = flag.Bool("silent", false, "Turn off printing the message's topic, partition, and offset to stdout")
26 tlsEnabled = flag.Bool("tls-enabled", false, "Whether to enable TLS")
27 tlsSkipVerify = flag.Bool("tls-skip-verify", false, "Whether skip TLS server cert verification")
28 tlsClientCert = flag.String("tls-client-cert", "", "Client cert for client authentication (use with -tls-enabled and -tls-client-key)")
29 tlsClientKey = flag.String("tls-client-key", "", "Client key for client authentication (use with tls-enabled and -tls-client-cert)")
30
31 logger = log.New(os.Stderr, "", log.LstdFlags)
32 )
33
34 func main() {
35 flag.Parse()
36
37 if *brokerList == "" {
38 printUsageErrorAndExit("no -brokers specified. Alternatively, set the KAFKA_PEERS environment variable")
39 }
40
41 if *topic == "" {
42 printUsageErrorAndExit("no -topic specified")
43 }
44
45 if *verbose {
46 sarama.Logger = logger
47 }
48
49 config := sarama.NewConfig()
50 config.Producer.RequiredAcks = sarama.WaitForAll
51 config.Producer.Return.Successes = true
52
53 if *tlsEnabled {
54 tlsConfig, err := tls.NewConfig(*tlsClientCert, *tlsClientKey)
55 if err != nil {
56 printErrorAndExit(69, "Failed to create TLS config: %s", err)
57 }
58
59 config.Net.TLS.Enable = true
60 config.Net.TLS.Config = tlsConfig
61 config.Net.TLS.Config.InsecureSkipVerify = *tlsSkipVerify
62 }
63
64 switch *partitioner {
65 case "":
66 if *partition >= 0 {
67 config.Producer.Partitioner = sarama.NewManualPartitioner
68 } else {
69 config.Producer.Partitioner = sarama.NewHashPartitioner
70 }
71 case "hash":
72 config.Producer.Partitioner = sarama.NewHashPartitioner
73 case "random":
74 config.Producer.Partitioner = sarama.NewRandomPartitioner
75 case "manual":
76 config.Producer.Partitioner = sarama.NewManualPartitioner
77 if *partition == -1 {
78 printUsageErrorAndExit("-partition is required when partitioning manually")
79 }
80 default:
81 printUsageErrorAndExit(fmt.Sprintf("Partitioner %s not supported.", *partitioner))
82 }
83
84 message := &sarama.ProducerMessage{Topic: *topic, Partition: int32(*partition)}
85
86 if *key != "" {
87 message.Key = sarama.StringEncoder(*key)
88 }
89
90 if *value != "" {
91 message.Value = sarama.StringEncoder(*value)
92 } else if stdinAvailable() {
93 bytes, err := ioutil.ReadAll(os.Stdin)
94 if err != nil {
95 printErrorAndExit(66, "Failed to read data from the standard input: %s", err)
96 }
97 message.Value = sarama.ByteEncoder(bytes)
98 } else {
99 printUsageErrorAndExit("-value is required, or you have to provide the value on stdin")
100 }
101
102 producer, err := sarama.NewSyncProducer(strings.Split(*brokerList, ","), config)
103 if err != nil {
104 printErrorAndExit(69, "Failed to open Kafka producer: %s", err)
105 }
106 defer func() {
107 if err := producer.Close(); err != nil {
108 logger.Println("Failed to close Kafka producer cleanly:", err)
109 }
110 }()
111
112 partition, offset, err := producer.SendMessage(message)
113 if err != nil {
114 printErrorAndExit(69, "Failed to produce message: %s", err)
115 } else if !*silent {
116 fmt.Printf("topic=%s\tpartition=%d\toffset=%d\n", *topic, partition, offset)
117 }
118 if *showMetrics {
119 metrics.WriteOnce(config.MetricRegistry, os.Stderr)
120 }
121 }
122
123 func printErrorAndExit(code int, format string, values ...interface{}) {
124 fmt.Fprintf(os.Stderr, "ERROR: %s\n", fmt.Sprintf(format, values...))
125 fmt.Fprintln(os.Stderr)
126 os.Exit(code)
127 }
128
129 func printUsageErrorAndExit(message string) {
130 fmt.Fprintln(os.Stderr, "ERROR:", message)
131 fmt.Fprintln(os.Stderr)
132 fmt.Fprintln(os.Stderr, "Available command line options:")
133 flag.PrintDefaults()
134 os.Exit(64)
135 }
136
137 func stdinAvailable() bool {
138 stat, _ := os.Stdin.Stat()
139 return (stat.Mode() & os.ModeCharDevice) == 0
140 }