-
Notifications
You must be signed in to change notification settings - Fork 6
/
kafka2.go
69 lines (50 loc) · 1.25 KB
/
kafka2.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"os"
"os/signal"
cluster "github.com/bsm/sarama-cluster"
"github.com/Shopify/sarama"
"time"
)
func main() {
// init (custom) config, enable errors and notifications
config := cluster.NewConfig()
config.Consumer.Return.Errors = true
config.Group.Return.Notifications = true
// init consumer
brokers := []string{"127.0.0.1:9092", "127.0.0.1:9093", "127.0.0.1:9094"}
//topics := []string{"wing-binlog-event"}
c,_:=cluster.NewClient(brokers,config)
bb := c.Brokers()
fmt.Printf("Brokers: %+v\n", bb)
for _, v:=range bb {
fmt.Printf("Broker: %+v\n", *v)
}
retention := "-1"
req := &sarama.CreateTopicsRequest{
//Version:sarama.V1_1_0_0,
TopicDetails: map[string]*sarama.TopicDetail{
"testtopic": {
NumPartitions: 3,
ReplicationFactor: 3,
ReplicaAssignment: map[int32][]int32{
0: []int32{0, 1, 2},
},
ConfigEntries: map[string]*string{
"retention.ms": &retention,
},
},
},
Timeout: 100 * time.Millisecond,
}
bb[0].Open(nil)
r, e := bb[0].CreateTopics(req)
fmt.Printf("%+v, %+v\n", r, e)
tt, _ := c.Topics()
fmt.Printf("Topics: %+v\n", tt)
// trap SIGINT to trigger a shutdown.
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
<-signals
}