forked from confluentinc/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
producer.go
executable file
·140 lines (123 loc) · 4 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
// =============================================================================
//
// Produce messages to Confluent Cloud
// Using Confluent Golang Client for Apache Kafka
//
// =============================================================================
package main
/**
* Copyright 2020 Confluent Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import (
"./ccloud"
"context"
"encoding/json"
"fmt"
"gopkg.in/confluentinc/confluent-kafka-go.v1/kafka"
"os"
"time"
)
// RecordValue represents the struct of the value in a Kafka message
type RecordValue ccloud.RecordValue
// CreateTopic creates a topic using the Admin Client API
func CreateTopic(p *kafka.Producer, topic string) {
a, err := kafka.NewAdminClientFromProducer(p)
if err != nil {
fmt.Printf("Failed to create new admin client from producer: %s", err)
os.Exit(1)
}
// Contexts are used to abort or limit the amount of time
// the Admin call blocks waiting for a result.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Create topics on cluster.
// Set Admin options to wait up to 60s for the operation to finish on the remote cluster
maxDur, err := time.ParseDuration("60s")
if err != nil {
fmt.Printf("ParseDuration(60s): %s", err)
os.Exit(1)
}
results, err := a.CreateTopics(
ctx,
// Multiple topics can be created simultaneously
// by providing more TopicSpecification structs here.
[]kafka.TopicSpecification{{
Topic: topic,
NumPartitions: 1,
ReplicationFactor: 3}},
// Admin options
kafka.SetAdminOperationTimeout(maxDur))
if err != nil {
fmt.Printf("Admin Client request error: %v\n", err)
os.Exit(1)
}
for _, result := range results {
if result.Error.Code() != kafka.ErrNoError && result.Error.Code() != kafka.ErrTopicAlreadyExists {
fmt.Printf("Failed to create topic: %v\n", result.Error)
os.Exit(1)
}
fmt.Printf("%v\n", result)
}
a.Close()
}
func main() {
// Initialization
configFile, topic := ccloud.ParseArgs()
conf := ccloud.ReadCCloudConfig(*configFile)
// Create Producer instance
p, err := kafka.NewProducer(&kafka.ConfigMap{
"bootstrap.servers": conf["bootstrap.servers"],
"sasl.mechanisms": conf["sasl.mechanisms"],
"security.protocol": conf["security.protocol"],
"sasl.username": conf["sasl.username"],
"sasl.password": conf["sasl.password"]})
if err != nil {
fmt.Printf("Failed to create producer: %s", err)
os.Exit(1)
}
// Create topic if needed
CreateTopic(p, *topic)
// Go-routine to handle message delivery reports and
// possibly other event types (errors, stats, etc)
go func() {
for e := range p.Events() {
switch ev := e.(type) {
case *kafka.Message:
if ev.TopicPartition.Error != nil {
fmt.Printf("Failed to deliver message: %v\n", ev.TopicPartition)
} else {
fmt.Printf("Successfully produced record to topic %s partition [%d] @ offset %v\n",
*ev.TopicPartition.Topic, ev.TopicPartition.Partition, ev.TopicPartition.Offset)
}
}
}
}()
for n := 0; n < 10; n++ {
recordKey := "alice"
data := &RecordValue{
Count: n}
recordValue, _ := json.Marshal(&data)
fmt.Printf("Preparing to produce record: %s\t%s\n", recordKey, recordValue)
p.Produce(&kafka.Message{
TopicPartition: kafka.TopicPartition{Topic: topic, Partition: kafka.PartitionAny},
Key: []byte(recordKey),
Value: []byte(recordValue),
}, nil)
}
// Wait for all messages to be delivered
p.Flush(15 * 1000)
fmt.Printf("10 messages were produced to topic %s!", *topic)
p.Close()
}