ITPub博客

首页 > 数据库 > Oracle > OCM实验-外部表

OCM实验-外部表

原创 Oracle 作者:super_sky 时间:2014-01-20 15:27:11 0 删除 编辑

要求从PROD数据库卸载数据,然后再OCM2库加载该数据,使用数据泵的方式。

外部表适用于导出的数据可用于二次开发或者数据迁移

 

1)从PROD数据库导出数据

SYS@PROD>create user exp identified by exp default tablespace users;

User created.

SYS@PROD>grant dba to exp;

Grant succeeded.

SYS@PROD>create directory dir_tmp as '/home/oracle';

Directory created.

SYS@PROD>grant read,write on directory dir_tmp to exp;

Grant succeeded.

创建外部表,使用数据泵卸载数据

SYS@PROD>conn exp/exp
Connected.
EXP@PROD>
EXP@PROD>create table t organization external                                                
  2  (type oracle_datapump
  3  default directory dir_tmp
  4  location ('t_part1_dump.dat','t_part2_dump.dat')
  5  )
  6  parallel 2  
  7  as
  8  select owner,object_id,object_name from dba_objects where owner='SYSTEM';

Table created.

EXP@PROD>

2)将生成的文件传递到OCM2主机

-rw-r----- 1 oracle oinstall     20480 Jan 20 14:32 t_part1_dump.dat
-rw-r----- 1 oracle oinstall     20480 Jan 20 14:32 t_part2_dump.dat

[oracle@ocm1 ~]$ scp t_part* oracle@ocm2:~
t_part1_dump.dat                                                                                                          100%   20KB  20.0KB/s   00:00   
t_part2_dump.dat                                                                                                          100%   20KB  20.0KB/s   00:00

3)在OCM2库,建imp账户并创建外部表t1表来加载数据

SYS@OCM2>grant dba to imp;  

Grant succeeded.

SYS@OCM2>create directory dir_tmp as '/home/orace';

Directory created.

grant">SYS@OCM2>grant read,write on directory dir_tmp to imp;

Grant succeeded.

SYS@OCM2>conn imp/imp
Connected.

创建外部表并加载数据

IMP@OCM2>create table t1 (owner varchar2(30),object_id number,object_name varchar2(128)) organization external
  2  (type oracle_datapump
  3  default directory dir_tmp
  4  location ('t_part1_dump.dat','t_part2_dump.dat'));

Table created.

测试

IMP@OCM2>select count(*) from t1;
select count(*) from t1
*
ERROR at line 1:
ORA-29913: error in executing ODCIEXTTABLEOPEN callout
ORA-29400: data cartridge error
KUP-04063: unable to open log file T1_31553.log
OS error No such file or directory
ORA-06512: at "SYS.ORACLE_DATAPUMP", line 19

检查是否可以使用expdp 导出

[oracle@ocm2 ~]$ expdp imp/imp  directory=dir_tmp dumpfile=objects.dat

Export: Release 10.2.0.1.0 - Production on Monday, 20 January, 2014 14:49:06

Copyright (c) 2003, 2005, Oracle.  All rights reserved.

Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
ORA-39002: invalid operation
ORA-39070: Unable to open the log file.
ORA-29283: invalid file operation
ORA-06512: at "SYS.UTL_FILE", line 475
ORA-29283: invalid file operation

检查了半天,原来是路径写错误了。哎。

SYS@OCM2>drop directory dir_tmp;

Directory dropped.

SYS@OCM2>create directory dir_tmp as '/home/oracle';

Directory created.

SYS@OCM2>grant read,write on directory dir_tmp to imp;

Grant succeeded.

SYS@OCM2>conn imp/imp
Connected.
IMP@OCM2>
IMP@OCM2>
IMP@OCM2>select count(*) from t1;

  COUNT(*)
----------
       449

IMP@OCM2>

深入学习一下

type参数:

Oracle Database provides two access drivers for external tables. The default access driver is ORACLE_LOADER, which allows the reading of data from external files using the Oracle loader technology. The ORACLE_LOADER access driver provides data mapping capabilities which are a subset of the control file syntax of SQL*Loader utility. The second access driver, ORACLE_DATAPUMP, lets you unload data--that is, read data from the database and insert it into an external table, represented by one or more external files--and then reload it into an Oracle Database.

使用sql loader 方式创建外部表并加载数据

1)新建两个外部文件

[oracle@ocm1 ~]$ cat empxt1.dat
360,Jane,Janus,ST_CLERK,121,17-MAY-2001,3000,0,50,jjanus
361,Mark,Jasper,SA_REP,145,17-MAY-2001,8000,.1,80,mjasper
362,Brenda,Starr,AD_ASST,200,17-MAY-2001,5500,0,10,bstarr
363,Alex,Alda,AC_MGR,145,17-MAY-2001,9000,.15,80,aalda
[oracle@ocm1 ~]$ cat empxt2.dat
401,Jesse,Cromwell,HR_REP,203,17-MAY-2001,7000,0,40,jcromwel
402,Abby,Applegate,IT_PROG,103,17-MAY-2001,9000,.2,60,aapplega
403,Carol,Cousins,AD_VP,100,17-MAY-2001,27000,.3,90,ccousins
404,John,Richardson,AC_ACCOUNT,205,17-MAY-2001,5000,0,110,jrichard

2)创建外部表

这个过程使用上面测试中的directory dir_tmp 。

EXP@PROD>create table employees
  2  (employee_id       NUMBER(4),
  3  first_name        VARCHAR2(20),
  4  last_name         VARCHAR2(25),
  5  job_id            VARCHAR2(10),
  6  manager_id        NUMBER(4),
  7  hire_date         DATE,
  8  salary            NUMBER(8,2),
  9  commission_pct    NUMBER(2,2),
10  department_id     NUMBER(4),
11  email             VARCHAR2(25))
12  organization external
13  (type oracle_loader
14  default directory dir_tmp
15  ACCESS PARAMETERS
16         (
17           records delimited by newline
18           badfile dir_tmp:'empxt%a_%p.bad'
19           logfile dir_tmp:'empxt%a_%p.log'
20           fields terminated by ','
21           missing field values are null
22           ( employee_id, first_name, last_name, job_id, manager_id,
23             hire_date char date_format date mask "dd-mon-yyyy",
24             salary, commission_pct, department_id, email
25           )
26  )
27  LOCATION ('empxt1.dat', 'empxt2.dat')
28  )
29  PARALLEL
30  REJECT LIMIT UNLIMITED;

Table created.

测试:

EXP@PROD>select count(*) from employees;

  COUNT(*)
----------
         8

EXP@PROD>

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/11590946/viewspace-1073126/,如需转载,请注明出处,否则将追究法律责任。

上一篇: OCM实验-物化视图
请登录后发表评论 登录
全部评论

注册时间:2011-01-11

  • 博文量
    77
  • 访问量
    757586