db2數(shù)據(jù)復(fù)制、遷移方法
以下方法經(jīng)測(cè)試,在環(huán)境ibm x346,3.2g×2,4g,raid 1,db2 v8.2.4,win2000 adv server,dms表空間中,數(shù)據(jù)的load速度在60-100萬(wàn)條/min左右。
背景:需要更改數(shù)據(jù)庫(kù)表空間,或者需要將數(shù)據(jù)庫(kù)中所有表的數(shù)據(jù)遷移到一個(gè)新的數(shù)據(jù)庫(kù)中。
步驟:
1.通過(guò)db2控制臺(tái)(db2cc)選中源數(shù)據(jù)庫(kù)中的所有表,將其導(dǎo)出成ddl腳本;
2.根據(jù)需要對(duì)腳本進(jìn)行必要的修改,譬如更改表空間為gather;
3.新建數(shù)據(jù)庫(kù),新建dms表空間:gather;
4.將ddl腳本在此數(shù)據(jù)庫(kù)中執(zhí)行;
5.編寫(xiě)代碼查詢?cè)磾?shù)據(jù)庫(kù)中的所有表,自動(dòng)生成export腳本;
6.編寫(xiě)代碼查詢?cè)磾?shù)據(jù)庫(kù)中的所有表,自動(dòng)生成import腳本;
7.連接源數(shù)據(jù)庫(kù)執(zhí)行export腳本;
8.連接目標(biāo)數(shù)據(jù)庫(kù)執(zhí)行import腳本;
附錄1:生成export腳本代碼示例:
/**
* 創(chuàng)建導(dǎo)出腳本
* @param conn
* @param creator 表創(chuàng)建者
* @param filepath
*/
public void createexportfile(connection conn,string creator,string filepath) throws exception {
dbbase dbbase = new dbbase(conn);
string selecttablesql = "select name from sysibm.systables where creator = '" + creator + "' and type='t'";
try {
dbbase.executequery(selecttablesql);
} catch (exception ex) {
throw ex;
} finally {
dbbase.close();
}
dbresult result = dbbase.getselectdbresult();
list list = new arraylist();
while (result.next()) {
string table = result.getstring(1);
list.add(table);
}
stringbuffer sb = new stringbuffer();
string enterflag = "/r/n";
for (int i = 0; i < list.size();i++) {
string tablename = (string)list.get(i);
sb.append("db2 /"export to aa" + string.valueof(i+1)+ ".ixf of ixf select * from " + tablename + "/"");
sb.append(enterflag);
}
string str = sb.tostring();
fileutility.savestringtofile(filepath, str, false);
}
附錄2:生成import腳本代碼示例:
/**
* 創(chuàng)建裝載腳本
* @param conn
* @param creator 表創(chuàng)建者
* @param filepath
*/
public void createloadfile(connection conn,string creator,string filepath) throws exception {
dbbase dbbase = new dbbase(conn);
string selecttablesql = "select name from sysibm.systables where creator = '" + creator + "' and type='t'";
try {
dbbase.executequery(selecttablesql);
} catch (exception ex) {
throw ex;
} finally {
dbbase.close();
}
dbresult result = dbbase.getselectdbresult();
list list = new arraylist();
while (result.next()) {
string table = result.getstring(1);
list.add(table);
}
stringbuffer sb = new stringbuffer();
string enterflag = "/r/n";
for (int i = 0; i < list.size();i++) {
string tablename = (string)list.get(i);
sb.append("db2 /"load from aa" + string.valueof(i+1)+ ".ixf of ixf into " + tablename + " copy no without prompting /"");
sb.append(enterflag);
}
string str = sb.tostring();
fileutility.savestringtofile(filepath, str, false);
}
附錄3:export腳本示例
db2 connect to testdb user test password test
db2 "export to aa1.ixf of ixf select * from table1"
db2 "export to aa2.ixf of ixf select * from table2"
db2 connect reset
附錄4:import腳本示例
db2 connect to testdb user test password test
db2 "load from aa1.ixf of ixf replace into table1 copy no without prompting "
db2 "load from aa2.ixf of ixf replace into table2 copy no without prompting "
db2 connect reset
新聞熱點(diǎn)
疑難解答