kafka-console-partitionconsumer.go 2.5 KB

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