Kafka API for Python

1.安装pip:

[root@kafka-con1 opt]# wget https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz

2.解压编译pip:

[root@kafka-con1 pip-1.5.4]# tar -zvxf pip-1.5.4.tar.gz 
[root@kafka-con1 pip-1.5.4]# python setup.py install

3.安装kafka-python

[root@kafka-con1 pip-1.5.4]# pip install kafka-python
Downloading/unpacking kafka-python
  Downloading kafka_python-1.3.0-py2.py3-none-any.whl (193kB): 193kB downloaded
Installing collected packages: kafka-python
Successfully installed kafka-python
Cleaning up...

4.producer 程序:

[root@kafka-con1 pythonscript]# more producer.py 
from kafka import KafkaProducer
producer = KafkaProducer(bootstrap_servers='10.170.197.16:9092')
for _ in range(100):
  producer.send('leontest', b'some_message_bytes')
  ```
5.consumer 程序:
  ```
[root@kafka-pr pythonscri]# more consumer.py 
from kafka import KafkaConsumer
consumer = KafkaConsumer('leontest',bootstrap_servers=['10.170.194.98:9092'])
for msg in consumer:
    print (msg)
consumer.close()

6.运行producer 和consumer:
[root@kafka-con1 pythonscript]# python producer.py

7.成功打印100个some_message_bytes
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值