外部表笔记 - ORACLE_DATAPUMP驱动

参考文档(11gr2的):https://docs.oracle.com/cd/E11882_01/server.112/e22490/et_dp_driver.htm#SUTIL500

测试环境:RDBMS 19.0.0.0

文档中关于DATAPUMP驱动的外部表的一些说明

Unloading and Loading Data with the ORACLE_DATAPUMP Access Driver

As part of creating an external table with a SQL CREATE TABLE AS SELECT statement, the ORACLE_DATAPUMP access driver can write data to a dump file. The data in the file is written in a binary format that can only be read by the ORACLE_DATAPUMP access driver. Once the dump file is created, it cannot be modified (that is, no data manipulation language (DML) operations can be performed on it). However, the file can be read any number of times and used as the dump file for another external table in the same database or in a different database.

谷歌翻译下,大概如下:

作为使用SQL CREATE table As SELECT语句创建外部表的一部分,ORACLE DATAPUMP访问驱动程序可以将数据写入转储文件。文件中的数据以二进制格式写入,只能由ORACLE DATAPUMP访问驱动程序读取。一旦创建了转储文件,就不能对其进行修改(即不能对其执行任何数据操作语言(DML)操作)。但是,该文件可以被读取任意次数,并用作同一数据库或不同数据库中的另一个外部表的转储文件。

--外部表的并行

Parallel Loading and Unloading

The dump file must be on a disk big enough to hold all the data being written. If there is insufficient space for all of the data, then an error is returned for the CREATE TABLE AS SELECT statement. One way to alleviate the problem is to create multiple files in multiple directory objects (assuming those directories are on different disks) when executing the CREATE TABLE AS SELECT statement. Multiple files can be created by specifying multiple locations in the form directory:file in the LOCATION clause and by specifying the PARALLEL clause. Each parallel I/O server process that is created to populate the external table writes to its own file. The number of files in the LOCATION clause should match the degree of parallelization because each I/O server process requires its own files. Any extra files that are specified will be ignored. If there are not enough files for the degree of parallelization specified, then the degree of parallelization is lowered to match the number of files in the LOCATION clause.

-- 外部表的转储文件,可以被其他的外部表使用

Dump files populated by different external tables can all be specified in the LOCATION clause of another external table. For example, data from different production databases can be unloaded into separate files, and then those files can all be included in an external table defined in a data warehouse. This provides an easy way of aggregating data from multiple sources. The only restriction is that the metadata for all of the external tables be exactly the same. This means that the character set, time zone, schema name, table name, and column names must all match. Also, the columns must be defined in the same order, and their datatypes must be exactly alike. This means that after you create the first external table you must drop it so that you can use the same table name for the second external table. This ensures that the metadata listed in the two dump files is the same and they can be used together to create the same external table.

-- 以下是测试

查看文件夹的路径,并创建外部表

select owner,directory_name,directory_path from dba_directories where directory_name='DUMP';

create table emp_50
organization external 
(
type oracle_datapump
default directory DUMP
location ('emp_50.dmp')
)
as select * from hr.employees where department_id=50 

---文件夹是DUMP,路径在/u01/dump下,创建一个外部表emp_50后,会在DUMP下生成dmp文件emp_50.dmp

SYS@test>select owner,directory_name,directory_path from dba_directories where directory_name='DUMP';

OWNE DIRECT DIRECTORY_PATH
---- ------ --------------------
SYS  DUMP   /u01/dump

SYS@test>

BB@test>create table emp_50
organization external
(
type oracle_datapump
default directory DUMP
location ('emp_50.dmp')
)
as select * from hr.employees where department_id=50   2    3    4    5    6    7    8
  9  ;

表已创建。

BB@test>

[oracle@redhat762100 dump]$ ll *.dmp
-rw-r-----. 1 oracle oinstall 16384 Oct 21 13:01 emp_50.dmp
[oracle@redhat762100 dump]$ 

[oracle@redhat762100 dump]$ cp emp_50.dmp emp_50_1.dmp
[oracle@redhat762100 dump]$ 

-- 外部表创建的文件,只能被外部表访问,是不可以被其他访问的,比如,使用impdp访问生成sql文件,会提示错误

