设为首页 收藏本站
查看: 602|回复: 0

[经验分享] python增量计算

[复制链接]

尚未签到

发表于 2017-4-23 13:00:38 | 显示全部楼层 |阅读模式
在rsync同步数据后,因业务需要以及文件大小限制,需要对数据做增量分析,而每次都需要拿出文件的增量部分。
    linux有差异计算命令diff以及补丁工具patch,都不是很符合预期。这两种工具都是需要对两个文件进行对比,而若如此做的话,其一计算增量耗时,其二需要有一个原数据的副本文件。多余的副本文件会导致额外的存储开销,以及数据移动成本。
    又因同步过来的数据是多个application的数据,需要针对不同的app进行增量计算,提交给相应的计算任务。希望每次在rsync后直接做增量计算,这里就考虑到直接使用python脚本编写。
   
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os
import getopt
import sys
import fnmatch  
from datetime import datetime,timedelta,date
#判断是否为新一天的开始,数据文件是按日保存,凌晨定时任务需要对前日的数据进行增量计算
def is_newday_begin(app,daystr):
file_day_file='/var/local/diff_file_storage/day_'+app
if os.path.exists(file_day_file):
try:
day_file=open(file_day_file,'r')
oldday=day_file.read()
if oldday != daystr:
return True
finally:
day_file.close()
return False
def read_position(app,daystr,file):
file_day_file='/var/local/diff_file_storage/day_'+app
if os.path.exists(file_day_file):
try:
day_file=open(file_day_file,'r')
oldday=day_file.read()
if oldday != daystr:
day_file.close()
day_file=open(file_day_file,'w+')
day_file.write(daystr)
write_position(daystr,file,0)
finally:
day_file.close()
else:
try:
day_file=open(file_day_file,'w+')
day_file.write(daystr)
finally:
day_file.close()

position=None
open_file=None
position_file=file+'_'+daystr
try:
if os.path.exists(position_file):
open_file=open(position_file,'r')
position = long(open_file.read())
else:
position=0
finally:
if open_file is not None:
open_file.close()
print 'read from ',position_file
return position
def write_position(daystr,file,position):
open_file=None
position_file=file+'_'+daystr
try:
open_file=open(position_file,'w+')
open_file.write(str(position))
finally:
if open_file is not None:
open_file.close()
print 'write to ',position_file
def read_diff(app,day,outpath,times):
filepath='/data/dc_files/'+app+day.strftime('/%Y/%m')
if is_newday_begin(app,day.strftime('%Y-%m-%d')) and times <= 0:
read_diff(app,day+timedelta(days=-1),outpath,times+1)
print 'read_diff old day : ',day
#get old position of file
file_position=0
file_position_dir='/var/local/diff_file_storage/position/'
filenames=os.listdir(filepath)
if not filepath.endswith('/'):
filepath = filepath + '/'
filepre = app+'_'+day.strftime('%Y-%m-%d')+'_'
target_file=open(outpath,'a')
try:
for line in filenames:
if line.startswith(filepre):
print 'read_diff: flush file ',line
file_position_path=file_position_dir+app+'_'+line.replace(filepre,'')
#read position from temp
oldposition = read_position(app,day.strftime('%Y-%m-%d'),file_position_path)
position=oldposition
print 'read_diff: old position is ',position
#read data,change position
try:
source_file=open(filepath+line,'r')
#target_file=open(outpath,'w')
source_file.seek(oldposition)
for temp_line in source_file:
target_file.write(temp_line)
position = position+len(temp_line)
finally:
source_file.close()
if position==oldposition:
#os.remove(outpath)
print outpath,' empty data'
#write position back to temp
write_position(day.strftime('%Y-%m-%d'),file_position_path,position)
print 'read_diff: new position is ',position
finally:
target_file.close()

    如上所示,是diff计算的demo代码。对每次增量计算的位移量进行保存,下次再进行增量计算时,就可以从位移量处直接读取。
    比之用diff,path少了副本相应开销,速度也可观。增加main后也可以直接在shell中调用。

    实现为module,则如下代码所示:
   
#! /usr/bin/env python
# Filename:diffmodule.py
# -*- coding: utf-8 -*-
import os
import getopt
import sys
import fnmatch
#read diff from sfile to ofile by position_file cached
def read_diff(position_file,sfile,ofile):
old_position=read_position(position_file)
tmp_position=0
source_file=None
target_file=None
try:
source_file=open(sfile,'r')
source_file.seek(old_position)
target_file=open(ofile,'a')
for temp_line in source_file:
target_file.write(temp_line)
tmp_position = tmp_position+len(temp_line)
finally:
if source_file is not None:
source_file.close()
if target_file is not None:
target_file.close()
if not tmp_position==0:
write_position(position_file,tmp_position+old_position)
return tmp_position
# read position from file
def read_position(position_file):
position=None
open_file=None
try:
if os.path.exists(position_file):
open_file=open(position_file,'r')
position = long(open_file.read())
else:
position=0
finally:
if open_file is not None:
open_file.close()
return position
#write position to file
def write_position(position_file,position):
open_file=None
result = False
try:
open_file=open(position_file,'w+')
open_file.write(str(position))
finally:
if open_file is not None:
open_file.close()
result = True
return result


   如上代码可供参考,在python 2.7.3下测试OK。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-368189-1-1.html 上篇帖子: Python socket简介 下篇帖子: python 操作符学习
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表