自主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的更多相关文章

  1. 使用 Python 编写 vim 插件

    使用 Python 编写 vim 插件 - 技术翻译 - 开源中国社区 code {margin: 0;padding: 0;white-space: pre;border: none;backgro ...

  2. 基于python编写的天气抓取程序

    以前一直使用中国天气网的天气预报组件都挺好,可是自从他们升级组件后数据加载变得非常不稳定,因为JS的阻塞常常导致网站打开速度很慢.为了解决这个问题决定现学现用python编写一个抓取程序,每天定时抓取 ...

  3. 用Python编写博客导出工具

    用Python编写博客导出工具 罗朝辉 (http://kesalin.github.io/) CC 许可,转载请注明出处   写在前面的话 我在 github 上用 octopress 搭建了个人博 ...

  4. .net IO异步和Producer/Consumer队列实现一分钟n次http请求

    简介 最近工作中有一个需求:要求发送http请求到某站点获取相应的数据,但对方网站限制了请求的次数:一分钟最多200次请求. 搜索之后,在stackoverflow网站查到一个类似的问题..但里面用到 ...

  5. 【转载】Python编写简易木马程序

    转载来自: http://drops.wooyun.org/papers/4751?utm_source=tuicool 使用Python编写一个具有键盘记录.截屏以及通信功能的简易木马. 首先准备好 ...

  6. 用Python编写的第一个回测程序

    用Python编写的第一个回测程序 2016-08-06 def savfig(figureObj, fn_prefix1='backtest8', fn_prefix2='_1_'): import ...

  7. [译]Python编写虚拟解释器

    使用Python编写虚拟机解释器 一.实验说明 1. 环境登录 无需密码自动登录,系统用户名shiyanlou,密码shiyanlou 2. 环境介绍 本实验环境采用带桌面的Ubuntu Linux环 ...

  8. Hadoop:使用原生python编写MapReduce

    功能实现 功能:统计文本文件中所有单词出现的频率功能. 下面是要统计的文本文件 [/root/hadooptest/input.txt] foo foo quux labs foo bar quux ...

  9. python编写的自动获取代理IP列表的爬虫-chinaboywg-ChinaUnix博客

    python编写的自动获取代理IP列表的爬虫-chinaboywg-ChinaUnix博客 undefined Python多线程抓取代理服务器 | Linux运维笔记 undefined java如 ...

随机推荐

  1. stl之容器、迭代器、算法几者之间的关系

    转自:https://blog.csdn.net/bobodem/article/details/49386131 stl包括容器.迭代器和算法: 容器 用于管理一些相关的数据类型.每种容器都有它的优 ...

  2. 浅谈多重检验校正FDR

    浅谈多重检验校正FDR Posted: 四月 12, 2017  Under: Basic  By Kai  no Comments 例如,在我们对鉴定到的差异蛋白做GO功能注释后,通常会计算一个p值 ...

  3. 转载(windows下安装mysql)

    转载请声明出处:http://blog.csdn.net/u013067166/article/details/49951577             最近重装了系统,去MySQL官网下载了最新的M ...

  4. BZOJ 3131 [SDOI2013]淘金 - 数位DP

    传送门 Solution 这道数位$DP$看的我很懵逼啊... 首先我们肯定要先预处理出 $12$位乘起来的所有的可能情况, 记录入数组 $b$, 发现个数并不多, 仅$1e4$不到. 然后我们考虑算 ...

  5. Centos查公网IP地址

    [root@syy ~]# curl icanhazip.com 115.29.208.111

  6. Python配置工具类ConfigParser使用

    ConfigParser模块定义了类ConfigParser,用于实现配置文件解释器.该模块ConfigParser在Python3中,已更名为configparser. 一,函数介绍 1.读取配置文 ...

  7. 把一行数字(readline)读进List并以科学计数法输出(write)到文件

    主要过程是读取的时候是一行字符串,需要Strip去除空格等,然后split变成一个List. 注意这时候数据结构是List但是每一个元素是Str性质的. 所以需要map(float,List)  把这 ...

  8. spring boot 无法启动

    spring boot 使用内置tomcat 报错  : Unable to start embedded Tomcat servlet container Tomcat connector in f ...

  9. windows 下设置nginx负载均衡

    #user nobody; worker_processes ; #error_log logs/error.log; #error_log logs/error.log notice; #error ...

  10. openssl初步使用

    centos平台 md5.c #include <stdio.h> #include <string.h> #include <stdlib.h> //#inclu ...