Confluent Go客户端 – 如何在集群上创建一个主题?

huangapple go评论81阅读模式
英文:

Confluent go client - How to create a topic on a cluster?

问题

我们正在编写集成测试用例,使用confluent kafka producer api生成消息,然后使用consumer api进行消费,使用本地的kafka docker容器设置(使用docker镜像confluentinc/cp-kafka:6.2.0)。

在生成消息之前,我们需要在kafka容器中的集群上创建一个主题。

根据confluent的文档

POST /clusters/{cluster_id}/topics 创建一个新的主题。

集群ID的值应该是什么?是否有其他的API来创建集群ID?

如果没有API,我们可以使用另一个docker容器来创建kafka主题吗?

英文:

We are writing integration test cases, to produce messages using confluent kafka producer api and then consume using consumer api, with local kafka docker container setup(from a docker image confluentinc/cp-kafka:6.2.0).

Before producing the message, we need to create a topic on a cluster in kafka container.


As per the confluent documentation:

POST /clusters/{cluster_id}/topics creates a new topic.

What should be the value of cluster id? Is there a different api to create cluster id?

If there is no api, can we create kafka topic using another docker container?

答案1

得分: 2

我不确定为什么你在查看Go客户端的REST代理文档。你可以在Go代码中完成所有操作,而无需使用另一个容器。

你需要的是一个AdminClient,它像生产者/消费者一样使用bootstrap-servers参数。

a, err := kafka.NewAdminClient(&kafka.ConfigMap{"bootstrap.servers": broker})
...
results, err := a.CreateTopics( ... )

例如:https://github.com/confluentinc/confluent-kafka-go/blob/master/examples/admin_create_topic/admin_create_topic.go

英文:

I'm not sure why you're looking at the REST Proxy documentation for the Go client. You can do this all in the Go code without another container

What you need is an AdminClient, which takes the bootstrap-servers like a producer/consumer would

a, err := kafka.NewAdminClient(&kafka.ConfigMap{"bootstrap.servers": broker})
...
results, err := a.CreateTopics( ... )

e.x. https://github.com/confluentinc/confluent-kafka-go/blob/master/examples/admin_create_topic/admin_create_topic.go

huangapple
  • 本文由 发表于 2021年6月29日 07:07:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/68170887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定