/
producer.go
178 lines (153 loc) · 4.97 KB
/
producer.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
167
168
169
170
171
172
173
174
175
176
177
178
package kafkabee
import (
"fmt"
"log"
"os"
"github.com/confluentinc/confluent-kafka-go/kafka"
"github.com/riferrei/srclient"
)
type KafkaStreaming struct {
producer *kafka.Producer
scClient *srclient.SchemaRegistryClient
valueSchema *srclient.Schema
keySchema *srclient.Schema
europeanBeeSchema *srclient.Schema
config kafkaConfig
}
type kafkaConfig struct {
kafkaUrl string
schemaRegistryUrl string
securityProtocol string
pathCaPem string
pathServiceCert string
pathServiceKey string
topic string
topicEuropeanBee string
schemaNameValue string
schemaNameKey string
europeanBeeSchemaNameValue string
}
func Init() error {
conf := getConfig()
kafkaConfigMap := &kafka.ConfigMap{
"bootstrap.servers": conf.kafkaUrl,
"security.protocol": conf.securityProtocol,
}
if conf.securityProtocol != "PLAINTEXT" {
kafkaConfigMap.SetKey("ssl.ca.location", conf.pathCaPem)
kafkaConfigMap.SetKey("ssl.certificate.location", conf.pathServiceCert)
kafkaConfigMap.SetKey("ssl.key.location", conf.pathServiceKey)
}
p, err := kafka.NewProducer(kafkaConfigMap)
if err != nil {
return (fmt.Errorf("error creating producer %s", err))
}
// uncomment to check the status of the produced messages
/*go func() {
for event := range p.Events() {
switch ev := event.(type) {
case *kafka.Message:
m := ev
if ev.TopicPartition.Error != nil {
fmt.Printf("Error delivering the message '%s'\n error %s\n", m.Key, ev.TopicPartition.Error)
}else {
fmt.Printf("Message delivered successfully!\n key : %s\n headers : %s\n opaque : %s\n timestamp: %s\n value : %s \n offet : %s \n partition : %d\n topic : %s\n", m.Key, m.Headers, m.Opaque, m.Timestamp, m.Value, m.TopicPartition.Offset, m.TopicPartition.Partition, m.TopicPartition.Topic)
}
}
}
}()
*/
schemaRegistryClient := srclient.CreateSchemaRegistryClient(conf.schemaRegistryUrl)
valueSchema, err := getSchema(schemaRegistryClient, conf.schemaNameValue)
if err != nil {
return fmt.Errorf("error getting schema %s => %s", conf.schemaNameValue, err)
}
keySchema, err := getSchema(schemaRegistryClient, conf.schemaNameKey)
if err != nil {
return fmt.Errorf("error getting schema %s => %s", conf.schemaNameKey, err)
}
europeanBeeSchema, err := getSchema(schemaRegistryClient, conf.europeanBeeSchemaNameValue)
if err != nil {
return fmt.Errorf("error getting schema %s => %s", conf.europeanBeeSchemaNameValue, err)
}
Stream = KafkaStreaming{
producer: p,
scClient: schemaRegistryClient,
valueSchema: valueSchema,
keySchema: keySchema,
europeanBeeSchema: europeanBeeSchema,
config: conf,
}
return nil
}
func (k KafkaStreaming) Produce(d Data) error {
recordValue, err := encodeDataFromSchema(k.valueSchema, d.DataValue)
if err != nil {
return err
}
recordKey, err := encodeDataFromSchema(k.keySchema, d.DataKey)
if err != nil {
return err
}
return k.producer.Produce(
&kafka.Message{
TopicPartition: kafka.TopicPartition{
Topic: &k.config.topic,
Partition: -1,
},
Value: recordValue,
Key: recordKey,
}, nil,
)
}
func (k KafkaStreaming) ProduceEuropeanBee(eB europeanBee) error {
v,err := encodeDataFromSchema(k.europeanBeeSchema, eB)
if err != nil {
return err
}
return k.producer.Produce(
&kafka.Message{
TopicPartition: kafka.TopicPartition{
Topic: &k.config.topicEuropeanBee,
Partition: -1,
},
Value: v,
Key: []byte(fmt.Sprintf("%d", eB.HiveID)),
}, nil,
)
}
func getConfig() kafkaConfig {
return kafkaConfig{
kafkaUrl: "localhost:29092",
schemaRegistryUrl: "http://localhost:8081",
securityProtocol: "PLAINTEXT",
// if you have ssl config, this is where you put your authentication files
pathCaPem: "",
pathServiceCert: "",
pathServiceKey: "",
topic: "detected",
topicEuropeanBee: "european-bee",
schemaNameValue: "detected-value",
schemaNameKey: "detected-key",
europeanBeeSchemaNameValue: "european-bee-value",
}
/*return kafkaConfig{
kafkaUrl: getEnvAndWarnIfMissing("KAFKA_URL"),
schemaRegistryUrl: getEnvAndWarnIfMissing("SCHEMA_REGISTRY_URL"),
securityProtocol: getEnvAndWarnIfMissing("SECURITY_PROTOCOL"),
pathCaPem: getEnvAndWarnIfMissing("CERTS_PATH_CA_PEM"),
pathServiceCert: getEnvAndWarnIfMissing("CERTS_PATH_SERVICE_CERT"),
pathServiceKey: getEnvAndWarnIfMissing("CERTS_PATH_SERVICE_KEY"),
topic: getEnvAndWarnIfMissing("TOPIC"),
topic: getEnvAndWarnIfMissing("TOPIC_EUROPEAN_BEE"),
schemaNameValue: getEnvAndWarnIfMissing("SCHEMA_NAME_VALUE"),
schemaNameKey: getEnvAndWarnIfMissing("SCHEMA_NAME_KEY"),
}*/
}
func getEnvAndWarnIfMissing(s string) string {
v := os.Getenv(s)
if v == "" {
log.Printf("WARN : %s env is not set", s)
}
return v
}