Mac下Python与Kafka的配合使用
安装并配置Kafka
安装
# brew install kafka
配置
"""
zookeeper配置文件/usr/local/etc/kafka/zookeeper.propertie
kafka配置文件/usr/local/etc/kafka/server.properties
需要修改的地方:
############################# Socket Server Settings #############################
# The address the socket server listens on. It will get the value returned from
# java.net.InetAddress.getCanonicalHostName() if not configured.
# FORMAT:
# listeners = listener_name://host_name:port
# EXAMPLE:
# listeners = PLAINTEXT://your.host.name:9092
#listeners=PLAINTEXT://:9092
advertised.listeners=PLAINTEXT://a.b.c.d:9092
"""
启动
# zookeeper-server-start /usr/local/etc/kafka/zookeeper.properties & kafka-server-start /usr/local/etc/kafka/server.properties
测试
"""
#创建topic
$kafka-topics –create –zookeeper localhost:2181 –replication-factor 1 –partitions 1 –topic test
#查看创建的topic
$kafka-topics –list –zookeeper localhost:2181
#发送一些消息
$kafka-console-producer –broker-list localhost:9092 –topic test
#消费消息
$kafka-console-consumer –bootstrap-server localhost:9092 –topic test –from-beginning
"""
Python与kafka联动
安装依赖库
# pip2 install pykafka
生产者
# -*- coding:utf-8 -*-
#引入依赖库、包、模块、对象
from pykafka import KafkaClient
#定义全局变量
client = KafkaClient(hosts="192.168.1.1:9092")#建立kafka连接
config = SslConfig(
cafile='/your/ca.cert',
certfile='/your/client.cert',
keyfile='/your/client.key',
password='unlock my client key please'
)
#client = KafkaClient(host="192.168.1.1:9202",ssl_config=config)加ssl的连接方式
#查看所有的topic
#print client.topics
#topic = client.topics['topic_key']#选择一个topic
#当有了topic之后呢,可以创建一个producer,来发消息,生产kafka数据,通过字符串形式,这里是异步的流程,高性能时候delivery_reports=True,批量生产
with topic.get_sync_producer() as producer:
for i in range(4):
producer.produce('test message ' + str(i ** 2))
with topic.get_producer(delivery_reports=True) as producer:
count = 0
while True:
count += 1
producer.produce('test msg', partition_key='{}'.format(count))
if count % 10 ** 5 == 0:
while True:
try:
msg, exc = producer.get_delivery_report(block=False)
if exc is not None:
print 'Failed to deliver msg {}: {}'.format(
msg.partition_key, repr(exc))
else:
print 'Successfully delivered msg {}'.format(
msg.partition_key)
except Queue.Empty:
break
#从topic获取生产者,并生产数据
producer=topic.get_producer()
producer.produce(message)
消费者
#!/usr/bin/python
# -*- coding:utf-8 -*-
from pykafka import KafkaClient
#连接kafka
client = KafkaClient(hosts='192.168.1.1:9092')#这里连接多个客户端
topic = client.topics['topic_key']
#单一消费者
consumer = topic.get_simple_consumer()
#负载均衡
balanced_consumer = topic.get_balanced_consumer(
consumer_group='testgroup',
auto_commit_enable=True, # 设置为False的时候不需要添加consumer_group,直接连接topic即可取到消息
zookeeper_connect='192.168.1.1:2181'#这里就是连接多个zk
)
for message in consumer:
if message is not None:
print message.offset, message.value#打印接收到的消息体的偏移个数和值
for message in balanced_consumer:
if message is not None:
print message.offset, message.value#打印接收到的消息体的偏移个数和值
Mac下Python与Kafka的配合使用的更多相关文章
- Mac下python初学之Image库(PIL)
Mac下python 使用Image库 安装PIL,下载http://www.pythonware.com/products/pil/ 解压PIL源码包,阅读README知道需要使用python se ...
- mac下python环境pip报错[SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:590) 的解决方法
1.mac下python环境pip报错: issuserdeMacBook-Pro:~ issuser$ pip install pyinstallerCollecting pyinstaller ...
- mac下python实现vmstat
mac下没有linux/unix 的vmstat,只有vm_stat; sh-3.2# vm_statMach Virtual Memory Statistics: (page size of 409 ...
- Mac 下 python 环境问题
一.Mac下,可能存在的 python 环境: 1.Mac系统自带的python环境在(由于不同的 mac 系统,默认自带的 python 版本可能不一样): Python 2.7.10: /Syst ...
- Mac下Python和Pycharm之virtualenv
一.python如何配置virtualenv 1.安装virtualenv pip3 install virtualenvpip install -i https://pypi.tuna.tsin ...
- mac 下 python 虚拟环境的安装和配置
前言:继续安装中,这节记录 mac 安装 python 虚拟环境,多版本共存... 1. 安装 pip -- python的包管理工具: sudo easy_install pip 安装成功,出现下面 ...
- mac下Python安装路径的说明
Python安装路径的说明 mac在安装Python时, 对不同的安装方式 不同的型号均会安装在不同的文件夹下 安装方式 路径 系统默认(2.7) /System/Library/Frameworks ...
- 在MAC下 Python+Django+mysql配置
今天在搭建Django+mysql环境的时候遇到了一点问题,记录下来. 安装环境:OS X 10.10操作系统,Python 2.7. MySQLdb其实包含在MySQL-python包中,因此无论下 ...
- [转]mac下Python升级到指定的版本
以2.7升级到3.3为例1.删除原版本a)删除系统库中的版本sudo rm -R /System/Library/Frameworks/Python.framework/Versions/2.7 b) ...
随机推荐
- nginx日志分析利器GoAccess(转)
面试的时候一定会被面到的问题是:给出web服务器的访问日志,请写一个脚本来统计访问前10的IP有哪些?访问前10的请求有哪些?当你领略过goaccess之后,你就明白,这些问题,除了考验你的脚本背诵记 ...
- IS支持解析json
1. MIME设置:在IIS的站点属性的HTTP头设置里,选MIME 映射中点击”文件类型”-”新类型”,添加一个文件类型:关联扩展名:*.json内容类型(MIME):application/x-j ...
- 数组累加兼eval性能测试
<label> <input type="checkbox" />测试结果 </label> <script> document.g ...
- php连接mssql pdo
怀疑mssql的默认编码...应该不是utf8吧??? <?php $cnx = new PDO("odbc:Driver={SQL Server};Server=XEJMZWMDIX ...
- 联合主键用hibernate注解映射方式主要有三种:
将联合主键的字段单独放在一个类中,该类需要实现java.io.Serializable接口并重写equals和hascode 第一.将该类注解为@Embeddable,最后在主类中(该类不包含联合主键 ...
- MFC CreateWindow介绍
CreateWindow 该函数创建一个重叠式窗口.弹出式窗口或子窗口.它指定窗口类,窗口标题,窗口风格,以及窗口的初始位置及大小(可选的).函数也指该窗口的父窗口或所属窗口(如果存在的话),及窗口的 ...
- C++ 判断
C++ 判断判断结构要求程序员指定一个或多个要评估或测试的条件,以及条件为真时要执行的语句(必需的)和条件为假时要执行的语句(可选的). #include <iostream> /* ru ...
- js unicode处理
//岗位详细界面 var str="1.\u000D\u21B52.\u000D\u21B53"; var pad = function() { var tbl = []; ret ...
- 【Java面试题】44 java中有几种类型的流?JDK为每种类型的流提供了一些抽象类以供继承,请说出他们分别是哪些类?
字节流,字符流.字节流继承于InputStream OutputStream,字符流继承于InputStreamReader OutputStreamWriter.在java.io包中还有许多其他的流 ...
- 适配器模式(Adapter Pattern)----------结构型模式
对象适配器模式的缺点是:与类适配器模式相比,要在适配器中置换适配着类的某些方法比较麻烦.如果一定要置换掉适配者类的一个或多个方法,可以先做一个适配者类的子类,在子类中将适配者类的方法置换掉,然后再把适 ...