本文整理汇总了Python中afkak.kafkacodec.KafkaCodec.encode_produce_request方法的典型用法代码示例。如果您正苦于以下问题:Python KafkaCodec.encode_produce_request方法的具体用法?Python KafkaCodec.encode_produce_request怎么用?Python KafkaCodec.encode_produce_request使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类afkak.kafkacodec.KafkaCodec
的用法示例。
在下文中一共展示了KafkaCodec.encode_produce_request方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_encode_produce_request
# 需要导入模块: from afkak.kafkacodec import KafkaCodec [as 别名]
# 或者: from afkak.kafkacodec.KafkaCodec import encode_produce_request [as 别名]
def test_encode_produce_request(self):
requests = [
ProduceRequest("topic1", 0, [
create_message(b"a"),
create_message(b"b"),
]),
ProduceRequest(u"topic2", 1, [
create_message(b"c"),
]),
]
msg_a_binary = KafkaCodec._encode_message(create_message(b"a"))
msg_b_binary = KafkaCodec._encode_message(create_message(b"b"))
msg_c_binary = KafkaCodec._encode_message(create_message(b"c"))
header = b"".join([
struct.pack('>h', 0), # Msg Header, Message type = Produce
struct.pack('>h', 0), # Msg Header, API version
struct.pack('>i', 2), # Msg Header, Correlation ID
struct.pack('>h7s', 7, b"client1"), # Msg Header, The client ID
struct.pack('>h', 2), # Num acks required
struct.pack('>i', 100), # Request Timeout
struct.pack('>i', 2), # The number of requests
])
total_len = len(msg_a_binary) + len(msg_b_binary)
topic1 = b"".join([
struct.pack('>h6s', 6, b'topic1'), # The topic1
struct.pack('>i', 1), # One message set
struct.pack('>i', 0), # Partition 0
struct.pack('>i', total_len + 24), # Size of message set
struct.pack('>q', 0), # No offset specified
struct.pack('>i', len(msg_a_binary)), # Length of message
msg_a_binary, # Actual message
struct.pack('>q', 0), # No offset specified
struct.pack('>i', len(msg_b_binary)), # Length of message
msg_b_binary, # Actual message
])
topic2 = b"".join([
struct.pack('>h6s', 6, b'topic2'), # The topic1
struct.pack('>i', 1), # One message set
struct.pack('>i', 1), # Partition 1
struct.pack('>i', len(msg_c_binary) + 12), # Size of message set
struct.pack('>q', 0), # No offset specified
struct.pack('>i', len(msg_c_binary)), # Length of message
msg_c_binary, # Actual message
])
expected1 = b"".join([header, topic1, topic2])
expected2 = b"".join([header, topic2, topic1])
encoded = KafkaCodec.encode_produce_request(
b"client1", 2, requests, 2, 100)
self.assertIn(encoded, [expected1, expected2])