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