您应该能够使用分区键发送消息,
public OutgoingMessageEnvelope(SystemStream systemStream,
java.lang.Object partitionKey,
java.lang.Object key,
java.lang.Object message)
Constructs a new OutgoingMessageEnvelope from specified components.
Parameters:
systemStream - Object representing the appropriate stream of which this envelope will be sent on.
partitionKey - A key representing which partition of the systemStream to send this envelope on.
key - A deserialized key to be used for the message.
message - A deserialized message to be sent in this envelope.
使用此方法将对您的数据进行分区。但是我认为,如果您正在考虑以编程方式控制分区数量,您应该使用 kafka API 来创建/更改主题,如此处所述