外部表筆記 - 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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章