值此实验演示一种外部表的新用法。
1.为了实验方便,在根目录“/”下创建datapump目录,存放从数据库中卸载成数据泵格式的数据
[root@testdb /]# mkdir datapump
[root@testdb /]# chown -R oracle:oinstall datapump
[root@testdb /]# ls -ld datapump
drwxr-xr-x 2 oracle oinstall 4096 08-06 13:25 datapump
2.创建目录datapump
sec@ora10g> create or replace directory datapump as '/datapump';
Directory created.
3.待卸载表t_test的创建及样本数据
sec@ora10g> create table t_test (id int, name varchar2(10));
sec@ora10g> insert into t_test values (1,'Andy');
sec@ora10g> insert into t_test values (2,'Secooler');
sec@ora10g> insert into t_test values (3,'Hou');
sec@ora10g> commit;
sec@ora10g> select * from t_test;
ID NAME
---------- ----------
1 Andy
2 Secooler
3 Hou
4.卸载表t_test中的数据到操作系统文件/datapump/t_test.dat中
sec@ora10g> create table unload_t_test
2 organization external
3 ( type oracle_datapump
4 default directory datapump
5 location( 't_test.dat' )
6 )
7 as
8 select * from t_test;
OK,通过上面的方法,我们就把t_test表中的数据“转储到了”操作系统上。
5.查看一下卸载到的/datapump/t_test.dat文件中的内容
1)使用ls命令可以看到我们卸载到的t_test.dat文件和日志文件
ora10g@testdb /datapump$ ls -l
total 16
-rw-r--r-- 1 oracle oinstall 41 Aug 6 13:54 UNLOAD_T_TEST_10836.log
-rw-r----- 1 oracle oinstall 12288 Aug 6 13:54 t_test.dat
2)日志文件的内容非常的简单,如下:
ora10g@testdb /datapump$ cat UNLOAD_T_TEST_10836.log
LOG file opened at 08/06/09 13:54:06
3)重点看一下卸载得到的物理文件t_test.dat的内容:
ora10g@testdb /datapump$ strings t_test.dat
x86_64/Linux 2.4.xx
WE8ISO8859P1
LBB EMB GHC JWD SD EBE WMF DDG JG SJH SRH JGK CL EGM BJM RAP RLP RP KR PAR MS MRS JLS CET HLT
10.02.00.01.00
|
1
0
3
0
WE8ISO8859P1
AL16UTF16
+08:00
SEC
UNLOAD_T_TEST
1
0
ID
2
22
0
-127
0
0
0
2
0
NAME
1
10
0
0
31
1
10
Andy<
Secooler<
ora10g@testdb /datapump$
通过使用strings命令得到的全部内容展示在上面,可以看到,格式是XML的,这也是使用外部表卸载得到文件格式,XML格式的文件使用上非常的灵活。
6.此时的表unload_t_test引用的数据就是操作系统上t_test.dat文件的内容了
sec@ora10g> select * from unload_t_test;
ID NAME
---------- ----------
1 Andy
2 Secooler
3 Hou
(id int, name varchar2(10))
7.在我的这个实验环境中还有另外一个名为secooler的实例,我们在secooler实例中通过外部表的形式读取刚刚转储的t_test.dat文件,表现一下数据迁移的味道
1)指定实例名
ora10g@testdb /datapump$ export ORACLE_SID=secooler
2)进入到andy用户,目标是在andy用户中可以读到t_test.dat文件的内容
secooler@testdb /datapump$ sqlplus andy/andy
SQL*Plus: Release 10.2.0.1.0 - Production on Thu Aug 6 14:16:39 2009
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP and Data Mining Scoring Engine options
andy@secooler>
3)因为是全新的实例,所以需要重新创建目录,为了与上面的区别,这里我们将目录的名字命名为datapump_secooler,但目录还是要指定到/datapump
andy@secooler> create or replace directory datapump_secooler as '/datapump';
Directory created.
4)创建外部表t_secooler直接读取文件t_test.dat的内容
create table t_secooler (id int, name varchar2(10))
organization external
( type oracle_datapump
default directory datapump_secooler
location( 't_test.dat')
)
/
5)查看外部表t_secooler的内容
andy@secooler> select * from t_secooler;
ID NAME
---------- ----------
1 Andy
2 Secooler
3 Hou
6)很神奇吧,已经以只读的方式查看到了t_test.dat的内容
8.总结
这个特性是在10g Release 1版本中提供的。妙趣横生ing,提供给我们有一种迁移数据的途径。
secooler
09.08.06
-- The End --
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70034820/viewspace-2993876/
,如需转载,请注明出处,否则将追究法律责任。