如何在没有生产者的情况下创建 Kafka 消息?

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

How to create a kafka message without producer?

问题

我想创建一个基本的测试用例,而不需要为测试创建生产者、消费者和Kafka实例。我在创建基本消息时遇到了问题,无法找到错误所在。

这是来自confluent-kafka-go SDK的结构定义:

// Message represents a Kafka message
type Message struct {
	TopicPartition TopicPartition
	Value          []byte
	Key            []byte
	Timestamp      time.Time
	TimestampType  TimestampType
	Opaque         interface{}
	Headers        []Header
}

我的基本消息创建如下所示。我已经验证了topicPartition结构和validImageUploadMessageAsBytes是有效的对象。

kafkaMessage := kafka.Message{
	TopicPartition: topicPartition,
	Value:          validImageUploadMessageAsBytes,
	Key:            messageKey,
	Headers:        nil,
}

我还尝试了以下方法,以确保它不会因为我提供的某些数据而失败:

emptyMessage := new(kafka.Message)
emptyMessage.TopicPartition = topicPartition
emptyMessage.Value = []byte("")
emptyMessage.Key = []byte("")

这个示例产生了与下图中相同的输出。

在使用GoLand(2021.3.3)调试测试时,我看到了这个变量的值:

如何在没有生产者的情况下创建 Kafka 消息?

英文:

I want to create a basic test case without bootstrapping producer, consumer and an instance of kafka for a test. I'm stuck with creating a basic message somehow and cannot find my error.
This is the struct definition from the confluent-kafka-go sdk:

// Message represents a Kafka message
type Message struct {
	TopicPartition TopicPartition
	Value          []byte
	Key            []byte
	Timestamp      time.Time
	TimestampType  TimestampType
	Opaque         interface{}
	Headers        []Header
}

My basic message creation looks like this.
I already verified that topicPartition struct and validImageUploadMessageAsBytes are valid objects.

kafkaMessage := kafka.Message{
		TopicPartition: topicPartition,
		Value:          validImageUploadMessageAsBytes,
		Key:            messageKey,
		Headers:        nil,
	}

I also tried the following approach to make sure it does not fail because of some data I provide into the message:

emptyMessage := new(kafka.Message)
	emptyMessage.TopicPartition = topicPartition
	emptyMessage.Value = []byte("")
	emptyMessage.Key = []byte("")

This example produces the same output as in the picture below

When debugging it the test with GoLand (2021.3.3) I am presented with this variable value

如何在没有生产者的情况下创建 Kafka 消息?

答案1

得分: 1

代码正常工作,只是由于IDE GoLand(2021.3.3)的显示问题。

英文:

The code is working properly, it is just a display issue by the IDE GoLand (2021.3.3)

huangapple
  • 本文由 发表于 2022年3月3日 14:40:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/71333031.html
匿名

发表评论

匿名网友

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

确定