-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-test-topic.go
36 lines (29 loc) · 946 Bytes
/
create-test-topic.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
package main
import (
"log"
"github.com/IBM/sarama"
)
// this is implemented because we want to create a test topic if it is required to do so
// this is done by checking the config file and if the value is true then we create the test topic
// this is done by creating a new cluster admin from the kafka connection and then creating a topic with the name test-topic
func (s *Server) createTestTopicIfRequired() {
createTestTopic := s.config.CreateTestTopic
if !createTestTopic {
return
}
admin, err := sarama.NewClusterAdminFromClient(s.kafkaConn)
if err != nil {
log.Println("Failed to create Kafka admin client:", err)
return
}
topicDetail := &sarama.TopicDetail{
NumPartitions: 1,
ReplicationFactor: 1,
}
err = admin.CreateTopic("test-topic", topicDetail, false)
if err != nil {
log.Println("Failed to create test topic:", err)
return
}
log.Println("Test topic created successfully %v", "test-topic")
}