利用sqlldr从MySQL导出一张表数据到Oracle

时间:2022-01-15 23:40:36

根据业务需求,需要从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的更多相关文章

  1. Mysql导出(多张表)表结构及表数据 mysqldump用法

        命令行下具体用法如下:  mysqldump -u用戶名 -p密码 -d 數據库名 表名 脚本名; 1.导出數據库為dbname的表结构(其中用戶名為root,密码為dbpasswd,生成的脚 ...

  2. MySQL实现两张表数据的同步

    有两张表A和B,要求往A里面插入一条记录的同时要向B里面也插入一条记录,向B里面插入一条记录的同时也向A插入一条记录.两张表的结构不同,需要将其中几个字段对应起来.可以用下面的触发器实现. 表A的触发 ...

  3. mysql导出某张表的部分数据

    .使用into outfile '保存到操作系统的外部文件路径' mysql -uroot -p123456 -hhostname -P3306 select column_name_list fro ...

  4. mysql导出多个表数据为excel方法,substring函数查询

    //查询sys_username以S.00655开头的用户 ),sys_password FROM `tbl_sa_syslogin` where sys_username like 'S.%'; / ...

  5. mysql查询哪张表数据最大

    转载:https://blog.csdn.net/qq13650793239/article/details/81142134 mysql数据库中information_schema 数据库存储了数据 ...

  6. sqlplus 导出一张表数据

    内网只让用sql developer 这软件搓的不行,数据加载到51行就黑了,没法法用sqlplus. 打开cmd, sqlplus user/passwd@ip:port/库名set colsep ...

  7. 将Mysql的一张表导出至Excel格式文件

    将Mysql的一张表导出至Excel格式文件 导出语句 进入mysql数据库,输入如下sql语句: select id, name, age from tablename into outfile ' ...

  8. MySQL实例多库某张表数据文件损坏导致xxx库无法访问故障恢复

    一.问题发现 命令行进入数据库实例手动给某张表进行alter操作,发现如下报错. mysql> use xx_xxx; No connection. Trying to reconnect... ...

  9. [转]mysql导出导入中文表解决方法

    在开发过程中会经常用到mysql导出导入中文表,本文将详细介绍其如何使用,需要的朋友可以参考下. 在开发过程中会经常用到mysql导出导入中文表,本文将详细介绍其如何使用,需要的朋友可以参考下一.先针 ...

随机推荐

  1. Linq group

    using System;using System.Collections.Generic;using System.Linq; public class MyClass{ public static ...

  2. angularjs post

    /** * POST 1 * $http.post('http://localhost:8001/quickstart/task/create', { newTask: newTask }) */ / ...

  3. 工具: ass109.awk 分析 Oracle 的跟踪文件

    原文链接:http://www.eygle.com/archives/2009/11/awk_ass109.html 以前分析Oracle的跟踪文件,主要靠手工阅读,最近发现ass109.awk文件是 ...

  4. LA 3521 Joseph's Problem

    题意:给你正整数n和k,然后计算从i到n k%i的和: 思路:如果n小于1000000,直接暴力计算,然后大于1000000的情况,然后在讨论n和k的大小,根据k%i的情况,你会发现规律,是多个等差数 ...

  5. dirname(_file_) DIRECTORY_SEPARATOR

    <?php echo __FILE__ ; // 取得当前文件的绝对地址,结果:D:\www\test.php echo dirname(__FILE__); // 取得当前文件所在的绝对目录, ...

  6. logback Filter LevelFilter ThresholdFilter

    LevelFilter: 级别过滤器,根据日志级别进行过滤.如果日志级别等于配置级别,过滤器会根据onMath 和 onMismatch接收或拒绝日志.有以下子节点: <level>:设置 ...

  7. js原生事件系统与坐标系统

    今天来实现一个可兼容的js原生拖拽,在这里面我将会讲到: 1.封装兼容性的事件系统. 2.封装得到鼠标当前位置的系统. 3.完成拖拽的实现. 首先,我们要讲到鼠标位置的获取,讲到这个,就离不开js的w ...

  8. python sqlite3 数据库操作

    python sqlite3 数据库操作 SQLite3是python的内置模块,是一款非常小巧的嵌入式开源数据库软件. 1. 导入Python SQLite数据库模块 import sqlite3 ...

  9. mybatis ----&gt&semi; 各种方式使用MBG

    1.maven方式使用 配置好.pom文件 ①src/main/resources下创建 generatorConfig.xml,并配置好(自动生成的配置文件骨架) ②src/main/java 下创 ...

  10. NET设计模式 第二部分 行为型模式&lpar;17&rpar;:迭代器模式(Iterator Pattern)

    概述 在面向对象的软件设计中,我们经常会遇到一类集合对象,这类集合对象的内部结构可能有着各种各样的实现,但是归结起来,无非有两点是需要我们去关心的:一是集合内部的数据存储结构,二是遍历集合内部的数据. ...