-
Notifications
You must be signed in to change notification settings - Fork 1
/
avro_producer_test.go
58 lines (54 loc) · 1.55 KB
/
avro_producer_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
package pulsavro
import (
"github.com/apache/pulsar-client-go/pulsar"
"github.com/linkedin/goavro/v2"
"log"
"testing"
"time"
)
// NewAvroProducer is a basic producer to interact with schema registry, avro and pulsar
func TestNewAvroProducer(t *testing.T) {
client, err := pulsar.NewClient(pulsar.ClientOptions{
URL: "pulsar://localhost:6650",
OperationTimeout: 30 * time.Second,
ConnectionTimeout: 30 * time.Second,
})
if err != nil {
log.Fatalf("Initialize pulsar client error: %v", err)
}
defer client.Close()
producerOptions := pulsar.ProducerOptions{
Topic: "public/default/test2",
}
schemaRegistryUrls := []string{"http://localhost:8080"}
schema := `
{
"type": "record",
"name": "TestDatum",
"fields" : [
{"name": "TestString", "type": ["null","string"],"default":null},
{"name": "TestInt", "type": "int", "default": 0},
{"name": "TestBool", "type": "boolean", "default": false}
]
}`
producer, err := NewAvroProducer(client, producerOptions, schemaRegistryUrls, schema)
if err != nil {
log.Fatalf("Initialize pulsar producer error: %v", err)
}
defer producer.Close()
datum := map[string]interface{}{
"TestString": goavro.Union("string", "Test"),
"TestInt": 1,
"TestBool": true,
}
payload, err := producer.EncodeAvroMessage(datum)
if err != nil {
log.Fatalf("Encode avro message error: %v", err)
}
_, err = producer.Send(&pulsar.ProducerMessage{
Payload: payload,
})
if err != nil {
log.Fatalf("Send pulsar message error: %v", err)
}
}