[oracle@redhat762100 dump]$ impdp system/oracle directory=DUMP dumpfile=emp_50.dmp logfile=emp_50_1.log sqlfile=emp_50_1.sql 

Import: Release 19.0.0.0.0 - Production on 星期一 10月 21 14:47:42 2019
Version 19.3.0.0.0

Copyright (c) 1982, 2019, Oracle and/or its affiliates.  All rights reserved.

连接到: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
ORA-39001: 参数值无效
ORA-39000: 转储文件说明错误
ORA-39323: 无法从外部表转储文件 "/u01/dump/emp_50.dmp" 加载数据

-- 可以使用linux的strings命令来查看dmp文件的内容:

[oracle@redhat762100 dump]$ strings emp_50_1.dmp 
"BB"."U"
x86_64/Linux 2.4.xx
AL32UTF8
19.00.00.00.00
001:001:000001:000001
i<?xml version="1.0" encoding="UTF-8"?><ROWSET><ROW><STRMTABLE_T><VER_MAJOR>1</VER_MAJOR><VER_MINOR>0</VER_MINOR><VERS_DPAPI>3</VERS_DPAPI><ENDIANNESS>2</ENDIANNESS><CHARSET>AL32UTF8</CHARSET><NCHARSET>AL16UTF16</NCHARSET><DBTIMEZONE>+00:00</DBTIMEZONE><OWNER_NAME>BB</OWNER_NAME><NAME>EMP_50</NAME><COL_LIST><COL_LIST_ITEM><COL_NUM>1</COL_NUM><NAME>EMPLOYEE_ID</NAME><TYPE_NUM>2</TYPE_NUM><LENGTH>22</LENGTH><PRECISION_NUM>6</PRECISION_NUM><SCALE>0</SCALE><CHARSETID>0</CHARSETID><CHARSETFORM>0</CHARSETFORM><CHARLENGTH>0</CHARLENGTH></COL_LIST_ITEM><COL_LIST_ITEM><COL_NUM>2</COL_NUM><NAME>FIRST_NAME</NAME><TYPE_NUM>1</TYPE_NUM><LENGTH>20</LENGTH><CHARSETID>873</CHARSETID><CHARSETFORM>1</CHARSETFORM><CHARLENGTH>20</CHARLENGTH></COL_LIST_ITEM><COL_LIST_ITEM><COL_NUM>3</COL_NUM><NAME>LAST_NAME</NAME><TYPE_NUM>1</TYPE_NUM><LENGTH>25</LENGTH><CHARSETID>873</CHARSETID><CHARSETFORM>1</CHARSETFORM><CHARLENGTH>25</CHARLENGTH></COL_LIST_ITEM><COL_LIST_ITEM><COL_NUM>4</COL_NUM><NAME>EMAIL</NAME><TYPE_NUM>1</TYPE_NUM><LENGTH>25</LENGTH><CHARSETID>873</CHARSETID><CHARSETFORM>1</CHARSETFORM><CHARLENGTH>25</CHARLENGTH></COL_LIST_ITEM><COL_LIST_ITEM><COL_NUM>5</COL_NUM><NAME>PHONE_NUMBER</NAME><TYPE_NUM>1</TYPE_NUM><LENGTH>20</LENGTH><CHARSETID>873</CHARSETID><CHARSETFORM>1</CHARSETFORM><CHARLENGTH>20</CHARLENGTH></COL_LIST_ITEM><COL_LIST_ITEM><COL_NUM>6</COL_NUM><NAME>HIRE_DATE</NAME><TYPE_NUM>12</TYPE_NUM><LENGTH>7</LENGTH><CHARSETID>0</CHARSETID><CHARSETFORM>0</CHARSETFORM><CHARLENGTH>0</CHARLENGTH></COL_LIST_ITEM><COL_LIST_ITEM><COL_NUM>7</COL_NUM><NAME>JOB_ID</NAME><TYPE_NUM>1</TYPE_NUM><LENGTH>10</LENGTH><CHARSETID>873</CHARSETID><CHARSETFORM>1</CHARSETFORM><CHARLENGTH>10</CHARLENGTH></COL_LIST_ITEM><COL_LIST_ITEM><COL_NUM>8</COL_NUM><NAME>SALARY</NAME><TYPE_NUM>2</TYPE_NUM><LENGTH>22</LENGTH><PRECISION_NUM>8</PRECISION_NUM><SCALE>2</SCALE><CHARSETID>0</CHARSETID><CHARSETFORM>0</CHARSETFORM><CHARLENGTH>0</CHARLENGTH></COL_LIST_ITEM><COL_LIST_ITEM><COL_NUM>9</COL_NUM><NAME>COMMISSION_PCT</NAME><TYPE_NUM>2</TYPE_NUM><LENGTH>22</LENGTH><PRECISION_NUM>2</PRECISION_NUM><SCALE>2</SCALE><CHARSETID>0</CHARSETID><CHARSETFORM>0</CHARSETFORM><CHARLENGTH>0</CHARLENGTH></COL_LIST_ITEM><COL_LIST_ITEM><COL_NUM>10</COL_NUM><NAME>MANAGER_ID</NAME><TYPE_NUM>2</TYPE_NUM><LENGTH>22</LENGTH><PRECISION_NUM>6</PRECISION_NUM><SCALE>0</SCALE><CHARSETID>0</CHARSETID><CHARSETFORM>0</CHARSETFORM><CHARLENGTH>0</CHARLENGTH></COL_LIST_ITEM><COL_LIST_ITEM><COL_NUM>11</COL_NUM><NAME>DEPARTMENT_ID</NAME><TYPE_NUM>2</TYPE_NUM><LENGTH>22</LENGTH><PRECISION_NUM>4</PRECISION_NUM><SCALE>0</SCALE><CHARSETID>0</CHARSETID><CHARSETFORM>0</CHARSETFORM><CHARLENGTH>0</CHARLENGTH></COL_LIST_ITEM></COL_LIST></STRMTABLE_T></ROW></ROWSET>
Matthew
Weiss
MWEISS
650.123.1234
ST_MAN
Adam
Fripp
AFRIPP
650.123.2234
ST_MAN
Payam
Kaufling
PKAUFLIN
650.123.3234
ST_MAN
Shanta
Vollman
SVOLLMAN
650.123.4234
ST_MAN
Kevin
Mourgos
KMOURGOS
650.123.5234
ST_MAN
Julia
Nayer
JNAYER
650.124.1214
ST_CLERK
Irene
Mikkilineni
IMIKKILI
650.124.1224
ST_CLERK
James
Landry
JLANDRY
650.124.1334
ST_CLERK
Steven
Markle
SMARKLE
650.124.1434
ST_CLERK
Laura
Bissot
LBISSOT
650.124.5234
ST_CLERK
Mozhe
Atkinson
MATKINSO
650.124.6234
ST_CLERK
James
Marlow
JAMRLOW
650.124.7234
ST_CLERK
Olson
TJOLSON
650.124.8234
ST_CLERK
Jason
Mallin
JMALLIN
650.127.1934
ST_CLERK
Michael
Rogers
MROGERS
650.127.1834
ST_CLERK
KGEE
650.127.1734
ST_CLERK
Hazel
Philtanker
HPHILTAN
650.127.1634
ST_CLERK
Renske
Ladwig
RLADWIG
650.121.1234
ST_CLERK
Stephen
Stiles
SSTILES
650.121.2034
ST_CLERK
John
JSEO
650.121.2019
ST_CLERK
Joshua
Patel
JPATEL
650.121.1834
ST_CLERK
Trenna
Rajs
TRAJS
650.121.8009
ST_CLERK
Curtis
Davies
CDAVIES
650.121.2994
ST_CLERK
Randall
Matos
RMATOS
650.121.2874
ST_CLERK
Peter
Vargas
PVARGAS
650.121.2004
ST_CLERK
Winston
Taylor
WTAYLOR
650.507.9876
SH_CLERK
Jean
Fleaur
JFLEAUR
650.507.9877
SH_CLERK
Martha
Sullivan
MSULLIVA
650.507.9878
SH_CLERK
Girard
Geoni
GGEONI
650.507.9879
SH_CLERK
Nandita
Sarchand
NSARCHAN
650.509.1876
SH_CLERK
Alexis
Bull
ABULL
650.509.2876
SH_CLERK
Julia	Dellinger
JDELLING
650.509.3876
SH_CLERK
Anthony
Cabrio
ACABRIO
650.509.4876
SH_CLERK
Kelly
Chung
KCHUNG
650.505.1876
SH_CLERK
Jennifer
Dilly
JDILLY
650.505.2876
SH_CLERK
Timothy
Gates
TGATES
650.505.3876
SH_CLERK
Randall
Perkins
RPERKINS
650.505.4876
SH_CLERK
Sarah
Bell
SBELL
650.501.1876
SH_CLERK
Britney
Everett
BEVERETT
650.501.2876
SH_CLERK
Samuel
McCain
SMCCAIN
650.501.3876
SH_CLERK
Vance
Jones
VJONES
650.501.4876
SH_CLERK
Alana
Walsh
AWALSH
650.507.9811
SH_CLERK
Kevin
Feeney
KFEENEY
650.507.9822
SH_CLERK
Donald
OConnell
DOCONNEL
650.507.9833
SH_CLERK
Douglas
Grant
DGRANT
650.507.9844
SH_CLERK
[oracle@redhat762100 dump]$ 

