【12c】Oracle 12c External Table 外部表

外部表在Oracle 9i中引入,外部表的特性是对现有SQL*Loader功能的补充,它能够使您访问外部数据源中的数据,就像访问数据库中的表一样。本篇就Oracle 12c中的外部表进行总结和演示,演示环境为Oracle Database 12c Enterprise Edition Release 12.1.0.2.0。

1 外部表介绍

外部表是通过create table ... organization external语句创建的,当创建外部表时,需指定如下属性:

  • TYPE:指定外部表的类型,即访问驱动,访问驱动运行在服务器内部的,这点和SQL*Loader不同,有两种可用的类型,分别为:
    • ORACLE_LOADER:默认的访问驱动,它可以将数据从外部表加载到内部表,数据必须来自文本数据文件,但它不能将数据从内部表卸载到外部表;
    • ORACLE_DATAPUMP:该访问驱动可以执行数据的加载和卸载,数据必须来自二进制转储文件,从外部表加载到内部表是通过从二进制转储文件中提取来完成,而从内部表卸载到外部表是通过填充外部表的二进制转储文件来完成的。转储文件一旦创建,可以读取任意次数,但不能被修改,也就是说,不能执行任何DML语句;
  • DEFAULT DIRECTORY:指定所有输入输出文件使用的默认目录,位置是一个目录对象,而不是一个目录路径,在创建外部表前必须先创建目录对象,否则,会出错;
  • ACCESS PARAMETERS:描述外部数据源,并实现指定的外部表的类型,每一种类型的外部表都有自己的访问驱动程序,该驱动程序为该类型的外部表提供唯一的访问参数;
  • LOCATION:指定外部表的数据文件,文件命名方式为directory:file,其中,目录是可选的,若不指定,默认为默认目录。

2 外部表创建

外部表的创建可以通过两种访问驱动实现,下面将分别介绍。

2.1 创建目录对象

SQL> ho mkdir /home/oracle/ext



SQL> create directory ext_dir as '/home/oracle/ext';



Directory created.

SQL> grant read,write on directory ext_dir to alen;



Grant succeeded.

2.2 ORACLE_LOAD访问驱动

2.2.1 手动创建外部表

创建外部表时如果数据文件不是文本文件,则需要使用preprocessor子句。

1)创建测试数据

[oracle@odd ext]$ cat emp.dat

"EMPNO","ENAME","JOB","MGR","HIREDATE","SAL","COMM","DEPTNO"

"7369","SMITH","CLERK","7902","1980/12/17","800.00","","20"

"7499","ALLEN","SALESMAN","7698","1981/2/20","1600.00","300.00","30"

"7521","WARD","SALESMAN","7698","1981/2/22","1250.00","500.00","30"

"7566","JONES","MANAGER","7839","1981/4/2","2975.00","","20"

"7654","MARTIN","SALESMAN","7698","1981/9/28","1250.00","1400.00","30"

"7698","BLAKE","MANAGER","7839","1981/5/1","2850.00","","30"

"7782","CLARK","MANAGER","7839","1981/6/9","2450.00","","10"

"7788","SCOTT","ANALYST","7566","1987/4/19","3000.00","","20"

"7839","KING","PRESIDENT","","1981/11/17","5000.00","","10"

"7844","TURNER","SALESMAN","7698","1981/9/8","1500.00","0.00","30"

"7876","ADAMS","CLERK","7788","1987/5/23","1100.00","","20"

"7900","JAMES","CLERK","7698","1981/12/3","950.00","","30"

"7902","FORD","ANALYST","7566","1981/12/3","3000.00","","20"

"7934","MILLER","CLERK","7782","1982/1/23","1300.00","","10"

2)创建外部表

SQL> conn alen/alen@odd

Connected.

SQL> ho cat /home/oracle/ext/emp.sql

create table emp

