python下载模块命令_python kafka模块操作命令集合

1.安装pykafka

pip install pykafka

2.生产者

from pykafka import KafkaClient

from pykafka import KafkaClient

host = ‘192.168.20.203:9092,192.168.20.204:9092,192.168.20.205:9092‘

client = KafkaClient(hosts=host)

print(client.topics)

topic = client.topics["test_kafka_topic"]

for i in range(10):

print(i)

message = "test message test message" + str(i)

message = bytes(message,encoding=‘utf-8‘)

producer = topic.get_producer()

producer.produce(message)

3.消费者

from pykafka import KafkaClient

host = ‘192.168.20.203:9092,192.168.20.204:9092,192.168.20.205:9092‘

client = KafkaClient(hosts=host)

topic=client.topics[‘test_kafka_topic‘]

balanced_consumer = topic.get_balanced_consumer(consumer_group=‘test_kafka_topic‘,auto_commit_enable=True,

zookeeper_connect=‘192.168.20.201:2181,192.168.20.202:2181,192.168.20.203:2181‘)

for messgage in balanced_consumer:

print(messgage)

if messgage is not None:

print(messgage.offset)

print(messgage.value)

4.kafka的命令

创建主题

bin/kafka-topics.sh --create --zookeeper 192.168.183.100:2181 --replication-factor 2 --partitions 3 --topic topicnewtest1

查看主题信息

bin/kafka-topics.sh --describe --zookeeper 192.168.183.100:2181 --topic topicnewtest1

查看kafka中已经创建的主题列表

bin/kafka-topics.sh --list --zookeeper 192.168.183.100:2181

删除主题

bin/kafka-topics.sh --delete --zookeeper 192.168.183.100:2181 --topic topictest1

查看主题中的消息

bin/kafka-run-class.sh kafka.tools.ConsumerOffsetChecker --group test_kafka_topic --topic test_kafka_topic --zookeeper 192.168.20.203:2181

增加分区

bin/kafka-topics.sh --alter --zookeeper 192.168.183.100:2181 --topic topicnewtest1 --partitions 5

使用kafka自带的生产者客户端脚本

bin/kafka-console-producer.sh --broker-list 192.168.183.102:9092,192.168.183.103:9092 --topic topicnewtest1

使用kafka自带的消费者客户端脚本

bin/kafka-console-consumer.sh --zookeeper 192.168.183.100:2181 --from-beginning --topic topicnewtest1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值