-- 创建一个外部表,并行度3,这样会在DUMP文件夹下生成3个dmp文件。

create table dba_objects_ex
organization external 
(
type oracle_datapump
default directory DUMP
location ('dba_object01.dmp','dba_object02.dmp','dba_object03.dmp')
)
parallel 3 
as select * from sys.dba_objects


SYS@test>create table dba_objects_ex
organization external
(
type oracle_datapump
default directory DUMP
location ('dba_object01.dmp','dba_object02.dmp','dba_object03.dmp')
)
parallel 3
as select * from sys.dba_objects  2    3    4    5    6    7    8    9
 10  ;

表已创建。

SYS@test>


[oracle@redhat762100 dump]$ ll *.dmp
-rw-r-----. 1 oracle oinstall 3432448 Oct 21 14:57 dba_object01.dmp
-rw-r-----. 1 oracle oinstall 3358720 Oct 21 14:57 dba_object02.dmp
-rw-r-----. 1 oracle oinstall 3309568 Oct 21 14:57 dba_object03.dmp
-rw-r-----. 1 oracle oinstall   16384 Oct 21 13:02 emp_50_1.dmp
-rw-r-----. 1 oracle oinstall   16384 Oct 21 13:01 emp_50.dmp
[oracle@redhat762100 dump]$ 

