應(yīng)用場景:
本周在進行SIT,我?guī)椭鷥H有的一個測試妹妹對部分表進行數(shù)據(jù)質(zhì)量驗證,第一步需要做的就是比對source與stage表的table definition 與 數(shù)據(jù)內(nèi)容的一致性。
本項目使用的是oracle作為DW,source是oracle,sqlserver和xls.
沒有權(quán)限建立database link, 測試們常用的方法是比對總行數(shù),然后如果數(shù)據(jù)集太大的話,則抽樣比對,導(dǎo)出數(shù)據(jù)到xls,然后使用beyondcompare進行比對。
如果值出現(xiàn)不同,則需要查找出哪些行的值不同,最少要找出一條具體的值。
我測試的時候發(fā)現(xiàn),手動去做這一些事也是挺費力的。就寫了以下的小工具進行輔助測試。
功能:
1.支持從oracle,sqlserver,xlsx中取數(shù)據(jù)進行比對。
2.對oracle與sqlserver,可采用傳統(tǒng)的比對,各自獲取一個readonly ,forward 胡datareader,逐行比對。點擊CompareData按鈕。因為數(shù)據(jù)庫不同找不到統(tǒng)一的方法計算值,oracle中的ora_hash和sqlserver中的hashbyte不一致。
3.對于oracle與oracle的或者是sqlserver對sqlserver的query比對,可以使用GetDiffRange按鈕,即二分查找法進行比對。因為相同的數(shù)據(jù)庫可以找到一個方法進行數(shù)據(jù)比對。
測試數(shù)據(jù)如下:
在oracle的數(shù)據(jù)庫中執(zhí)行以下代碼,生成測試數(shù)據(jù)。
--create table mytest(id int, rid int,name varchar2(20));--create table mytest2(id int, rid int,name varchar2(20));declare i integer :=1;beginwhile i<110000 loopinsert into mytest(ID,RID,NAME) VALUES(mysequence.nextval,i,'dataitem'||i);i:=i+1;end loop;end ; insert into mytest2 SELECT * FROM mytest order by id; update mytest2 set rid=100 where id=100000; commit;select * from mytest minus select * from mytest2; select count(*) as totalcount, to_char(avg(ora_hash(id||rid ||name))) as avghash from (select id as rn, t.* from mytest t)select count(*) as totalcount, to_char(avg(ora_hash(id||rid ||name))) as avghash from (select id as rn, t.* from mytest2 t) update mytest2 set name='EvanTEst' where id=1000;commit;
Range:可以自己設(shè)定,如果是1的話,則直接定位到第一條不同的記錄,如果是大于1 則是一個區(qū)間值。
表示不同的數(shù)據(jù)行就是在他們之間。
為了同時支持oracle和sqlserver,我使用庫中自帶的接口IDbConnection和IDataReader進行開發(fā),可以同時支持oracle和sqlserver and Xlsx.
sql要求:
二分查找需要樣本有序,所以需要寫成
select count(*) as totalcount, to_char(avg(ora_hash(id||rid ||name))) as avghash from (select id as rn, t.* from mytest t)
的樣式,第一個是總行數(shù),第二個要求是里面需要有一個固定列名叫rn。
我使用ora_hash函數(shù)對每行的值進行hash,然后求平均值的方式來計算表區(qū)間內(nèi)行的內(nèi)容是否相同,這是oracle提供的函數(shù)。
類似的有函數(shù)checksum,但是計算速度要比這個慢許多,尤其是表數(shù)據(jù)量大的時候。
sqlserver中有hashbyte函數(shù)類似。 注意如果不使用to_char函數(shù),則算出的值會溢出,如果放到.net中來承接,會報oci數(shù)據(jù)溢出的錯誤,所以to_char也是必須的。
歡迎大家分享BI測試的一些經(jīng)驗心得。
軟件下載地址: files.cnblogs.com/huaxiaoyao/datacompare.rar
主要源碼:
PRivate void compareData2(int start, int end,int total) { rtb_log.AppendText(string.Format("comparedata2({0},{1},{2})", start, end, total) + Environment.NewLine); string tmpsql = " where rn between {0} and {1} "; if (total == 0) { dr1 = getDataReader(conn, rtx_sql.Text); dr2 = getDataReader(conn2, rtx_sql2.Text); } else { dr1 = getDataReader(conn, rtx_sql.Text + string.Format(tmpsql, start, end)); dr2 = getDataReader(conn2, rtx_sql2.Text + string.Format(tmpsql, start, end)); } //assume the dr schema totalcount,avghash bool isdiff = false; while (dr1.Read() && dr2.Read()) { if (total == 0) {end= total = dr1.GetInt32(0); } for (int col = 1; col < dr1.FieldCount; col++) { if (!dr1.GetValue(col).ToString().Equals(dr2.GetValue(col).ToString()) || !(dr1.IsDBNull(col) == dr2.IsDBNull(col)) ) { isdiff = true; } } } dr1.Close(); dr2.Close(); //if isdiff=true the split the totalcount two part if (isdiff) { if ((end - start < range)) return; compareData2(start, (start + end) / 2, end); } else //if same { if (start == end) end = total; compareData2(end, total, total); } }
|
新聞熱點
疑難解答
圖片精選