利用sqlldr从MySQL导出一张表数据到Oracle
根据业务需求,需要从MySQL库中同步一张表tap_application到Oracle中,下面是记录的导入过程.
1. 查看MySQL表结构
desc tap_application;
+---------------------+---------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+---------------+------+-----+---------------------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| VENDORID | int(11) | NO | | NULL | |
| NAME | varchar(128) | NO | | NULL | |
| DESCRIPTION | varchar(255) | YES | | NULL | |
| CREATIONDATE | datetime | NO | | 0000-00-00 00:00:00 | |
| MODIFICATIONDATE | datetime | NO | | 0000-00-00 00:00:00 | |
| STATUS | int(11) | NO | | NULL | |
| URL | varchar(1024) | YES | | NULL | |
| APPTYPE | smallint(6) | YES | | NULL | |
| TEST_KEY | varchar(512) | YES | | NULL | |
| PRODUCTION_KEY | varchar(512) | YES | | NULL | |
| TEST_APISUSED | varchar(256) | YES | | NULL | |
| PRODUCTION_APISUSED | varchar(256) | YES | | NULL | |
+---------------------+---------------+------+-----+---------------------+----------------+
13 rows in set (0.01 sec)
2. 查看表是否有特殊字符
select * from tap_application where ID=1 or ID=55\G
*************************** 1. row ***************************
ID: 1
VENDORID: 1
NAME: app name
DESCRIPTION: NULL
CREATIONDATE: 2009-03-30 18:40:36
MODIFICATIONDATE: 2009-03-30 18:40:36
STATUS: 1
URL: NULL
APPTYPE: NULL
TEST_KEY: AQAAASBaL2Igf/////////8AAAABAAAAAQEAAAAQnPDr8PHxexN/hwVcTYQL9AEAAAAOAwAAAAEAAAABAAAAAQA=
PRODUCTION_KEY: NULL
TEST_APISUSED: NULL
PRODUCTION_APISUSED: NULL
*************************** 2. row ***************************
ID: 55
VENDORID: 52
NAME: Raj Rao's test 1 application.
DESCRIPTION: <script type='text/javascript'>
alert("I am an alert box!");
</script> CREATIONDATE: 2010-04-09 15:20:53
MODIFICATIONDATE: 2010-06-04 09:09:01
STATUS: 1
URL: http://somewhere.a.b
APPTYPE: 1
TEST_KEY: NULL
PRODUCTION_KEY: NULL
TEST_APISUSED: NULL
PRODUCTION_APISUSED: NULL
#可以看到"TEST_KEY"字段有特殊字符存在,"DESCRIPTION"字段也有换行符等特殊字符存在,因此在导出时需要特别留意,实际上通过cat -v查看数据时可以看到windows换行符"\r\n"
3. 导出MySQL表数据到文本文件中
select count(*) from tap_application;
+----------+
| count(*) |
+----------+
| 847 |
+----------+
1 row in set (0.01 sec) select
IFNULL(ID,''),
IFNULL(VENDORID,''),
IFNULL(NAME,''),
REPLACE(IFNULL(DESCRIPTION,''),'\r\n','/$'),
IFNULL(CREATIONDATE,''),
IFNULL(MODIFICATIONDATE,''),
IFNULL(STATUS,''),
IFNULL(URL,''),
IFNULL(APPTYPE,''),
IFNULL(TEST_KEY,''),
IFNULL(PRODUCTION_KEY,''),
IFNULL(TEST_APISUSED,''),
IFNULL(PRODUCTION_APISUSED,'')
into outfile '/usr/local/mysql/dba/exp/tap_application.txt' fields terminated by '||' optionally enclosed by '^' lines terminated by '\r\n' from tap_application; Query OK, 847 rows affected (0.01 sec) scp /usr/local/mysql/dba/exp/tap_application.txt oracle@10.189.102.118:/u01/app/oracle/dba/imp/
4. 在Oracle数据库创建对应表结构
CREATE TABLE tap_application (
ID INT NOT NULL PRIMARY KEY,
VENDORID INT NOT NULL,
NAME VARCHAR2(128) NOT NULL,
DESCRIPTION VARCHAR2(255),
CREATIONDATE DATE NOT NULL,
MODIFICATIONDATE DATE NOT NULL,
STATUS INT NOT NULL,
URL VARCHAR2(1024),
APPTYPE INT,
TEST_KEY VARCHAR2(512),
PRODUCTION_KEY VARCHAR2(512),
PRODUCTION_APISUSED VARCHAR2(256)
);
5. 创建sqlldr控制文件
$ cat /u01/app/oracle/dba/imp/load_tap_application.ctl
load DATA
INFILE '/u01/app/oracle/dba/imp/tap_application.txt'
BADFILE '/u01/app/oracle/dba/imp/tap_application.bad'
DISCARDFILE '/u01/app/oracle/dba/imp/tap_application.dsc'
TRUNCATE
INTO TABLE TAP_APPLICATION
fields terminated by '||' optionally enclosed by '^'
TRAILING NULLCOLS
(
ID integer external,
VENDORID integer external,
NAME char(128),
DESCRIPTION char(255) "replace(:DESCRIPTION,'/$','\r\n')",
CREATIONDATE date "YYYY-MM-DD HH24:MI:SS",
MODIFICATIONDATE date "YYYY-MM-DD HH24:MI:SS",
STATUS integer external,
URL char(1024),
APPTYPE integer external,
TEST_KEY char(512),
PRODUCTION_KEY char(512),
PRODUCTION_APISUSED char(256)
)
6. 导入数据到Oracle
$ $ORACLE_HOME/bin/sqlldr system/888888 control=/u01/app/oracle/dba/imp/load_tap_application.ctl errors=50000 log=/u01/app/oracle/dba/imp/load_tap_application.log SQL*Loader: Release 11.2.0.4.0 - Production on Fri Jul 7 08:33:39 2017 Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved. Commit point reached - logical record count 60
Commit point reached - logical record count 120
Commit point reached - logical record count 180
Commit point reached - logical record count 240
Commit point reached - logical record count 300
Commit point reached - logical record count 360
Commit point reached - logical record count 420
Commit point reached - logical record count 480
Commit point reached - logical record count 540
Commit point reached - logical record count 600
Commit point reached - logical record count 660
Commit point reached - logical record count 720
Commit point reached - logical record count 780
Commit point reached - logical record count 840
Commit point reached - logical record count 847
7. 查看导入的log信息
$ cat /u01/app/oracle/dba/imp/load_tap_application.log SQL*Loader: Release 11.2.0.4. - Production on Fri Jul :: Copyright (c) , , Oracle and/or its affiliates. All rights reserved. Control File: /u01/app/oracle/dba/imp/load_tap_application.ctl
Data File: /u01/app/oracle/dba/imp/tap_application.txt
Bad File: /u01/app/oracle/dba/imp/tap_application.bad
Discard File: /u01/app/oracle/dba/imp/tap_application.dsc
(Allow all discards) Number to load: ALL
Number to skip:
Errors allowed:
Bind array: rows, maximum of bytes
Continuation: none specified
Path used: Conventional Table TAP_APPLICATION, loaded from every logical record.
Insert option in effect for this table: TRUNCATE
TRAILING NULLCOLS option in effect Column Name Position Len Term Encl Datatype
------------------------------ ---------- ----- ---- ---- ---------------------
ID FIRST * O(^) CHARACTER
Terminator string : '||'
VENDORID NEXT * O(^) CHARACTER
Terminator string : '||'
NAME NEXT O(^) CHARACTER
Terminator string : '||'
DESCRIPTION NEXT O(^) CHARACTER
Terminator string : '||'
SQL string for column : "replace(:DESCRIPTION,'/$','
')"
CREATIONDATE NEXT * O(^) DATE YYYY-MM-DD HH24:MI:SS
Terminator string : '||'
MODIFICATIONDATE NEXT * O(^) DATE YYYY-MM-DD HH24:MI:SS
Terminator string : '||'
STATUS NEXT * O(^) CHARACTER
Terminator string : '||'
URL NEXT O(^) CHARACTER
Terminator string : '||'
APPTYPE NEXT * O(^) CHARACTER
Terminator string : '||'
TEST_KEY NEXT O(^) CHARACTER
Terminator string : '||'
PRODUCTION_KEY NEXT O(^) CHARACTER
Terminator string : '||'
PRODUCTION_APISUSED NEXT O(^) CHARACTER
Terminator string : '||' value used for ROWS parameter changed from to Table TAP_APPLICATION:
Rows successfully loaded.
Rows not loaded due to data errors.
Rows not loaded because all WHEN clauses were failed.
Rows not loaded because all fields were null. Space allocated for bind array: bytes( rows)
Read buffer bytes: Total logical records skipped:
Total logical records read:
Total logical records rejected:
Total logical records discarded: Run began on Fri Jul ::
Run ended on Fri Jul :: Elapsed time was: ::00.44
CPU time was: ::00.01
利用sqlldr从MySQL导出一张表数据到Oracle的更多相关文章
- Mysql导出(多张表)表结构及表数据 mysqldump用法
命令行下具体用法如下: mysqldump -u用戶名 -p密码 -d 數據库名 表名 脚本名; 1.导出數據库為dbname的表结构(其中用戶名為root,密码為dbpasswd,生成的脚 ...
- MySQL实现两张表数据的同步
有两张表A和B,要求往A里面插入一条记录的同时要向B里面也插入一条记录,向B里面插入一条记录的同时也向A插入一条记录.两张表的结构不同,需要将其中几个字段对应起来.可以用下面的触发器实现. 表A的触发 ...
- mysql导出某张表的部分数据
.使用into outfile '保存到操作系统的外部文件路径' mysql -uroot -p123456 -hhostname -P3306 select column_name_list fro ...
- mysql导出多个表数据为excel方法,substring函数查询
//查询sys_username以S.00655开头的用户 ),sys_password FROM `tbl_sa_syslogin` where sys_username like 'S.%'; / ...
- mysql查询哪张表数据最大
转载:https://blog.csdn.net/qq13650793239/article/details/81142134 mysql数据库中information_schema 数据库存储了数据 ...
- sqlplus 导出一张表数据
内网只让用sql developer 这软件搓的不行,数据加载到51行就黑了,没法法用sqlplus. 打开cmd, sqlplus user/passwd@ip:port/库名set colsep ...
- 将Mysql的一张表导出至Excel格式文件
将Mysql的一张表导出至Excel格式文件 导出语句 进入mysql数据库,输入如下sql语句: select id, name, age from tablename into outfile ' ...
- MySQL实例多库某张表数据文件损坏导致xxx库无法访问故障恢复
一.问题发现 命令行进入数据库实例手动给某张表进行alter操作,发现如下报错. mysql> use xx_xxx; No connection. Trying to reconnect... ...
- [转]mysql导出导入中文表解决方法
在开发过程中会经常用到mysql导出导入中文表,本文将详细介绍其如何使用,需要的朋友可以参考下. 在开发过程中会经常用到mysql导出导入中文表,本文将详细介绍其如何使用,需要的朋友可以参考下一.先针 ...
随机推荐
- postgreSql——时区问题
timestamptz.timestamp SELECT ts AT TIME ZONE 'UTC' FROM ( VALUES (timestamptz '2012-03-05 17:00:00+0 ...
- DAG最小路径点覆盖
Problem 给出一个有向无环图 (\(DAG\)),求出最少使用其中多少条互不相交的路径覆盖所有点. Solution 若有 \(n\) 个点,对于每个点 \(i\) ,我们将它拆成两个点 \(i ...
- HDU 5791 Two(LCS求公共子序列个数)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5791 题意: 给出两个序列,求这两个序列的公共子序列的总个数. 思路: 和LCS差不多,dp[i][ ...
- sprinf sprintf_s 的用法
函数功能: 将数据格式化输出到字符串 函数原型: int sprintf( char *buffer, const char *format [,argument] ... ) 注意这里的buffer ...
- 主键非自增列 EF 插入数据库引起的 ID 列不能为 NULL 的错误
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<PostBo ...
- 【Python】【异步IO】
# [[异步IO]] # [协程] '''协程,又称微线程,纤程.英文名Coroutine. 协程的概念很早就提出来了,但直到最近几年才在某些语言(如Lua)中得到广泛应用. 子程序,或者称为函数,在 ...
- unity 截图 压缩 处理
/****************************************************** unity屏幕截图,并转换成Base64码* 作者: lyb* 日期:2017年7月25 ...
- Program type already present:okio.AsyncTimeout$Watchdog Message{kind=ERROR, text=Program type :okio
在app中的build.gradle中加入如下代码, configurations { all*.exclude group: 'com.google.code.gson' all*.exclude ...
- python 拷贝文件
使用绝对目录: import os import shutil shutil.copyfile("/opt/test/update.tar.gz","/opt/updat ...
- mysql 中判断表是否存在 以及表存在则删除
select * from information_schema.tables where table_name ='student';select * from information_schema ...