欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python每5分鐘從kafka中提取數(shù)據(jù)的例子

 更新時間:2019年12月23日 09:04:24   作者:sxf_0123  
今天小編就為大家分享一篇python每5分鐘從kafka中提取數(shù)據(jù)的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

我就廢話不多說了,直接上代碼吧!

import sys
sys.path.append("..")
from datetime import datetime
from utils.kafka2file import KafkaDownloader
import os
"""
實(shí)現(xiàn)取kafka數(shù)據(jù),文件按照取數(shù)據(jù)的間隔命名
如每5分鐘從kafka取數(shù)據(jù)寫入文件中,文件名為當(dāng)前時間加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)

以上這篇python每5分鐘從kafka中提取數(shù)據(jù)的例子就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論