是否可以在sarama中创建kafka主题? 我知道java API可以让你创建主题,但我无法找到有关如何在sarama中执行此操作的任何信息。 如果可能的话,我应该使用api的例子或解释会很棒 提前谢谢
答案 0 :(得分:4)
可以使用sarama在Kafka中管理主题。我正在编写一个用于管理Kafka主题的Terraform提供程序,并使用sarama在后端进行繁重的工作。
您需要使用sarama.Broker API来执行此操作。例如
// Set broker configuration
broker := sarama.NewBroker("localhost:9092")
// Additional configurations. Check sarama doc for more info
config := sarama.NewConfig()
config.Version = sarama.V1_0_0_0
// Open broker connection with configs defined above
broker.Open(config)
// check if the connection was OK
connected, err := broker.Connected()
if err != nil {
log.Print(err.Error())
}
log.Print(connected)
// Setup the Topic details in CreateTopicRequest struct
topic := "blah25s"
topicDetail := &sarama.TopicDetail{}
topicDetail.NumPartitions = int32(1)
topicDetail.ReplicationFactor = int16(1)
topicDetail.ConfigEntries = make(map[string]*string)
topicDetails := make(map[string]*sarama.TopicDetail)
topicDetails[topic] = topicDetail
request := sarama.CreateTopicsRequest{
Timeout: time.Second * 15,
TopicDetails: topicDetails,
}
// Send request to Broker
response, err := broker.CreateTopics(&request)
// handle errors if any
if err != nil {
log.Printf("%#v", &err)
}
t := response.TopicErrors
for key, val := range t {
log.Printf("Key is %s", key)
log.Printf("Value is %#v", val.Err.Error())
log.Printf("Value3 is %#v", val.ErrMsg)
}
log.Printf("the response is %#v", response)
// close connection to broker
broker.Close()
您可以在github查看有效的代码。请记住在运行代码之前启动kafka代理并导入所有golang依赖项。
答案 1 :(得分:2)
为此,最好直接使用https://github.com/Shopify/sarama/blob/master/admin.go,而不是直接连接到代理。
这可以处理很多情况,例如:
答案 2 :(得分:1)
实际上,在较新版本的Sarama中,您可以使用ClusterAdmin创建主题。您可以在下面找到示例代码:
package main
import (
"github.com/Shopify/sarama" // Sarama 1.22.0
"log"
)
func main() {
brokerAddrs := []string{"localhost:9092"}
config := sarama.NewConfig()
config.Version = sarama.V2_1_0_0
admin, err := sarama.NewClusterAdmin(brokerAddrs, config)
if err != nil {
log.Fatal("Error while creating cluster admin: ", err.Error())
}
defer func() { _ = admin.Close() }()
err = admin.CreateTopic("topic.test.1", &sarama.TopicDetail{
NumPartitions: 1,
ReplicationFactor: 1,
}, false)
if err != nil {
log.Fatal("Error while creating topic: ", err.Error())
}
}