forked from dangkaka/go-kafka-avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avroConsumer.go
166 lines (137 loc) · 4.37 KB
/
avroConsumer.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package kafka
import (
"context"
"encoding/binary"
"errors"
"github.com/Shopify/sarama"
"github.com/linkedin/goavro"
)
type avroConsumer struct {
SchemaRegistryClient *CachedSchemaRegistryClient
callbacks ConsumerCallbacks
ready chan bool
cleanup chan bool
consumerGroup sarama.ConsumerGroup
meta string
}
type ConsumerCallbacks struct {
OnDataReceived func(msg Message) bool // return true to continue
OnError func(err error) bool // return true to continue on error
}
type Message struct {
SchemaId int
Topic string
Partition int32
Offset int64
Key string
Value string
}
// avroConsumer is a basic consumer to interact with schema registry, avro and kafka
func NewAvroConsumer(kafkaServers []string, kafkaVersion sarama.KafkaVersion, schemaRegistryServers []string,
topic string, groupId string, callbacks ConsumerCallbacks, meta string) (*avroConsumer, error) {
// // init (custom) config, enable errors and notifications
// config := cluster.NewConfig()
// config.Consumer.Return.Errors = true
// config.Group.Return.Notifications = true
// //read from beginning at the first time
// config.Consumer.Offsets.Initial = sarama.OffsetOldest
// topics := []string{topic}
// consumer, err := cluster.NewConsumer(kafkaServers, groupId, topics, config)
// if err != nil {
// return nil, err
// }
config := sarama.NewConfig()
config.Version = kafkaVersion
config.Consumer.Offsets.Initial = sarama.OffsetOldest
schemaRegistryClient := NewCachedSchemaRegistryClient(schemaRegistryServers)
consumer := &avroConsumer{
SchemaRegistryClient: schemaRegistryClient,
callbacks: callbacks,
ready: make(chan bool, 0),
cleanup: make(chan bool, 0),
meta: meta,
}
ctx := context.Background()
consumerGroup, err := sarama.NewConsumerGroup(kafkaServers, groupId, config)
if err != nil {
return nil, err
}
consumer.consumerGroup = consumerGroup
go func() {
for {
err := consumerGroup.Consume(ctx, []string{topic}, consumer)
if err != nil {
panic(err)
}
}
}()
<-consumer.ready // Await till the consumer has been set up
return consumer, nil
}
//GetSchemaId get schema id from schema-registry service
func (ac *avroConsumer) GetSchema(id int) (*goavro.Codec, error) {
codec, err := ac.SchemaRegistryClient.GetSchema(id)
if err != nil {
return nil, err
}
return codec, nil
}
// Setup is run at the beginning of a new session, before ConsumeClaim
func (ac *avroConsumer) Setup(sarama.ConsumerGroupSession) error {
// Mark the consumer as ready
close(ac.ready)
return nil
}
// Cleanup is run at the end of a session, once all ConsumeClaim goroutines have exited
func (ac *avroConsumer) Cleanup(sarama.ConsumerGroupSession) error {
close(ac.cleanup)
return nil
}
// ConsumeClaim must start a consumer loop of ConsumerGroupClaim's Messages().
func (ac *avroConsumer) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
//log.Printf("%v consuming from %v[%v]", ac.meta, claim.Topic(), claim.Partition())
// NOTE:
// Do not move the code below to a goroutine.
// The `ConsumeClaim` itself is called within a goroutine, see:
// https://github.com/Shopify/sarama/blob/master/consumer_group.go#L27-L29
for message := range claim.Messages() {
msg, err := ac.ProcessAvroMsg(message)
if err != nil {
if !ac.callbacks.OnError(err) {
return err
}
}
if ac.callbacks.OnDataReceived != nil {
if !ac.callbacks.OnDataReceived(msg) {
return errors.New("OnDataReceived decided to abort")
}
}
session.MarkMessage(message, "")
}
return nil
}
func (ac *avroConsumer) ProcessAvroMsg(m *sarama.ConsumerMessage) (Message, error) {
schemaId := binary.BigEndian.Uint32(m.Value[1:5])
codec, err := ac.GetSchema(int(schemaId))
if err != nil {
return Message{}, err
}
// Convert binary Avro data back to native Go form
native, _, err := codec.NativeFromBinary(m.Value[5:])
if err != nil {
return Message{}, err
}
// Convert native Go form to textual Avro data
textual, err := codec.TextualFromNative(nil, native)
if err != nil {
return Message{}, err
}
msg := Message{int(schemaId), m.Topic, m.Partition, m.Offset, string(m.Key), string(textual)}
return msg, nil
}
func (ac *avroConsumer) Close() {
ac.consumerGroup.Close()
}
func (ac *avroConsumer) Wait() {
<-ac.cleanup
}