程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> DB2數據庫 >> DB2教程 >> DB2數據復制與遷移的實際操作方法,經典版!

DB2數據復制與遷移的實際操作方法,經典版!

編輯:DB2教程

以下的文章主要是介紹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腳本代碼示例:/**

  1. * 創建導出腳本  
  2. * @param conn  
  3. * @param creator 表創建者  
  4. * @param filePath  
  5. */  
  6. public void createExportFile(Connection conn,String creator,String filePath) throws Exception {  
  7. DBBase dbBase = new DBBase(conn);  
  8. String selectTableSql = "select name from sysibm.systables where creator = '" + creator + "' and type='T'";  
  9. try {  
  10. dbBase.executeQuery(selectTableSql);  
  11. } catch (Exception ex) {  
  12. throw ex;  
  13. } finally {  
  14. dbBase.close();  
  15. }  
  16. DBResult result = dbBase.getSelectDBResult();  
  17. List list = new ArrayList();  
  18. while (result.next()) {  
  19. String table = result.getString(1);  
  20. list.add(table);  
  21. }  
  22. StringBuffer sb = new StringBuffer();  
  23. String enterFlag = "\r\n";  
  24. for (int i = 0; i < list.size();i++) {  
  25. String tableName = (String)list.get(i);  
  26. sb.append("db2 \"export to aa" + String.valueOf(i+1)+ ".ixf of ixf select * from " + tableName + "\"");  
  27. sb.append(enterFlag);  
  28. }  
  29. String str = sb.toString();  
  30. FileUtility.saveStringToFile(filePath, str, false);  
  31. }  

附錄2:生成import腳本代碼示例:/**

  1. * 創建裝載腳本  
  2. * @param conn  
  3. * @param creator 表創建者  
  4. * @param filePath  
  5. */  
  6. public void createLoadFile(Connection conn,String creator,String filePath) throws Exception {  
  7. DBBase dbBase = new DBBase(conn);  
  8. String selectTableSql = "select name from sysibm.systables where creator = '" + creator + "' and type='T'";  
  9. try {  
  10. dbBase.executeQuery(selectTableSql);  
  11. } catch (Exception ex) {  
  12. throw ex;  
  13. } finally {  
  14. dbBase.close();  
  15. }  
  16. DBResult result = dbBase.getSelectDBResult();  
  17. List list = new ArrayList();  
  18. while (result.next()) {  
  19. String table = result.getString(1);  
  20. list.add(table);  
  21. }  
  22. StringBuffer sb = new StringBuffer();  
  23. String enterFlag = "\r\n";  
  24. for (int i = 0; i < list.size();i++) {  
  25. String tableName = (String)list.get(i);  
  26. sb.append("db2 \"load from aa" + String.valueOf(i+1)+ ".ixf of ixf into " + tableName + " COPY NO without prompting \"");  
  27. sb.append(enterFlag);  
  28. }  
  29. String str = sb.toString();  
  30. FileUtility.saveStringToFile(filePath, str, false);  
  31. }  

附錄3:export腳本示例db2 connect to testdb user test password test

  1. db2 "export to aa1.ixf of ixf select * from table1"  
  2. db2 "export to aa2.ixf of ixf select * from table2"  
  3. db2 connect reset  

附錄4:import腳本示例db2 connect to testdb user test password test

  1. db2 "load from aa1.ixf of ixf replace into table1 COPY NO without prompting "  
  2. db2 "load from aa2.ixf of ixf replace into table2 COPY NO without prompting "  
  3. db2 connect reset  

以上的相關內容就是對DB2數據復制和遷移方法的介紹,望你能有所收獲。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved