Kafka 客户端开发

前言

在了解消费者与消费组之前的的概念之后,可以进行消费者客户端的开发了;

参数配置

在Kafka消息者端KafkaConsumer中有4个参数是必填的:

bootstrap.servers:该参数与生产者相同,指定连接Kafka集群所需的broker地址清单,格式为:host1:port,host2:port,,,

group.id:消费者隶属的消费组名称,默认为“”。

key.deserializer和value.deserializer:与生产者参数相似,消息格式是字节数组(byte[])类型,必须进行相应的反序列化才能还原原有的对象格式;

案例代码

环境使用的是pythn3.8:

生产者代码

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 import datetime
 4 import json
 5 import time
 6 import uuid
 7 
 8 from kafka import KafkaProducer
 9 from kafka.errors import KafkaError
10 
11 producer = KafkaProducer(bootstrap_servers='localhost')
12 topic = 'topic1'
13 
14 def test():
15     print('begin')
16 
17     try:
18         n = 0
19         while True:
20             dic = {}
21             dic['id'] = n
22             n = n + 1
23             dic['myuuid'] = str(uuid.uuid4().hex)
24             dic['time'] = datetime.datetime.now().strftime("%Y%m%d %H:%M:%S")
25             producer.send(topic, json.dumps(dic).encode())
26             print("send:" + json.dumps(dic))
27             time.sleep(0.1)
28 
29     except KafkaError as e:
30         print(e)
31     finally:
32         producer.close()
33         print('error')
34 
35 if __name__ == '__main__':
36     test()

消费者代码

 1 from kafka import KafkaConsumer,TopicPartition
 2 
 3 # connect to Kafka server and pass the topic we want to consume
 4 my_topic = "topic1"                                             # 指定需要消费的主题
 5 consumer = KafkaConsumer(group_id = '20201030_group1',
 6                          bootstrap_servers='127.0.0.1',
 7                          enable_auto_commit = True)
 8 
 9 consumer.assign([
10     TopicPartition(topic=my_topic, partition=0),
11     TopicPartition(topic=my_topic, partition=1),
12     TopicPartition(topic=my_topic, partition=2)
13 ])
14 
15 consumer.seek(TopicPartition(topic=my_topic, partition=1), 12000) # 指定起始offset为12
16 
17 for msg in consumer:                                              # 迭代器,等待下一条消息
18     recv = "%s:%d:%d: key=%s value=%s" % (msg.topic, msg.partition, msg.offset, msg.key, msg.value)
19     print (recv )                                                 # 打印消息

订阅主题和分区

使用subscribe()方法订阅一个主题,还可以订阅多个主题;

 

posted @ 2020-11-25 16:03  lenomail  阅读(140)  评论(0编辑  收藏  举报