-- 创建一个外部表,使用之前的dmp文件,使用上面的外部表生成的dba_object01-03.dmp文件

create table DBA_OBJECTS_EX1
(
  owner             VARCHAR2(128),
  object_name       VARCHAR2(128),
  subobject_name    VARCHAR2(128),
  object_id         NUMBER,
  data_object_id    NUMBER,
  object_type       VARCHAR2(23),
  created           DATE,
  last_ddl_time     DATE,
  timestamp         VARCHAR2(19),
  status            VARCHAR2(7),
  temporary         VARCHAR2(1),
  generated         VARCHAR2(1),
  secondary         VARCHAR2(1),
  namespace         NUMBER,
  edition_name      VARCHAR2(128),
  sharing           VARCHAR2(18),
  editionable       VARCHAR2(1),
  oracle_maintained VARCHAR2(1),
  application       VARCHAR2(1),
  default_collation VARCHAR2(100),
  duplicated        VARCHAR2(1),
  sharded           VARCHAR2(1),
  created_appid     NUMBER,
  created_vsnid     NUMBER,
  modified_appid    NUMBER,
  modified_vsnid    NUMBER
)
organization external 
(
type oracle_datapump
default directory DUMP
location ('dba_object01.dmp','dba_object02.dmp','dba_object03.dmp')
)
;

