kafka-console-partitionconsumer.go 2.5 KB

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