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) ...
随机推荐
- 【译】Linux概念架构的理解
声明:本文转载,原路径地址:http://www.jianshu.com/p/c5ae8f061cfe 摘要 Linux kernel成功的两个原因:(1)灵活的架构设计使得大量的志愿开发者能够很容易 ...
- Thinkphp3.2版本Controller和Action的访问方法
一.3.2版本以前controller和action的访问方式在3.2版本以前如果Controller=c.Action=a的话,访问规则如下:http://localhost:81/demo1/in ...
- LINQ教程三:Lambda表达式解剖
C#3.0(.NET3.5)中引入了Lambda表达式和LINQ.Lambda表达式是使用一些特殊语法表示匿名方法的较短方法. 最基本的Lambda表达式语法如下: (参数列表)=>{方法体} ...
- etl数据库查询
//----------------------------------------利用数据库查询按钮查询-------------------------------------------- st ...
- HBase系统入门--整体介绍
转自:http://www.aboutyun.com/thread-8957-1-2.html 问题导读:1.HBase查询与写入哪个更好一些?2.HBase面对复杂操作能否实现?3.Region服务 ...
- Openfire配置过程,以及与php交互注意事项。
Ben Werdmuller 是一位 Web 策划师和开发人员,他专注于开放源码平台.他是开源社交网络框架 Elgg 的共同创始人和技术带头人.Ben 的博客 http://benwerd.com/. ...
- selenium测试(Java)-- 验证信息(八)
package com.test.validationinfor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.f ...
- e652. Getting the Font Faces for a Font Family
To create a Font object to draw text, it is necessary to specify the font face name. This example de ...
- smb使用 ------转载自http://blog.csdn.net/tlaff/article/details/5463068
一.在Linux系统中查看网络中Windows共享文件及Linux中的Samba共享文件: 常用到smbclient:用法如下 [root@localhost ~]# smbclient -L / ...
- 记js的一个奇葩问题!!!!!!!!!!!!
<a id='gc-yl' onclick='Preview('" + imgPath + "')' style='margin-left:5px;' >预览</ ...