python跨庫檢查數(shù)據(jù)一致性的示例分析,針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。
成都網(wǎng)絡(luò)公司-成都網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)十余年經(jīng)驗(yàn)成就非凡,專業(yè)從事成都網(wǎng)站制作、做網(wǎng)站,成都網(wǎng)頁設(shè)計(jì),成都網(wǎng)頁制作,軟文發(fā)稿,廣告投放平臺等。十余年來已成功提供全面的成都網(wǎng)站建設(shè)方案,打造行業(yè)特色的成都網(wǎng)站建設(shè)案例,建站熱線:18982081108,我們期待您的來電!因?yàn)樽罱暇€了samza實(shí)時(shí)流平臺,實(shí)時(shí)從源數(shù)據(jù)端實(shí)時(shí)傳輸數(shù)據(jù)到數(shù)據(jù)倉庫,于是就需要檢查samza數(shù)據(jù)傳輸?shù)臏?zhǔn)確性,則通過下面的python腳本定時(shí)檢查兩個(gè)數(shù)據(jù)端表的行數(shù),以簡單的檢測下數(shù)據(jù)一致性。下面的腳本每天在早上6點(diǎn)運(yùn)行,檢查昨天以前的所有數(shù)據(jù)行數(shù)是否一致,并郵件通知。因?yàn)橐獧z測的表比較多,并且清洗規(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)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道了解更多相關(guān)知識。