簡體   English   中英

如何創建沒有生產者的kafka消息?

[英]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("")

此示例生成與下圖相同的 output

在使用 GoLand (2021.3.3) 調試測試時,我看到了這個變量值

計算節點表示失敗

代碼運行正常,只是IDE GoLand (2021.3.3)的顯示問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM