2025-03-23 12:00:56 +08:00

59 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package event_test
import (
"net"
"strconv"
"testing"
"github.com/segmentio/kafka-go"
)
// 创建Topic
func TestCreateTopic(t *testing.T) {
// 1. 连上kafka
conn, err := kafka.Dial("tcp", "localhost:9092")
if err != nil {
panic(err.Error())
}
defer conn.Close()
// contoller 管理, 获取zk地址后管理集群状态
controller, err := conn.Controller()
if err != nil {
panic(err.Error())
}
var controllerConn *kafka.Conn
controllerConn, err = kafka.Dial("tcp", net.JoinHostPort(controller.Host, strconv.Itoa(controller.Port)))
if err != nil {
panic(err.Error())
}
defer controllerConn.Close()
// contoller 集群的维护
err = controllerConn.CreateTopics(kafka.TopicConfig{Topic: "maudit_new", NumPartitions: 6, ReplicationFactor: 1})
if err != nil {
t.Fatal(err)
}
}
// 查询Topic列表
func TestListTopic(t *testing.T) {
// 1. 连上kafka
conn, err := kafka.Dial("tcp", "localhost:9092")
if err != nil {
panic(err.Error())
}
defer conn.Close()
partitions, err := conn.ReadPartitions()
if err != nil {
t.Fatal(err)
}
topics := map[string]int{}
for _, p := range partitions {
topics[p.Topic]++
}
t.Log(topics)
}