首页 > 数据库 > DB2 > 正文

IBM DB2 的数据复制、迁移方法

2020-03-09 22:23:01
字体:
来源:转载
供稿:网友

db2数据复制、迁移方法

以下方法经测试,在环境ibm x346,3.2g×2,4g,raid 1,db2 v8.2.4,win2000 adv server,dms表空间中,数据的load速度在60-100万条/min左右。

背景:需要更改数据库表空间,或者需要将数据库中所有表的数据迁移到一个新的数据库中。

步骤:

1.通过db2控制台(db2cc)选中源数据库中的所有表,将其导出成ddl脚本;

2.根据需要对脚本进行必要的修改,譬如更改表空间为gather;

3.新建数据库,新建dms表空间:gather;

4.将ddl脚本在此数据库中执行;

5.编写代码查询源数据库中的所有表,自动生成export脚本;

6.编写代码查询源数据库中的所有表,自动生成import脚本;

7.连接源数据库执行export脚本;

8.连接目标数据库执行import脚本;

附录1:生成export脚本代码示例:

/**

* 创建导出脚本

* @param conn

* @param creator 表创建者

* @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脚本代码示例:

/**

* 创建装载脚本

* @param conn

* @param creator 表创建者

* @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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表