python每5分钟从kafka中提取数据的例子

作者:sxf_0123 时间:2022-05-15 16:35:52 

我就废话不多说了,直接上代码吧!


import sys
sys.path.append("..")
from datetime import datetime
from utils.kafka2file import KafkaDownloader
import os
"""
实现取kafka数据,文件按照取数据的间隔命名
如每5分钟从kafka取数据写入文件中,文件名为当前时间加5
"""

TOPIC = "rtz_queue"
HOSTS = "ip:9092,ip:9092"
GROUP = "2001"

def get_end_time(hour,minute,time_step):
if (minute+time_step)%60<60:
 if (minute+time_step)%60<10:
  return str(hour+int((minute+time_step)/60))+":"+"0"+str((minute+time_step)%60)
 else:
  return str(hour+int((minute+time_step)/60))+":"+str((minute+time_step)%60)
else:
 pass

def kafkawritefile(time_step,time_num):
start = datetime.now()
downloader = KafkaDownloader(HOSTS, TOPIC, GROUP)
i = 1
while(i<=time_num):
 end_time = get_end_time(start.hour, start.minute,i*time_step)
 end_time_file = end_time.replace(':', '_')
 outfile_path = "/data/tmp/" + end_time_file + ".csv"

if os.path.exists(outfile_path):
  os.remove(outfile_path)
 writefile = open(outfile_path, 'a+', encoding='utf-8')

for msg in downloader.message():
  curr_time = datetime.now()
  curr_time = str(curr_time)
  split_curr_time = curr_time.split(' ')
  curr_time_str = split_curr_time[1][0:5]

if curr_time_str >= str(end_time):  
   break
 i += 1

if __name__=='__main__':
time_step = 15
time_num = 1
kafkawritefile(time_step,time_num)

来源:https://blog.csdn.net/sxf_123456/article/details/80297959

标签:python,kafka,提取数据
0
投稿

猜你喜欢

  • 用Python将Excel数据导入到SQL Server的例子

    2021-08-21 19:24:30
  • python办公自动化之excel的操作

    2023-11-20 14:01:01
  • Python利用Pillow处理图像的实践指南

    2023-05-07 09:15:30
  • 浅谈django model postgres的json字段编码问题

    2021-12-21 13:55:12
  • MySQL 数据编码 latin1 转 UTF8

    2010-10-14 14:20:00
  • python跳出双层for循环的解决方法

    2022-12-25 18:55:37
  • 浅谈SQL Server中统计对于查询的影响分析

    2012-06-06 20:08:23
  • 详解phpMyAdmin的安装和配置

    2007-06-15 09:56:00
  • Python使用sorted对字典的key或value排序

    2023-12-12 06:36:53
  • XMLHTTP 使用代理获取数据asp代码

    2010-03-22 14:40:00
  • 谈谈网页设计中的字体应用 (1) Font Set

    2009-11-24 12:55:00
  • Python 给某个文件名添加时间戳的方法

    2023-02-10 21:12:56
  • 视觉注意力—解剖设计的根源

    2010-01-22 15:12:00
  • PyQt中实现自定义工具提示ToolTip的方法详解

    2023-11-09 13:34:56
  • conda与jupyter notebook kernel核环境不一致的问题解决

    2021-07-03 15:43:02
  • Python线程编程之Thread详解

    2022-04-10 17:37:01
  • Python pip更新的两种方式详解

    2022-07-06 20:00:29
  • Python中如何使用if语句处理列表实例代码

    2023-11-01 03:12:46
  • Python3多线程版TCP端口扫描器

    2021-07-13 21:25:45
  • 详解如何用Python写个听小说的爬虫

    2021-09-04 09:56:09
  • asp之家 网络编程 m.aspxhome.com