-
Notifications
You must be signed in to change notification settings - Fork 792
/
example_consumergroup_test.go
94 lines (84 loc) · 2.22 KB
/
example_consumergroup_test.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
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
package kafka_test
import (
"context"
"errors"
"fmt"
"os"
"github.com/segmentio/kafka-go"
)
func ExampleGeneration_Start_consumerGroupParallelReaders() {
group, err := kafka.NewConsumerGroup(kafka.ConsumerGroupConfig{
ID: "my-group",
Brokers: []string{"kafka:9092"},
Topics: []string{"my-topic"},
})
if err != nil {
fmt.Printf("error creating consumer group: %+v\n", err)
os.Exit(1)
}
defer group.Close()
for {
gen, err := group.Next(context.TODO())
if err != nil {
break
}
assignments := gen.Assignments["my-topic"]
for _, assignment := range assignments {
partition, offset := assignment.ID, assignment.Offset
gen.Start(func(ctx context.Context) {
// create reader for this partition.
reader := kafka.NewReader(kafka.ReaderConfig{
Brokers: []string{"127.0.0.1:9092"},
Topic: "my-topic",
Partition: partition,
})
defer reader.Close()
// seek to the last committed offset for this partition.
reader.SetOffset(offset)
for {
msg, err := reader.ReadMessage(ctx)
if err != nil {
if errors.Is(err, kafka.ErrGenerationEnded) {
// generation has ended. commit offsets. in a real app,
// offsets would be committed periodically.
gen.CommitOffsets(map[string]map[int]int64{"my-topic": {partition: offset + 1}})
return
}
fmt.Printf("error reading message: %+v\n", err)
return
}
fmt.Printf("received message %s/%d/%d : %s\n", msg.Topic, msg.Partition, msg.Offset, string(msg.Value))
offset = msg.Offset
}
})
}
}
}
func ExampleGeneration_CommitOffsets_overwriteOffsets() {
group, err := kafka.NewConsumerGroup(kafka.ConsumerGroupConfig{
ID: "my-group",
Brokers: []string{"kafka:9092"},
Topics: []string{"my-topic"},
})
if err != nil {
fmt.Printf("error creating consumer group: %+v\n", err)
os.Exit(1)
}
defer group.Close()
gen, err := group.Next(context.TODO())
if err != nil {
fmt.Printf("error getting next generation: %+v\n", err)
os.Exit(1)
}
err = gen.CommitOffsets(map[string]map[int]int64{
"my-topic": {
0: 123,
1: 456,
3: 789,
},
})
if err != nil {
fmt.Printf("error committing offsets next generation: %+v\n", err)
os.Exit(1)
}
}