(

empno number(4),

ename varchar2(10),

job varchar2(9),

mgr number(4),

hiredate date,

sal number(7,2),

comm number(7,2),

deptno number(2)

)

organization external --指定创建的表是外部表

(

type oracle_loader --外部表访问驱动类型

default directory ext_dir --默认目录,需提前创建

access parameters --设置访问参数,注意先后顺序,否则出错

(

records delimited by newline

badfile ext_dir:'emp.bad'

discardfile ext_dir:'emp.dsc'

logfile ext_dir:'emp.log'

field names all files ignore

fields terminated by ',' optionally enclosed by '"' ldrtrim

reject rows with all null fields

(empno,

ename,

job,

mgr,

hiredate char(10) date_format date mask 'yyyy-mm-dd',

sal,

comm,

deptno

))

location('emp.dat')

)reject limit unlimited;

SQL> @emp.sql



Table created.



SQL> set linesize 200

SQL> select * from emp;



EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

---------- ---------- --------- ---------- --------- ---------- ---------- ----------

7369 SMITH CLERK 7902 17-DEC-80 800 20

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7566 JONES MANAGER 7839 02-APR-81 2975 20

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7698 BLAKE MANAGER 7839 01-MAY-81 2850 30

7782 CLARK MANAGER 7839 09-JUN-81 2450 10

7788 SCOTT ANALYST 7566 19-APR-87 3000 20

7839 KING PRESIDENT 17-NOV-81 5000 10

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

7876 ADAMS CLERK 7788 23-MAY-87 1100 20



EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

---------- ---------- --------- ---------- --------- ---------- ---------- ----------

7900 JAMES CLERK 7698 03-DEC-81 950 30

7902 FORD ANALYST 7566 03-DEC-81 3000 20

7934 MILLER CLERK 7782 23-JAN-82 1300 10



14 rows selected.

2.2.2 通过sqlldr创建外部表

外部表可以手动写SQL,也可以使用sqlldr工具生成外部表,下面是具体创建过程。

1)创建控制文件


[oracle@odd ext]$ cat alen.ctl

load data

field names all files ignore

infile 'emp.dat'

badfile 'emp.bad'

discardfile 'emp.dsc'

into table alen

fields terminated by ',' optionally enclosed by '"'

(empno,

ename,

job,

mgr,

hiredate date 'yyyy-mm-dd',

sal,

comm,

deptno )

2)sqlldr利用generate_only生成外部表

[oracle@odd ext]$ sqlldr alen/alen@odd control=alen.ctl external_table=generate_only log=alen.log



SQL*Loader: Release 12.1.0.2.0 - Production on Wed May 13 22:08:47 2020



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



Path used: External Table

3)查看日志

[oracle@odd ext]$ cat alen.log



SQL*Loader: Release 12.1.0.2.0 - Production on Wed May 13 22:08:47 2020



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



Control File: alen.ctl

Data File: emp.dat

Bad File: emp.bad

Discard File: emp.dsc

(Allow all discards)



Number to load: ALL

Number to skip: 0

Errors allowed: 50

Continuation: none specified

Path used: External Table



Table ALEN, loaded from every logical record.

Insert option in effect for this table: INSERT



Column Name Position Len Term Encl Datatype

------------------------------ ---------- ----- ---- ---- ---------------------

