优化生产数据的数据结构
This commit is contained in:
parent
d5e63c46c2
commit
afe3d487b1
@ -13,7 +13,7 @@ package kafka
|
|||||||
//
|
//
|
||||||
// Date : 4:13 下午 2021/9/21
|
// Date : 4:13 下午 2021/9/21
|
||||||
type ProducerData struct {
|
type ProducerData struct {
|
||||||
Data interface{} // 发送的数据
|
Data map[interface{}]interface{} // 发送的数据
|
||||||
Key string // 分区key
|
Key string // 分区key
|
||||||
}
|
}
|
||||||
|
|
||||||
|
10
producer.go
10
producer.go
@ -141,14 +141,10 @@ func (p *Producer) buildMessage(data ProducerData) *sarama.ProducerMessage {
|
|||||||
Key: sarama.StringEncoder(data.Key),
|
Key: sarama.StringEncoder(data.Key),
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
}
|
}
|
||||||
switch v := data.Data.(type) {
|
if nil == data.Data {
|
||||||
case string:
|
data.Data = make(map[interface{}]interface{})
|
||||||
mes.Value = sarama.StringEncoder(v)
|
}
|
||||||
case []byte:
|
|
||||||
mes.Value = sarama.ByteEncoder(v)
|
|
||||||
default:
|
|
||||||
byteData, _ := json.Marshal(data.Data)
|
byteData, _ := json.Marshal(data.Data)
|
||||||
mes.Value = sarama.ByteEncoder(byteData)
|
mes.Value = sarama.ByteEncoder(byteData)
|
||||||
}
|
|
||||||
return mes
|
return mes
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user