DIY编程器网

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1071|回复: 0
打印 上一主题 下一主题

[待整理] IBM DB2 数据复制迁移方法

[复制链接]
跳转到指定楼层
楼主
发表于 2014-10-13 15:28:05 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
背景:需要更改数据库表空间,或者需要将数据库中所有表的数据迁移到一个新的数据库中。  步骤:
       
  •   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
            */
            * 创建导出脚本
            * @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 = "rn";
            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 = "rn";
            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
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|文字版|手机版|DIY编程器网 ( 桂ICP备14005565号-1 )

GMT+8, 2024-9-27 09:23 , 耗时 0.098329 秒, 18 个查询请求 , Gzip 开启.

各位嘉宾言论仅代表个人观点,非属DIY编程器网立场。

桂公网安备 45031202000115号

DIY编程器群(超员):41210778 DIY编程器

DIY编程器群1(满员):3044634 DIY编程器1

diy编程器群2:551025008 diy编程器群2

QQ:28000622;Email:libyoufer@sina.com

本站由桂林市临桂区技兴电子商务经营部独家赞助。旨在技术交流,请自觉遵守国家法律法规,一旦发现将做封号删号处理。

快速回复 返回顶部 返回列表