Debian Codegraph @

tools/kafka-console-partitionconsumer/kafka-console-partitionconsumer.go

raw
1 package main
2
3 import (
4 "flag"
5 "fmt"
6 "log"
7 "os"
8 "os/signal"
9 "strconv"
10 "strings"
11
12 "github.com/Shopify/sarama"
13 )
14
15 var (
16 brokerList = flag.String("brokers", os.Getenv("KAFKA_PEERS"), "The comma separated list of brokers in the Kafka cluster")
17 topic = flag.String("topic", "", "REQUIRED: the topic to consume")
18 partition = flag.Int("partition", -1, "REQUIRED: the partition to consume")
19 offset = flag.String("offset", "newest", "The offset to start with. Can be `oldest`, `newest`, or an actual offset")
20 verbose = flag.Bool("verbose", false, "Whether to turn on sarama logging")
21
22 logger = log.New(os.Stderr, "", log.LstdFlags)
23 )
24
25 func main() {
26 flag.Parse()
27
28 if *brokerList == "" {
29 printUsageErrorAndExit("You have to provide -brokers as a comma-separated list, or set the KAFKA_PEERS environment variable.")
30 }
31
32 if *topic == "" {
33 printUsageErrorAndExit("-topic is required")
34 }
35
36 if *partition == -1 {
37 printUsageErrorAndExit("-partition is required")
38 }
39
40 if *verbose {
41 sarama.Logger = logger
42 }
43
44 var (
45 initialOffset int64
46 offsetError error
47 )
48 switch *offset {
49 case "oldest":
50 initialOffset = sarama.OffsetOldest
51 case "newest":
52 initialOffset = sarama.OffsetNewest
53 default:
54 initialOffset, offsetError = strconv.ParseInt(*offset, 10, 64)
55 }
56
57 if offsetError != nil {
58 printUsageErrorAndExit("Invalid initial offset: %s", *offset)
59 }
60
61 c, err := sarama.NewConsumer(strings.Split(*brokerList, ","), nil)
62 if err != nil {
63 printErrorAndExit(69, "Failed to start consumer: %s", err)
64 }
65
66 pc, err := c.ConsumePartition(*topic, int32(*partition), initialOffset)
67 if err != nil {
68 printErrorAndExit(69, "Failed to start partition consumer: %s", err)
69 }
70
71 go func() {
72 signals := make(chan os.Signal, 1)
73 signal.Notify(signals, os.Kill, os.Interrupt)
74 <-signals
75 pc.AsyncClose()
76 }()
77
78 for msg := range pc.Messages() {
79 fmt.Printf("Offset:\t%d\n", msg.Offset)
80 fmt.Printf("Key:\t%s\n", string(msg.Key))
81 fmt.Printf("Value:\t%s\n", string(msg.Value))
82 fmt.Println()
83 }
84
85 if err := c.Close(); err != nil {
86 logger.Println("Failed to close consumer: ", err)
87 }
88 }
89
90 func printErrorAndExit(code int, format string, values ...interface{}) {
91 fmt.Fprintf(os.Stderr, "ERROR: %s\n", fmt.Sprintf(format, values...))
92 fmt.Fprintln(os.Stderr)
93 os.Exit(code)
94 }
95
96 func printUsageErrorAndExit(format string, values ...interface{}) {
97 fmt.Fprintf(os.Stderr, "ERROR: %s\n", fmt.Sprintf(format, values...))
98 fmt.Fprintln(os.Stderr)
99 fmt.Fprintln(os.Stderr, "Available command line options:")
100 flag.PrintDefaults()
101 os.Exit(64)
102 }