【實驗】【外部表】以資料泵檔案格式抽取and遷移資料演示

secooler發表於2009-08-06
值此實驗演示一種外部表的新用法。

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部落格 ” ,連結:http://blog.itpub.net/519536/viewspace-611493/,如需轉載,請註明出處,否則將追究法律責任。

相關文章