[同步脚本]mysql-elasticsearch同步
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[同步脚本]mysql-elasticsearch同步,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3217字,纯文字阅读大概需要5分钟。
内容图文
!/usr/bin/env python # coding=utf-8 import sys sys.path.append(‘/Users/cangyufu/work_jbkj/elabels-flask‘) from modules.utils.commons import app, redispool, db_master, db_slave from sqlalchemy import text import os import datetime import time from service.myelasticsearch.index import es from modules.utils.mysqldb import db_obj_dict import datetime CONST_SLEEP = 3 WORK_INDEX = ‘test‘ #https://stackoverflow.com/questions/136168/get-last-n-lines-of-a-file-with-python-similar-to-tail def tail(f, lines=1): total_lines_wanted = lines BLOCK_SIZE = 1024 f.seek(0, 2) block_end_byte = f.tell() lines_to_go = total_lines_wanted block_number = -1 blocks = [] # blocks of size BLOCK_SIZE, in reverse order starting # from the end of the file while lines_to_go > 0 and block_end_byte > 0: if (block_end_byte - BLOCK_SIZE > 0): # read the last block we haven‘t yet read f.seek(block_number*BLOCK_SIZE, 2) blocks.append(f.read(BLOCK_SIZE)) else: # file too small, start from begining f.seek(0,0) # only read what was not read blocks.append(f.read(block_end_byte)) lines_found = blocks[-1].count(‘\n‘) lines_to_go -= lines_found block_end_byte -= BLOCK_SIZE block_number -= 1 all_read_text = ‘‘.join(reversed(blocks)) return ‘\n‘.join(all_read_text.splitlines()[-total_lines_wanted:]) def is_file_exists(filename): if not os.path.isfile(filename): file = open(filename, ‘wb‘) file.write("1970-01-01 00:00:00\n") file.close() #传入要监控的表名 def sync_main(*args): for table in args: try: callable(globals()[‘monitor_‘+table]) except Exception: raise Exception(‘lack function monitor_{}‘.format(table)) for table in args: filename = ‘‘.join([‘monitor_‘, table, ‘.txt‘]) locals()[table+‘path‘] = os.path.join(os.path.dirname(__file__), filename) is_file_exists(locals()[table+‘path‘]) locals()[table+‘file‘] = open(locals()[table+‘path‘], ‘rb+‘) try: print "begin" while True: count = 0 for table in args: print ‘handleing ‘+table last_time = tail(locals()[table+‘file‘], 1) update_time = globals()[‘monitor_‘+table](last_time) print update_time if update_time == last_time: count += 1 continue locals()[table + ‘file‘].write(update_time+‘\n‘) locals()[table + ‘file‘].flush() if count == len(args): time.sleep(CONST_SLEEP) except Exception, e: print e raise e finally: for table in args: locals()[table + ‘file‘].close() ######################################################################################################################## # # 如果要监控哪个表,必须要实现 函数 monitor_table_name,比如要监控table1表,就必须要实现monitor_table1函数, # 传入参数为开始更新的起始时间,初始化时候为1970-01-01 00:00:00,返回更新到的最新的时间 # ######################################################################################################################## def monitor_table1(last_time): pass return last_timedef monitor_table2(last_time):
pass
return last_time
def trans_date_time(dt):
return datetime.datetime.strptime(dt, "%Y-%m-%d %H:%M:%S")
sync_main(‘table1‘,‘table2‘)
[同步脚本]mysql-elasticsearch同步
标签:同步 lines 开始 pat ast 网上 too and was
本文系统来源:http://www.cnblogs.com/fcyworld/p/7436093.html
内容总结
以上是互联网集市为您收集整理的[同步脚本]mysql-elasticsearch同步全部内容,希望文章能够帮你解决[同步脚本]mysql-elasticsearch同步所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。