EMPNO FIRST * , O(") CHARACTER

ENAME NEXT * , O(") CHARACTER

JOB NEXT * , O(") CHARACTER

MGR NEXT * , O(") CHARACTER

HIREDATE NEXT * , O(") DATE 'yyyy-mm-dd'

SAL NEXT * , O(") CHARACTER

COMM NEXT * , O(") CHARACTER

DEPTNO NEXT * , O(") CHARACTER







CREATE TABLE statement for external table:

------------------------------------------------------------------------

CREATE TABLE "SYS_SQLLDR_X_EXT_ALEN"

(

"EMPNO" NUMBER(4),

"ENAME" VARCHAR2(10),

"JOB" VARCHAR2(9),

"MGR" NUMBER(4),

"HIREDATE" DATE,

"SAL" NUMBER(7,2),

"COMM" NUMBER(7,2),

"DEPTNO" NUMBER(2)

)

ORGANIZATION external

(

TYPE oracle_loader

DEFAULT DIRECTORY EXT_DIR

ACCESS PARAMETERS

(

RECORDS DELIMITED BY NEWLINE CHARACTERSET US7ASCII

BADFILE 'EXT_DIR':'emp.bad'

DISCARDFILE 'EXT_DIR':'emp.dsc'

LOGFILE 'alen.log_xt'

READSIZE 1048576

FIELD NAMES ALL FILES IGNORE

FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY '"' LDRTRIM

REJECT ROWS WITH ALL NULL FIELDS

(

"EMPNO" CHAR(255)

TERMINATED BY "," OPTIONALLY ENCLOSED BY '"',

"ENAME" CHAR(255)

TERMINATED BY "," OPTIONALLY ENCLOSED BY '"',

"JOB" CHAR(255)

TERMINATED BY "," OPTIONALLY ENCLOSED BY '"',

"MGR" CHAR(255)

TERMINATED BY "," OPTIONALLY ENCLOSED BY '"',

"HIREDATE" CHAR(255)

TERMINATED BY "," OPTIONALLY ENCLOSED BY '"'

DATE_FORMAT DATE MASK 'yyyy-mm-dd',

"SAL" CHAR(255)

TERMINATED BY "," OPTIONALLY ENCLOSED BY '"',

"COMM" CHAR(255)

TERMINATED BY "," OPTIONALLY ENCLOSED BY '"',

"DEPTNO" CHAR(255)

TERMINATED BY "," OPTIONALLY ENCLOSED BY '"'

)

)

location

(

'emp.dat'

)

)REJECT LIMIT UNLIMITED





INSERT statements used to load internal tables:

------------------------------------------------------------------------

INSERT /*+ append */ INTO ALEN

(

EMPNO,

ENAME,

JOB,

MGR,

HIREDATE,

SAL,

COMM,

DEPTNO

)

SELECT

"EMPNO",

"ENAME",

"JOB",

"MGR",

"HIREDATE",

"SAL",

"COMM",

"DEPTNO"

FROM "SYS_SQLLDR_X_EXT_ALEN"





statements to cleanup objects created by previous statements:

------------------------------------------------------------------------

DROP TABLE "SYS_SQLLDR_X_EXT_ALEN"







Run began on Wed May 13 22:08:47 2020

Run ended on Wed May 13 22:08:47 2020



Elapsed time was: 00:00:00.47

CPU time was: 00:00:00.01

4)利用上述sql创建外部表SQL

[oracle@odd ext]$ cat alen.sql

CREATE TABLE alen

(

"EMPNO" NUMBER(4),

"ENAME" VARCHAR2(10),

"JOB" VARCHAR2(9),

"MGR" NUMBER(4),

"HIREDATE" DATE,

"SAL" NUMBER(7,2),

"COMM" NUMBER(7,2),

"DEPTNO" NUMBER(2)

)

ORGANIZATION external

(

TYPE oracle_loader

DEFAULT DIRECTORY EXT_DIR

ACCESS PARAMETERS

(

RECORDS DELIMITED BY NEWLINE CHARACTERSET US7ASCII

BADFILE 'EXT_DIR':'emp.bad'

DISCARDFILE 'EXT_DIR':'emp.dsc'

LOGFILE 'alen.log_xt'

READSIZE 1048576

FIELD NAMES ALL FILES IGNORE

FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY '"' LDRTRIM

REJECT ROWS WITH ALL NULL FIELDS

(

"EMPNO" CHAR(255)

TERMINATED BY "," OPTIONALLY ENCLOSED BY '"',

"ENAME" CHAR(255)

TERMINATED BY "," OPTIONALLY ENCLOSED BY '"',

"JOB" CHAR(255)

TERMINATED BY "," OPTIONALLY ENCLOSED BY '"',

"MGR" CHAR(255)

TERMINATED BY "," OPTIONALLY ENCLOSED BY '"',

"HIREDATE" CHAR(255)

TERMINATED BY "," OPTIONALLY ENCLOSED BY '"'

DATE_FORMAT DATE MASK 'yyyy-mm-dd',

"SAL" CHAR(255)

TERMINATED BY "," OPTIONALLY ENCLOSED BY '"',

"COMM" CHAR(255)

TERMINATED BY "," OPTIONALLY ENCLOSED BY '"',

"DEPTNO" CHAR(255)

TERMINATED BY "," OPTIONALLY ENCLOSED BY '"'

)

)

location

(

'emp.dat'

)

)REJECT LIMIT UNLIMITED

5)删除原alen表,创建外部表并验证

SQL> drop table alen;



Table dropped.



SQL> @alen.sql

51 ;



Table created.



SQL> select count(1) from alen;



COUNT(1)

----------

14

2.3 ORACLE_DATAPUMP访问驱动

创建外部表,除了使用ORACLE_LOADER驱动之外,还可以使用ORACLE_DATAPUMP,使用ORACLE_DATAPUMP可以实现数据的卸载和加载,通过create table as select 语句,ORACLE_DATAPUMP驱动可以将数据写到一个二进制的dump文件中,但该文件只能通过ORACLE_DATAPUMP访问驱动进行访问,而且,该文件一旦创建,不能被修改,可以被多次读取。

2.3.1 卸载数据

1)创建测试数据

SQL> conn alen/alen@odd

Connected.

SQL> create table dept as select * from scott.dept;



Table created.



SQL> select * from dept;



DEPTNO DNAME LOC

---------- -------------- -------------

10 ACCOUNTING NEW YORK

20 RESEARCH DALLAS

30 SALES CHICAGO

40 OPERATIONS BOSTON

2)CTAS方式创建外部表

SQL> ho cat /home/oracle/ext/dept.sql

create table dept_xt

organization external

(

type oracle_datapump

default directory ext_dir

location('dept_xt.dump')

)

as select * from dept;



SQL> @/home/oracle/ext/dept.sql



Table created.

3)查看表及dump文件

SQL> select * from dept_xt;



DEPTNO DNAME LOC

---------- -------------- -------------

10 ACCOUNTING NEW YORK

20 RESEARCH DALLAS

30 SALES CHICAGO

40 OPERATIONS BOSTON

SQL>ho ls /home/oracle/ext/dept_xt.dump

/home/oracle/ext/dept_xt.dump

4)修改外部表,出错,即不能进行修改数据

SQL> update dept_xt set loc='SZ' where deptno=10;

update dept_xt set loc='SZ' where deptno=10

*

ERROR at line 1:

ORA-30657: operation not supported on external organized table

2.3.2 加载数据

可以利用上面创建的dump文件创建新的外部表。

1)利用dump文件创建外部表,location位置可以指定多个dump文件

SQL> ho cat /home/oracle/ext/alen_xt.sql

create table alen_xt

(

deptno number(2),

dname varchar2(14),

loc varchar2(13)

)

organization external

(

type oracle_datapump

default directory ext_dir

location('dept_xt.dump')

)

;

SQL> @/home/oracle/ext/alen_xt.sql



Table created.

2)查看数据

SQL> select * from alen_xt;



DEPTNO DNAME LOC

---------- -------------- -------------

10 ACCOUNTING NEW YORK

20 RESEARCH DALLAS

30 SALES CHICAGO

40 OPERATIONS BOSTON

通过上面的介绍和演示,可以看出,外部表在使用上有很大的优势,尤其是在批量处理外部数据的时候。

 

posted @ 2020-05-13 23:39  追梦男生  阅读(695)  评论(0编辑  收藏  举报