forked from bsm/sarama-cluster
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexample_test.go
More file actions
51 lines (41 loc) · 1.69 KB
/
example_test.go
File metadata and controls
51 lines (41 loc) · 1.69 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
/*
A simple kafka consumer-group client
Copyright 2016 MistSys
*/
package consumer_test
import (
"fmt"
"time"
"github.com/Shopify/sarama"
consumer "github.com/mistsys/sarama-consumer"
"github.com/mistsys/sarama-consumer/offsets"
"github.com/mistsys/sarama-consumer/stable"
)
func ExampleNewClient() {
// create a suitable sarama.Client
sconfig := sarama.NewConfig()
sconfig.Version = consumer.MinVersion // consumer requires at least 0.9
sconfig.Consumer.Return.Errors = true // needed if asynchronous ErrOffsetOutOfRange handling is desired (it's a good idea)
sclient, _ := sarama.NewClient([]string{"kafka-broker:9092"}, sconfig)
// from that, create a consumer.Config with some fancy options
config := consumer.NewConfig()
config.Partitioner = stable.New(false) // use a stable (but inconsistent) partitioner
config.StartingOffset, config.OffsetOutOfRange = offsets.NoOlderThan(time.Second * 30) // always start and restart no more than 30 seconds in the past (NOTE: requires kafka 0.10 brokers to work properly)
// and finally a consumer Client
client, _ := consumer.NewClient("group_name", config, sclient)
defer client.Close() // not strictly necessary, since we don't exit, but this is example code and someone might C&V it and exit
// consume and print errors
go func() {
for err := range client.Errors() {
fmt.Println(err)
}
}()
// consume a topic
topic_consumer, _ := client.Consume("topic1")
defer topic_consumer.AsyncClose() // same comment as for client.Close() above
// process messages
for msg := range topic_consumer.Messages() {
fmt.Println("processing message", msg)
topic_consumer.Done(msg) // required
}
}