SYS@test>create table DBA_OBJECTS_EX1
  2  (
  3    owner             VARCHAR2(128),
  object_name       VARCHAR2(128),
  subobject_name    VARCHAR2(128),
  4    5    6    object_id         NUMBER,
  data_object_id    NUMBER,
  object_type       VARCHAR2(23),
  created           DATE,
  last_ddl_time     DATE,
  7    timestamp         VARCHAR2(19),
  status            VARCHAR2(7),
  temporary         VARCHAR2(1),
  generated         VARCHAR2(1),
  secondary         VARCHAR2(1),
  8    namespace         NUMBER,
  edition_name      VARCHAR2(128),
  sharing           VARCHAR2(18),
  9   10   11   12   13   14   15   16   17   18   19    editionable       VARCHAR2(1),
  oracle_maintained VARCHAR2(1),
  application       VARCHAR2(1),
  default_collation VARCHAR2(100),
  duplicated        VARCHAR2(1),
  sharded           VARCHAR2(1),
  created_appid     NUMBER,
  created_vsnid     NUMBER,
  modified_appid    NUMBER,
  modified_vsnid    NUMBER
)
organization external
(
type oracle_datapump
default directory DUMP
location ('dba_object01.dmp','dba_object02.dmp','dba_object03.dmp')
)
; 20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36

表已创建。

SYS@test>

-- 当然,也可以使用上面的外部表产生的dmp文件中的任何一个,这里测试使用上面的外部表产生的其中两个dmp文件

create table DBA_OBJECTS_EX2
(
  owner             VARCHAR2(128),
  object_name       VARCHAR2(128),
  subobject_name    VARCHAR2(128),
  object_id         NUMBER,
  data_object_id    NUMBER,
  object_type       VARCHAR2(23),
  created           DATE,
  last_ddl_time     DATE,
  timestamp         VARCHAR2(19),
  status            VARCHAR2(7),
  temporary         VARCHAR2(1),
  generated         VARCHAR2(1),
  secondary         VARCHAR2(1),
  namespace         NUMBER,
  edition_name      VARCHAR2(128),
  sharing           VARCHAR2(18),
  editionable       VARCHAR2(1),
  oracle_maintained VARCHAR2(1),
  application       VARCHAR2(1),
  default_collation VARCHAR2(100),
  duplicated        VARCHAR2(1),
  sharded           VARCHAR2(1),
  created_appid     NUMBER,
  created_vsnid     NUMBER,
  modified_appid    NUMBER,
  modified_vsnid    NUMBER
)
organization external 
(
type oracle_datapump
default directory DUMP
location ('dba_object01.dmp','dba_object03.dmp')
)
;

SYS@test>create table DBA_OBJECTS_EX2
(
  owner             VARCHAR2(128),
  object_name       VARCHAR2(128),
  subobject_name    VARCHAR2(128),
  object_id         NUMBER,
  data_object_id    NUMBER,
  object_type       VARCHAR2(23),
  created           DATE,
  last_ddl_time     DATE,
  timestamp         VARCHAR2(19),
  status            VARCHAR2(7),
  temporary         VARCHAR2(1),
  2    3    4    5    6    7    8    9   10   11   12   13   14    generated         VARCHAR2(1),
  secondary         VARCHAR2(1),
  namespace         NUMBER,
  edition_name      VARCHAR2(128),
  sharing           VARCHAR2(18),
  editionable       VARCHAR2(1),
  oracle_maintained VARCHAR2(1),
  application       VARCHAR2(1),
  default_collation VARCHAR2(100),
  duplicated        VARCHAR2(1),
  sharded           VARCHAR2(1),
  created_appid     NUMBER,
  created_vsnid     NUMBER,
  modified_appid    NUMBER,
  modified_vsnid    NUMBER
)
organization external
(
type oracle_datapump
default directory DUMP
location ('dba_object01.dmp','dba_object03.dmp')
)
;

 15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36
表已创建。

SYS@test>SYS@test>

-- 查询数据,可以看到,是可以使用其中任何一个dmp文件创建外部表的。

SYS@test>select count(*) from DBA_OBJECTS_EX1 union all select count(*) from DBA_OBJECTS_EX2;

  COUNT(*)
----------
     72887
     48887

SYS@test>

-- drop掉外部表,并不会删除该外部表的dmp文件(自行测试,这里不写了)

-- 补充,比较适合的一个场景,每天需要将数据导入到测试环境进行查看,可以在测试环境创建一个外部表。每天从生产环境导出dmp文件,拷贝出来,覆盖测试环境的文件即可。

END

发布了754 篇原创文章 · 获赞 31 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/xxzhaobb/article/details/102665409