db2資料複製、遷移方法

來源:互聯網
上載者:User
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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.