python编写producer、consumer
自主producer、consumer
首先在不同的终端,分别开启两个consumer,保证groupid一致
]# python consumer_kafka.py
执行一次producer
]# python producer_kafka.py
指定key的partition进行发送信息:
from kafka import KafkaProducer
producer = KafkaProducer(bootstrap_servers='localhost:9092')
# # block until all pending messages are sent
# for _ in range(10):
# producer.send('test_m_brokers', b'are you ok!!!')
#
# producer.flush()
# key for hashed partitioning
producer.send('zhongqiu_many_brokers', key=b'', value=b'aaa')
producer.flush()指定partition和offset读数据
#encoding=utf8
from kafka import KafkaConsumer
from kafka import TopicPartition
from kafka.structs import OffsetAndMetadata
from kafka.structs import TopicPartition def main():
consumer = KafkaConsumer('zhongqiu_many_brokers', bootstrap_servers=['master:9092'])
print consumer.partitions_for_topic("zhongqiu_many_brokers")
print consumer.topics() #获取主题列表
print consumer.subscription() #获取当前消费者订阅的主题
print consumer.assignment() #获取当前消费者topic、分区信息
print consumer.beginning_offsets(consumer.assignment()) #获取当前消费者可消费的偏移量 consumer.seek(TopicPartition(topic=u'zhongqiu_many_brokers', partition=0), 10) #重置偏移量
for message in consumer:
print ("%s:%d:%d: key=%s value=%s" % (message.topic, message.partition,
message.offset, message.key,
message.value)) if __name__ == "__main__":
main()
python编写producer、consumer的更多相关文章
- 使用 Python 编写 vim 插件
使用 Python 编写 vim 插件 - 技术翻译 - 开源中国社区 code {margin: 0;padding: 0;white-space: pre;border: none;backgro ...
- 基于python编写的天气抓取程序
以前一直使用中国天气网的天气预报组件都挺好,可是自从他们升级组件后数据加载变得非常不稳定,因为JS的阻塞常常导致网站打开速度很慢.为了解决这个问题决定现学现用python编写一个抓取程序,每天定时抓取 ...
- 用Python编写博客导出工具
用Python编写博客导出工具 罗朝辉 (http://kesalin.github.io/) CC 许可,转载请注明出处 写在前面的话 我在 github 上用 octopress 搭建了个人博 ...
- .net IO异步和Producer/Consumer队列实现一分钟n次http请求
简介 最近工作中有一个需求:要求发送http请求到某站点获取相应的数据,但对方网站限制了请求的次数:一分钟最多200次请求. 搜索之后,在stackoverflow网站查到一个类似的问题..但里面用到 ...
- 【转载】Python编写简易木马程序
转载来自: http://drops.wooyun.org/papers/4751?utm_source=tuicool 使用Python编写一个具有键盘记录.截屏以及通信功能的简易木马. 首先准备好 ...
- 用Python编写的第一个回测程序
用Python编写的第一个回测程序 2016-08-06 def savfig(figureObj, fn_prefix1='backtest8', fn_prefix2='_1_'): import ...
- [译]Python编写虚拟解释器
使用Python编写虚拟机解释器 一.实验说明 1. 环境登录 无需密码自动登录,系统用户名shiyanlou,密码shiyanlou 2. 环境介绍 本实验环境采用带桌面的Ubuntu Linux环 ...
- Hadoop:使用原生python编写MapReduce
功能实现 功能:统计文本文件中所有单词出现的频率功能. 下面是要统计的文本文件 [/root/hadooptest/input.txt] foo foo quux labs foo bar quux ...
- python编写的自动获取代理IP列表的爬虫-chinaboywg-ChinaUnix博客
python编写的自动获取代理IP列表的爬虫-chinaboywg-ChinaUnix博客 undefined Python多线程抓取代理服务器 | Linux运维笔记 undefined java如 ...
随机推荐
- ES6解构赋值的应用场景
一.变量交换 1.ES6的方式 { let a=; let b=; [a,b]=[b,a]; console.log(a,b); } 输出为 2.ES5的方式 采用中间变量的方式进行存储 二.获取函数 ...
- spring读取properties的几种方式
参考链接:http://www.cnblogs.com/zxf330301/p/6184139.html
- Codeforces 767B. The Queue 模拟题
B. The Queue time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...
- Metasploit学习
阶段一:初步渗透 GO! msfconsole 相关漏洞 msf > search platform: windows xp sp3 查看某个漏洞后,查看漏洞详细信息 msf > info ...
- 从hash算法到java hashcode()
转载 https://blog.csdn.net/Walk_er/article/details/74976146 hash算法是一个摘要算法(yy:描述性算法:可以给一个物体确切的描述,但是不能通过 ...
- Windows 8 Stroe Apps 控件
重新想象 Windows 8 Store Apps (3) - 控件之内容控件: ToolTip, Frame, AppBar, ContentControl, ContentPresenter; 容 ...
- 2017年7月25日多校一Function
Function这道题我当时一直很迷,到底怎么来的啊,为什么会这样啊?? 然后看了题解才知道,原来是找循环啊. 已知f(i)=b[f(a(i)],则 f(0) = b[f(a[0])] = b[f(2 ...
- C#中string类型是值类型还是引用类型?(转)
出处:https://www.cnblogs.com/dxxzst/p/8488567.html .Net框架程序设计(修订版)中有这样一段描述:String类型直接继承自Object,这使得它成为一 ...
- 更新源pip
国内镜像源列表 豆瓣(douban) http://pypi.douban.com/simple/清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/阿里云 h ...
- oracle compile 编译无效对象
原博主:http://blog.csdn.net/tianlesoftware/article/details/4843600 Applies to: Oracle Server - Enterpri ...