我就废话不多说了,直接上代码吧!
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)
以上这篇python每5分钟从kafka中提取数据的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 黑鸭子.2005-《发烧DSD》柏菲[WAV分轨]
- 群星.1990-电影看板·人间道【宝丽金】【WAV+CUE】
- 周殷廷.2024-TO.WHOM.IT.MAY.CONCERN【环球】【FLAC分轨】
- 草蜢.1997-三人世界精选17首【宝丽金】【WAV+CUE】
- 【古琴音乐】管平湖《广陵散》2004[FLAC+CUE]
- 【中国民乐】《少数民族器乐·苗岭的早晨》2004[WAV+CUE]
- 【中国音乐】盛中国《新疆之春》2004[FLAC+CUE]
- 群星《音你而来 第8期》[320K/MP3][44.64MB]
- 群星《音你而来 第8期》[FLAC/分轨][121.21MB]
- 群星《十二女声Ⅲ》[WAV/CUE/分轨][626.4MB]
- 陈百强.1993-离不开...纪念歌集2CD【华纳】【WAV+CUE】
- 于台烟.2003-爱自己【乾坤唱片】【WAV+CUE】
- 群星.2024-我们永远是我们电影原声带【乐禾发文化】【FLAC分轨】
- 【中国民乐】张维良《秋江夜泊》2004[FLAC+CUE]
- 明达年度发烧碟MasterSuperiorAudiophile2011[DSF]