安装并配置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的配合使用的更多相关文章

  1. Mac下python初学之Image库(PIL)

    Mac下python 使用Image库 安装PIL,下载http://www.pythonware.com/products/pil/ 解压PIL源码包,阅读README知道需要使用python se ...

  2. 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  ...

  3. mac下python实现vmstat

    mac下没有linux/unix 的vmstat,只有vm_stat; sh-3.2# vm_statMach Virtual Memory Statistics: (page size of 409 ...

  4. Mac 下 python 环境问题

    一.Mac下,可能存在的 python 环境: 1.Mac系统自带的python环境在(由于不同的 mac 系统,默认自带的 python 版本可能不一样): Python 2.7.10: /Syst ...

  5. Mac下Python和Pycharm之virtualenv

    一.python如何配置virtualenv   1.安装virtualenv pip3 install virtualenvpip install -i https://pypi.tuna.tsin ...

  6. mac 下 python 虚拟环境的安装和配置

    前言:继续安装中,这节记录 mac 安装 python 虚拟环境,多版本共存... 1. 安装 pip -- python的包管理工具: sudo easy_install pip 安装成功,出现下面 ...

  7. mac下Python安装路径的说明

    Python安装路径的说明 mac在安装Python时, 对不同的安装方式 不同的型号均会安装在不同的文件夹下 安装方式 路径 系统默认(2.7) /System/Library/Frameworks ...

  8. 在MAC下 Python+Django+mysql配置

    今天在搭建Django+mysql环境的时候遇到了一点问题,记录下来. 安装环境:OS X 10.10操作系统,Python 2.7. MySQLdb其实包含在MySQL-python包中,因此无论下 ...

  9. [转]mac下Python升级到指定的版本

    以2.7升级到3.3为例1.删除原版本a)删除系统库中的版本sudo rm -R /System/Library/Frameworks/Python.framework/Versions/2.7 b) ...

随机推荐

  1. 关于trunk、access以及hybrid的一些简单知识

    关于trunk.access以及hybrid的一些简单知识:Access 类型的端口只能属于 1 个 VLAN ,一般用于连接计算机的端口: Trunk 类型的端口可以允许多个 VLAN 通过,可以接 ...

  2. CentOS6.5下安装Nexus

    一.环境准备 (1) CentOS6.5 (2) nexus-latest-bundle.tar.gz 下载地址:http://www.sonatype.org/nexus/archived/ 二.安 ...

  3. 常用的百度API地图操作

    常用的百度API地图操作,包括模糊搜索,放大缩小,并支持移动端 效果图如下 完整代码: http://download.csdn.net/detail/jine515073/8778167

  4. jquery树形表格实现方法

    效果图 准备步骤: 具体使用的Dome可以在这个位置下载 http://download.csdn.net/detail/jine515073/7986227 1.引入jquery.treeTable ...

  5. Laravel Debugbar

    Installation Require this package with composer: composer require barryvdh/laravel-debugbar After up ...

  6. svn命令使用常见问题

    1.如何添加文件 ? svn add filename svn ci -m "add file" 2. svn ci 出现冲突 经常多人开发时难免多个人修改同一个文件导致冲突发送, ...

  7. Qt中Pro文件变量详细说明

    学习Qt时,发现有些知识看了不经常用就忘了,以下是书本上写的一些关于qmake的相关知识,自己看后,打算把一些经常用到的记下来,整理整理. Qt程序一般使用Qt提供的qmake工具来编译. qmake ...

  8. php -- 检查是否存在

    1.检查变量是否存在:isset() 2.检查常量是否存在:defined() 3.检查方法是否存在:function_exists() 4.检查类是否存在:class_exists()

  9. hoj Counting the algorithms

    贪心加树状数组 给出的数据可能出现两种情况,包括与不包括,但我们从右向左删就能避免这个问题. #include<stdio.h> #include<string.h> #inc ...

  10. PHP 去除iphone,ios,emoji表情

    public static function removeEmoji($text) { $clean_text = ""; // Match Emoticons $regexEmo ...