-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtopic_echo.go
47 lines (37 loc) · 1.07 KB
/
topic_echo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"flag"
"fmt"
"gopkg.in/confluentinc/confluent-kafka-go.v1/kafka"
)
func main() {
var topicName string
flag.StringVar(&topicName, "t", "movies", "topic")
flag.Parse()
c, err := kafka.NewConsumer(&kafka.ConfigMap{
"bootstrap.servers": "localhost:29092",
"group.id": "myGroup",
"auto.offset.reset": "earliest",
})
if err != nil {
panic(err)
}
fmt.Println("Subscribe Topic: " + topicName)
c.SubscribeTopics([]string{topicName, "^aRegex.*[Tt]opic"}, nil)
for {
msg, err := c.ReadMessage(-1)
if err == nil {
fmt.Printf("Message on %s: ", msg.TopicPartition)
fmt.Printf("\t headers-> %v\n", msg.Headers)
fmt.Printf("\t Key -> %s\n", string(msg.Key))
fmt.Printf("\t Value -> %s\n", string(msg.Value))
fmt.Printf("\t Timestamp -> %v\n", msg.Timestamp)
fmt.Printf("\t TimestampType -> %v\n", msg.TimestampType)
fmt.Printf("\t Opaque -> %v\n", msg.Opaque)
} else {
// The client will automatically try to recover from all errors.
fmt.Printf("Consumer error: %v (%v)\n", err, msg)
}
}
c.Close()
}