-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
131 lines (104 loc) · 3.02 KB
/
main.go
File metadata and controls
131 lines (104 loc) · 3.02 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// kafka-cli project main.go
package main
import (
"crypto/sha1"
"encoding/hex"
"fmt"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/confluentinc/confluent-kafka-go/kafka"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
func hash_group(t time.Time) string {
const Size = 7
hash := sha1.New()
hash.Write([]byte(t.String()))
group_hash := hex.EncodeToString(hash.Sum(nil))
return group_hash[:7]
}
var (
offset = kingpin.Flag("offset", "Offset where to start from (auto.offset.reset).").Default("earliest").Short('o').String()
broker = kingpin.Flag("broker", "Which broker to poll the messages from.").Default("localhost:29092").Short('b').String()
group = kingpin.Flag("group", "Consumer Group to register. Will be defaulted randomly.").Short('g').Default(hash_group(time.Now())).String()
topic = kingpin.Flag("topic", "Which topic should be used.").Short('T').Required().String()
tseconds = kingpin.Flag("timeout", "Session Timeout in seconds.").Default("6").Short('t').String()
verbose = kingpin.Flag("verbose", "Verbose mode.").Short('v').Bool()
tail = kingpin.Flag("tail", "Stay connected to the topic to consume messages.").Short('f').Bool()
)
func main() {
kingpin.Parse()
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, syscall.SIGINT, syscall.SIGTERM)
timeout, _ := strconv.Atoi(*tseconds)
c, err := kafka.NewConsumer(&kafka.ConfigMap{
"bootstrap.servers": *broker,
"group.id": *group,
"auto.offset.reset": *offset,
"session.timeout.ms": timeout * 1000,
"broker.address.family": "v4",
})
if err != nil {
fmt.Fprintf(os.Stderr,
"Failed to create a consumer to broker: %s with group: s% and topic: %s.\n",
*broker, *group, *topic)
if *verbose {
fmt.Fprintln(os.Stderr, "%s", err)
}
os.Exit(1)
}
fmt.Printf("Successful created consumer: %v.\n", c)
defer func() {
fmt.Println("Closing consumer.")
c.Close()
}()
err = c.SubscribeTopics([]string{*topic, "^aRegex.*[Tt]opic"}, nil)
if err != nil {
fmt.Fprintln(os.Stderr,
"Failed to subscribe topic %s.", *topic)
if *verbose {
fmt.Fprintln(os.Stderr, "%s", err)
}
os.Exit(1)
}
run := true
if *verbose {
fmt.Fprintf(os.Stdout, "Subscribed topic: %s with group: %s.\n", *topic, *group)
}
for run == true {
select {
case sig := <-sigchan:
fmt.Printf("Caught signal %v: terminating.\n", sig)
run = false
default:
event := c.Poll(100)
if event == nil {
if *tail {
continue
} else {
fmt.Printf("No messages to consume found, terminating.\n")
run = false
}
}
switch e := event.(type) {
case *kafka.Message:
fmt.Printf("Recieved message on %s:\n%s.\n",
e.TopicPartition, string(e.Value))
if e.Headers != nil && *verbose == true {
fmt.Printf("With headers: %v.\n", e.Headers)
}
case kafka.Error:
fmt.Fprintf(os.Stderr, "Error: %v: %v.\n", e.Code(), e)
if e.Code() == kafka.ErrAllBrokersDown {
run = false
}
case nil:
continue
default:
fmt.Printf("Ignored %v\n", e)
}
}
}
}