Oracle数据加载和卸载的实现方法

在日常工作中;经常会遇到这样的需求:

  • oracle 数据表跟文本或者文件格式进行交互;即将指定文件内容导入对应的 oracle 数据表中;或者从 oracle 数据表导出。
  • 其他数据库中的表跟oracle数据库进行交互。

  若是少量数据;可选择的解决方案有很多。常用的用 pl/sql developer工具,或者手动转换为 insert 语句,或者通过api。但数据量大;用上面的方法效率太烂了。本文来说说 oracle 数据的加载和卸载。

  • oracle中的dblink
  • oracle加载数据-外部表
  • oracle加载数据-sqlldr工具
  • oracle卸载数据-sqludr

一. oracle 中的 dblink

  在日常工作中;会遇到不同的数据库进行数据对接;每个数据库都有着功能;像oracle有 dblink ; postgresql有外部表。

1.1 oracle dblink 语法

create [public] database link link
connect to username
identified by password
using ‘connectstring’

1.2 oracle to mysql

  在oracle配置mysql数据库的dblink

二.oracle加载数据-外部表

  oracle外部表用来存取数据库以外的文本文件(text file)或oracle专属格式文件。因此,建立外部表时不会产生段、区、数据块等存储结构,只有与表相关的定义放在数据字典中。外部表,顾名思义,存储在数据库外面的表。当存取时才能从oracle专属格式文件中取得数据,外部表仅供查询,不能对外部表的内容进行修改(insert、update、delete操作)。不能对外部表建立索引。

2.1 创建外部表需要的目录

# 创建外部表需要的目录
sql> create or replace directory dump_dir as '/data/ora_ext_lottu'; 
directory created.
# 给用户授予指定目录的操作权限
sql> grant read,write on directory dump_dir to lottu;

grant succeeded.

2.2 外部表源文件lottu.txt

10,accounting,new york
20,research,dallas
30,sales,chicago
40,operations,boston

2.3 创建外部表

drop table dept_external purge;

create table dept_external (
  deptno   number(6),
  dname   varchar2(20),
  loc    varchar2(25) 
)
organization external
(type oracle_loader
 default directory dump_dir
 access parameters
 (
 records delimited by newline
 badfile 'lottu.bad'
 logfile 'lottu.log'
 fields terminated by "," optionally enclosed by '"'
 (
  deptno   integer external(6),
  dname   char(20),
  loc    char(25)
 )
 )
 location ('lottu.txt')
)
reject limit unlimited;

查看数据

sql> select * from dept_external;

  deptno dname loc
---------- -------------------- -------------------------
 10 accounting new york
 20 research dallas
 30 sales chicago
 40 operations boston

三. oracle加载数据-sqlldr工具

3.1 准备实验对象

  创建文件lottu.txt;和表tbl_load_01。

[oracle@oracle235 ~]$ seq 1000|awk -vofs="," '{print $1,"lottu",systime()-$1}' > lottu.txt
[oracle@oracle235 ~]$ sqlplus lottu/li0924
sql*plus: release 11.2.0.4.0 production on mon aug 13 22:58:34 2018
copyright (c) 1982, 2013, oracle. all rights reserved.

connected to:
oracle database 11g enterprise edition release 11.2.0.4.0 - 64bit production
with the partitioning, olap, data mining and real application testing options
sql> create table tbl_load_01 (id number,name varchar2(10),accountid number);
table created.

3.2 创建控制文件lottu.ctl

load data
characterset utf8
    infile '/home/oracle/lottu.txt'
    truncate into table tbl_load_01
    fields terminated by ','
    trailing nullcols
 optionally enclosed by ' ' trailing nullcols
(
 id ,
 name,
 accountid
)

3.3 执行sqlldr

[oracle@oracle235 ~]$ sqlldr 'lottu/"li0924"' control=/home/oracle/lottu.ctl log=/home/oracle/lottu.log bad=/home/oracle/lottu.bad
sql*loader: release 11.2.0.4.0 - production on mon aug 13 23:10:12 2018
copyright (c) 1982, 2011, oracle and/or its affiliates. all rights reserved.
commit point reached - logical record count 64
commit point reached - logical record count 128
commit point reached - logical record count 192
commit point reached - logical record count 256
commit point reached - logical record count 320
commit point reached - logical record count 384
commit point reached - logical record count 448
commit point reached - logical record count 512
commit point reached - logical record count 576
commit point reached - logical record count 640
commit point reached - logical record count 704
commit point reached - logical record count 768
commit point reached - logical record count 832
commit point reached - logical record count 896
commit point reached - logical record count 960
commit point reached - logical record count 1000

四.oracle卸载数据-sqludr

  sqludr是将oracle数据表导出到文本中;是牛人楼方鑫开发的。并非oracle自带工具;需要下载安装才能使用。

4.1 sqludr安装

[oracle@oracle235 ~]$ unzip sqluldr2linux64.zip 
archive: sqluldr2linux64.zip
 inflating: sqluldr2linux64.bin   
[oracle@oracle235 ~]$ mv sqluldr2linux64.bin $oracle_home/bin/sqludr

4.2 查看sqludr帮助

[oracle@oracle235 ~]$ sqludr -?
sql*unloader: fast oracle text unloader (gzip, parallel), release 4.0.1
(@) copyright lou fangxin (anysql.net) 2004 - 2010, all rights reserved.
license: free for non-commercial useage, else 100 usd per server.
usage: sqluldr2 keyword=value [,keyword=value,...]
valid keywords:
  user  = username/password@tnsname
  sql   = sql file name
  query  = select statement
  field  = separator string between fields
  record = separator string between records
  rows  = print progress for every given rows (default, 1000000) 
  file  = output file name(default: uldrdata.txt)
  log   = log file name, prefix with + to append mode
  fast  = auto tuning the session level parameters(yes)
  text  = output type (mysql, csv, mysqlins, oracleins, form, search).
  charset = character set name of the target database.
  ncharset= national character set name of the target database.
  parfile = read command option from parameter file 
 for field and record, you can use '0x' to specify hex character code,
 \r=0x0d \n=0x0a |=0x7c ,=0x2c, \t=0x09, :=0x3a, #=0x23, "=0x22 '=0x27

4.3 执行sqludr

[oracle@oracle235 ~]$ sqludr lottu/li0924 query="tbl_load_01" file=lottu01.txt field=","
      0 rows exported at 2018-08-13 23:47:55, size 0 mb.
    1000 rows exported at 2018-08-13 23:47:55, size 0 mb.
     output file lottu01.txt closed at 1000 rows, size 0 mb.

总结

以上所述是www.887551.com给大家介绍的oracle数据加载和卸载的实现方法,希望对大家有所帮助

(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