forked from dangkaka/go-kafka-avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavroProducer.go
76 lines (66 loc) · 2.18 KB
/
avroProducer.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
package kafka
import (
"encoding/binary"
"github.com/Shopify/sarama"
"github.com/linkedin/goavro"
)
type AvroProducer struct {
producer sarama.SyncProducer
schemaRegistryClient *CachedSchemaRegistryClient
}
// NewAvroProducer is a basic producer to interact with schema registry, avro and kafka
func NewAvroProducer(kafkaServers []string, schemaRegistryServers []string) (*AvroProducer, error) {
config := sarama.NewConfig()
config.Producer.Partitioner = sarama.NewRandomPartitioner
config.Producer.Return.Successes = true
config.Producer.RequiredAcks = sarama.WaitForAll
producer, err := sarama.NewSyncProducer(kafkaServers, config)
if err != nil {
return nil, err
}
schemaRegistryClient := NewCachedSchemaRegistryClient(schemaRegistryServers)
return &AvroProducer{producer, schemaRegistryClient}, nil
}
//GetSchemaId get schema id from schema-registry service
func (ap *AvroProducer) GetSchemaId(topic string, avroCodec *goavro.Codec) (int, error) {
schemaId, err := ap.schemaRegistryClient.CreateSubject(topic, avroCodec)
if err != nil {
return 0, err
}
return schemaId, nil
}
func (ap *AvroProducer) Add(topic string, schema string, key []byte, value []byte) error {
avroCodec, err := goavro.NewCodec(schema)
schemaId, err := ap.GetSchemaId(topic + "-value", avroCodec)
if err != nil {
return err
}
binarySchemaId := make([]byte, 4)
binary.BigEndian.PutUint32(binarySchemaId, uint32(schemaId))
native, _, err := avroCodec.NativeFromTextual(value)
if err != nil {
return err
}
// Convert native Go form to binary Avro data
binaryValue, err := avroCodec.BinaryFromNative(nil, native)
if err != nil {
return err
}
var binaryMsg []byte
// first byte is magic byte, always 0 for now
binaryMsg = append(binaryMsg, byte(0))
//4-byte schema ID as returned by the Schema Registry
binaryMsg = append(binaryMsg, binarySchemaId...)
//avro serialized data in Avro’s binary encoding
binaryMsg = append(binaryMsg, binaryValue...)
msg := &sarama.ProducerMessage{
Topic: topic,
Key: sarama.StringEncoder(key),
Value: sarama.StringEncoder(binaryMsg),
}
_, _, err = ap.producer.SendMessage(msg)
return err
}
func (ac *AvroProducer) Close() {
ac.producer.Close()
}