您好,登錄后才能下訂單哦!
python跨庫檢查數(shù)據(jù)一致性的示例分析,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
因?yàn)樽罱暇€了samza實(shí)時(shí)流平臺(tái),實(shí)時(shí)從源數(shù)據(jù)端實(shí)時(shí)傳輸數(shù)據(jù)到數(shù)據(jù)倉庫,于是就需要檢查samza數(shù)據(jù)傳輸?shù)臏?zhǔn)確性,則通過下面的python腳本定時(shí)檢查兩個(gè)數(shù)據(jù)端表的行數(shù),以簡(jiǎn)單的檢測(cè)下數(shù)據(jù)一致性。下面的腳本每天在早上6點(diǎn)運(yùn)行,檢查昨天以前的所有數(shù)據(jù)行數(shù)是否一致,并郵件通知。因?yàn)橐獧z測(cè)的表比較多,并且清洗規(guī)則不一致,所以只能一張表一張表的進(jìn)行比較,下面只是腳本的部分代碼
#coding=utf-8
import MySQLdb
import psycopg2
import smtplib
import time
from email.mime.text import MIMEText
#通用的郵件函數(shù)
def mail(sub,content):
mailto_list=["hzwuj@tairanchina.com"]
mail_host="smtp.tairanchina.com"
mail_uer="trcloud@tairanchina.com"
mail_pass="r!dHE#3OAGs5TGeh"
message = MIMEText(content,_charset='utf-8')
message['Subject'] = sub
message['From']=mail_uer
message['To'] = ";".join(mailto_list)
try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_uer,mail_pass)
s.sendmail(mail_uer, mailto_list, message.as_string())
s.close()
return True
except Exception:
print 'filed'
return False
#設(shè)置變量為今天的日期
datetime=time.strftime("%Y%m%d", time.localtime())
#從源數(shù)據(jù)庫mysql去得出清洗后的總條數(shù)
conn=MySQLdb.connect(host='115.231.97.10',port=3306,user='select',passwd='123456',db='cms')
cursor1=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
cursor1.execute("select 'biz_account' as tablename,count(*) as sum from biz_account where del_flag='0' and date(create_date)<%s" % datetime)
rows1=cursor1.fetchall()
for list1 in rows1:
sum1=list1['sum']
tablename1=list1['tablename']
#從數(shù)據(jù)庫倉庫greenplum從獲取samza清洗后的表總行數(shù)
conn2 = psycopg2.connect(database="dw", user="admin", password="123456", host="172.30.248.24", port="5432")
cursor2 = conn2.cursor()
cursor2.execute("select 'dw_biz_account' as tablename,count(*) as sum from dw.dw_biz_account where create_day<'%s'" % datetime )
rows2=cursor2.fetchall()
for list2 in rows2:
sum2=list2[1]
tablename2=list2[0]
#總條數(shù)比較
if sum1!=sum2:
mail('數(shù)據(jù)不一致',"%s:%s %s:%s"%(tablename1,sum1,tablename2,sum2))
else:
mail('數(shù)據(jù)一致','數(shù)據(jù)一致')
conn.close()
conn2.close()
關(guān)于python跨庫檢查數(shù)據(jù)一致性的示例分析問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。