在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。
分享到:
相关推荐
从2.3版本开始接触Python,工作中使用Python编写脚本程序,用于快速原型构建以及日志计算等日常作业;业余时,作为一个编程语言爱好者,对D、Kotlin、Lua、Clojure、Scala、Julia、Go等语言均有了解,但至今仍为...
基于半监督密度聚类和增量学习的故障诊断系统python源码+项目说明+代码注释拉满.zip在工业系统中,机械设备在运行过程中会产生数据流,不断变化且缺乏标签,使得基于深度学习的故障诊断方法难以在这种环境下有效工作...
常用的排序算法,使用PYTHON实现,包括:插入排序、选择排序、快速排序、希尔排序(附多种增量生成算法)、堆排序、归并排序
基于半监督密度聚类和增量学习的故障诊断系统python源码(项目说明+代码注释拉满 【项目说明】 整体架构 故障诊断模块 半监督标记模块 增量更新模块 故障诊断模块读取设备监测数据,根据数据判断设备是否处于正常状态...
为解决分类器学习新样本知识的问题,提出一种基于近邻算法的增量学习算法。该算法以最近邻算法为基础,首先计算新样本与标准样本之间的匹配度,找到最佳匹配样本和次佳匹配样本,然后通过与匹配度阈值进行比较来决定...
基于知识蒸馏的目标检测模型增量深度学习方法的python源码 - 不懂运行,下载完可以私聊问,可远程教学 该资源内项目源码是个人的毕设,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载...
3.2.2 增量赋值 3.2.3 多重赋值 3.2.4 “多元”赋值 3.3 标识符 3.3.1 合法的Python标识符 3.3.2 关键字 3.3.3 内建 3.3.4 专用下划线标识符 3.4 基本风格指南 ...
6.4 增量式解析大型XML文件 6.5 将字典转换为XML 6.6 解析和修改XML 6.7 利用命名空间解析XML文档 6.8 与关系型数据库的交互 6.9 编码和解码十六进制数 6.10 编码解码Base64数据 6.11 读写二进制数组数据 ...
答:首先计算坐标增量dx,dy(两个对应坐标分量相减,终点的减始点的)。 若dx,dy中有一个为零时,根据另一个的正负决定方位角(0,90,180,270这四个中的一个,可画坐标轴图分析,但不要画为数学坐标哦)。 基本思路...
增量学习是解决这个问题的有效方法,但它严重依赖标记数据并且无法检测数据中的新类别,这使得它在实际应用中并不理想。 鉴于此,设计了一种基于半监督式增量学习的工业物联网设备故 - 不懂运行,下载完可以私聊问...
3.2.2 增量赋值 3.2.3 多重赋值 3.2.4 “多元”赋值 3.3 标识符 3.3.1 合法的Python标识符 3.3.2 关键字 3.3.3 内建 3.3.4 专用下划线标识符 3.4 基本风格指南 ...
经营资产自由现金流=公司维持原有生产经营规模前提下的增量现金流入=经营活动现金流量净额-保全性资本支出=经营活动现金流量净额-固定资产折旧-无形资产和长期待摊费用摊销-处置长期资产的损失 $WACC=k_d\times\frac...
根据设置的基金涨落阈值以及达到阈值之后的额外增量给用户发送包含表格附件的提醒邮件 开始之前 环境要求 python 3 版本 依赖的第三方库 requests yagmail 开通邮箱的SMTP服务 使用方法 获取代码 直接下载zip形式的...
相关参考:关于Python和Java的多进程多线程计算方法对比 6. 对于Ajax请求的处理 对于“加载更多”情况,使用Ajax来传输很多数据。 它的工作原理是:从网页的url加载网页的源代码之后,会在浏览器里执行JavaScript...
支持批量比较文件,差异增量更新、批量更新。 ## 使用方式 * 安装七牛Python SDK `pip install qiniu` * 填写脚本文件(qiniusync.py)的配置信息 ``` access_key = '' secret_key = '' bucket_name = '' bucket_...
pyTMD基于Python的潮汐预测软件,可读取OTIS,GOT和FES格式的潮汐解决方案,以计算洋潮和装船潮汐NASA地壳动力学数据信息系统(CDDIS)的增量时间极潮预测软件,用于计算径向极潮位移 来自NASA CDDIS的IERS每日地球...
通过Python进行自动基础分析这个python程序会根据相对于行业的估值,盈利能力,增长和价格绩效指标对100种股票进行评级。 查看文件S&P500StockRatings.csv和MidCap+2BStockRatings作为输出示例评分标准此程序中使用...
2. 根据设置的基金涨跌阈值以及达到阈值之后的百分点增量给使用者发送包含表格附件的提醒邮件 ## 开始之前 1. 环境要求 ```text python 3 版本 ``` 2. 依赖的第三方库 ```text requests yagmail ``` -------- ...
增量kd 增量 KD 项目的源代码和 IPython notebook 实验实验依赖关系 :用于快速符号计算数学表达式的 Python 库。 :Python 科学计算包。 通过轻松安装 Numpy 和其他有用的科学软件包。 :Lisa Lab 的机器学习研究库...