文章主要讲述的是IBM 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脚本代码示例:
/**
* 创建导出脚本
*@paramconn *@paramcreator 表创建者 *@paramfilePath */ publicvoidcreateExportFile(Connectionconn,Stringcreator,StringfilePath)throwsException{ DBBasedbBase=newDBBase(conn); StringselectTableSql="selectnamefromsysibm.systableswherecreator='"+creator+"'andtype='T'"; try{ dbBase.executeQuery(selectTableSql); }catch(Exceptionex){ throwex; }finally{ dbBase.close(); } DBResultresult=dbBase.getSelectDBResult(); Listlist=newArrayList(); while(result.next()){ Stringtable=result.getString(1); list.add(table); } StringBuffersb=newStringBuffer(); StringenterFlag="\r\n"; for(inti=0;i<list.size();i++){ StringtableName=(String)list.get(i); sb.append("db2\"exporttoaa"+String.valueOf(i+1)+".ixfofixfselect*from"+tableName+"\""); sb.append(enterFlag); } Stringstr=sb.toString(); FileUtility.saveStringToFile(filePath,str,false); }
附录2:生成import脚本代码示例:
/**
* 创建装载脚本
* @param conn
* @param creator 表创建者
*@paramfilePath */ publicvoidcreateLoadFile(Connectionconn,Stringcreator,StringfilePath)throwsException{ DBBasedbBase=newDBBase(conn); StringselectTableSql="selectnamefromsysibm.systableswherecreator='"+creator+"'andtype='T'"; try{ dbBase.executeQuery(selectTableSql); }catch(Exceptionex){ throwex; }finally{ dbBase.close(); } DBResultresult=dbBase.getSelectDBResult(); Listlist=newArrayList(); while(result.next()){ Stringtable=result.getString(1); list.add(table); } StringBuffersb=newStringBuffer(); StringenterFlag="\r\n"; for(inti=0;i<list.size();i++){ StringtableName=(String)list.get(i); sb.append("db2\"loadfromaa"+String.valueOf(i+1)+".ixfofixfinto"+tableName+"COPYNOwithoutprompting\""); sb.append(enterFlag); } Stringstr=sb.toString(); FileUtility.saveStringToFile(filePath,str,false); }
附录3:export脚本示例
db2connecttotestdbusertestpasswordtest db2"exporttoaa1.ixfofixfselect*fromtable1" db2"exporttoaa2.ixfofixfselect*fromtable2" db2connectreset
附录4:import脚本示例
db2connecttotestdbusertestpasswordtest db2"loadfromaa1.ixfofixfreplaceintotable1COPYNOwithoutprompting" db2"loadfromaa2.ixfofixfreplaceintotable2COPYNOwithoutprompting" db2connectreset TAG:IBMibm