最近因爲遇到oracle控制文件的使用,雖然不是很複雜,可是歷來沒有用過,專門花點時間看看。點擊 這裏 查看詳細
sql
1,概述:數據庫
Sql Loader: 一個批量工具,將文件數據導入到數據庫。能夠導入一個表或者多個表,甚至能夠在導入時修改數據。session
2,使用oracle
a,你電腦須要裝Oracle,否則你是找不到Sqlldr 這個命令的。app
在控制輸入臺輸入 sqlldr:ide
會列出相關的參數介紹。工具
> sqlldr . . . Usage: SQLLDR keyword=value [,keyword=value,...] Valid Keywords: userid -- ORACLE username/password control -- control file name log -- log file name bad -- bad file name data -- data file name discard -- discard file name discardmax -- number of discards to allow (Default all) skip -- number of logical records to skip (Default 0) load -- number of logical records to load (Default all) errors -- number of errors to allow (Default 50) rows -- number of rows in conventional path bind array or between direct path data saves (Default: Conventional path 64, Direct path all) bindsize -- size of conventional path bind array in bytes (Default 256000) silent -- suppress messages during run (header,feedback,errors,discards, partitions) direct -- use direct path (Default FALSE) parfile -- parameter file: name of file that contains parameter specifications parallel -- do parallel load (Default FALSE) file -- file to allocate extents from skip_unusable_indexes -- disallow/allow unusable indexes or index partitions (Default FALSE) skip_index_maintenance -- do not maintain indexes, mark affected indexes as unusable (Default FALSE) commit_discontinued -- commit loaded rows when load is discontinued (Default FALSE) readsize -- size of read buffer (Default 1048576) external_table -- use external table for load; NOT_USED, GENERATE_ONLY, EXECUTE (Default NOT_USED) columnarrayrows -- number of rows for direct path column array (Default 5000) streamsize -- size of direct path stream buffer in bytes (Default 256000) multithreading -- use multithreading in direct path resumable -- enable or disable resumable for current session (Default FALSE) resumable_name -- text string to help identify resumable statement resumable_timeout -- wait time (in seconds) for RESUMABLE (Default 7200) date_cache -- size (in entries) of date conversion cache (Default 1000) PLEASE NOTE: Command-line parameters may be specified either by position or by keywords. An example of the former case is 'sqlldr scott/tiger foo'; an example of the latter is 'sqlldr control=foo userid=scott/tiger'.One may specify parameters by position before but not after parameters specified by keywords.For example, 'sqlldr scott/tiger control=foo logfile=log' is allowed, but 'sqlldr scott/tiger control=foo log' is not, even though the position of the parameter 'log' is correct.
b, sqlldr 將文本文件的導入到數據庫學習
這裏看個簡單例子。看看sqlldr到底怎麼工做的。this
1,準備數據文件,例如input.txt.這個文件將導入到數據庫中。spa
首先查看咱們數據庫的表格式。
create table student( SNAME VARCHAR(20), SAGE INTEGER, SEMAIL VARCHAR(20), SPHONE VARCHAR(20), SADDRESS VARCHAR(20) )
input.txt 文件
12,12,abc@gmail.com,12,address 13,13,abc@gmail.com,13,address 14,14,abc@gmail.com,14,address 15,15,abc@gmail.com,15,address 16,16,abc@gmail.com,16,address 17,17,abc@gmail.com,17,address 18,18,abc@gmail.com,18,address 19,19,abc@gmail.com,19,address
2,控制文件input.ctl
load data infile 'input.txt' append into table student --這裏用的Append. fields terminated by "," --這裏表示逗號分割。 (SNAME,SAGE,SEMAIL,SPHONE,SADDRESS)
這裏用的Append, 追加數據,還有幾個其餘的參數:
a,insert,爲缺省方式,在數據裝載開始時要求表爲空
b,append,在表中追加新記錄
c ,replace,刪除舊記錄,替換成新裝載的記錄
d,truncate,同上
3,sqlldr 調用控制文件
sqlldr username/password@Database control =input.ctl //input.ctl 爲控制文件
在這裏須要提下,這裏是會生成日誌文件,默認爲文件名文件名+.log. 當前爲 input.log
若是執行失敗了,會生成bad file. 若是在當前執行中錯誤,會生成input.bad file。
下面指定Log 和bad 文件,固然能夠加上路徑
sqlldr userid=username/password@database control=input.ctl log=input.log bad=input.bad SILENT=(HEADER, FEEDBACK)
SILENT=(HEADER, FEEDBACK) 控制端不顯示信息,例以下面的信息將再也不控制端顯示。只在日誌文件中
Record 4: Rejected - Error on table EMP ORA-00001: unique constraint <name> violated
固然是能夠顯示指定的。
load data infile input.txt badfile t.bad discardfile t.dsc append into table student fields terminated by "," (SNAME,SAGE,SEMAIL,SPHONE,SADDRESS)
看看日誌文件:input.log
SQL*Loader: Release 10.2.0.1.0 - Production on Tue May 20 17:36:52 2014 Copyright (c) 1982, 2005, Oracle. All rights reserved. Control File: input1.ctl Data File: input1.ctl Bad File: input1.bad Discard File: none specified (Allow all discards) Number to load: ALL Number to skip: 0 Errors allowed: 50 Bind array: 64 rows, maximum of 256000 bytes Continuation: none specified Path used: Conventional Table STUDENT, loaded from every logical record. Insert option in effect for this table: APPEND Column Name Position Len Term Encl Datatype ------------------------------ ---------- ----- ---- ---- --------------------- SNAME FIRST * , CHARACTER SAGE NEXT * , CHARACTER SEMAIL NEXT * , CHARACTER SPHONE NEXT * , CHARACTER SADDRESS NEXT * , CHARACTER Table STUDENT: 1 Row successfully loaded. 0 Rows not loaded due to data errors. 0 Rows not loaded because all WHEN clauses were failed. 0 Rows not loaded because all fields were null. Space allocated for bind array: 82560 bytes(64 rows) Read buffer bytes: 1048576 Total logical records skipped: 0 Total logical records read: 1 Total logical records rejected: 0 Total logical records discarded: 0 Run began on Tue May 20 17:36:52 2014 Run ended on Tue May 20 17:36:52 2014 Elapsed time was: 00:00:00.05 CPU time was: 00:00:00.04
4,查看數據庫
到此一個簡單的例子完成,從一個文本文件導入到數據庫。
文件能夠爲不一樣格式文件,.dat,.csv均可以的。
C,sqlldr直接在控制文件中導入數據。
load data infile * append into table student fields terminated by "," (SNAME,SAGE,SEMAIL,SPHONE,SADDRESS) begindata 20,20,abc@gmail.com,20,address --這裏是數據
D,當文件數據是以絕對位置分開的,咱們能夠直接截取。固然,截取的開始與結束必須當心了。
load data infile t.dat append into table student (SNAME position(01:20), SAGE position(21:23) , SEMAIL position(41:60), SPHONE position(61:80), SADDRESS position(81:100) )
t.dat 文件
Jack 12 abc@gmail.com 134998879 Singapore Jack2 12 abc@gmail.com 134998879 Singapore Jack3 12 abc@gmail.com 134998879 Singapore Jack4 12 abc@gmail.com 134998879 Singapore Jack5 12 abc@gmail.com 134998879 Singapore Jack6 12 abc@gmail.com 134998879 Singapore Jack7 12 abc@gmail.com 134998879 Singapore
還數據在Load to database 的時候,load的數據是能夠改變的。
LOAD DATA INFILE * INTO TABLE modified_data ( rec_no "my_db_sequence.nextval", region CONSTANT '31', time_loaded "to_char(SYSDATE, 'HH24:MI')", data1 POSITION(1:5) ":data1/100", data2 POSITION(6:15) "upper(:data2)", data3 POSITION(16:22)"to_date(:data3, 'YYMMDD')" ) BEGINDATA 11111AAAAAAAAAA991201 22222BBBBBBBBBB990112
能夠將多個文件導入到同一個表或者多個表中
load data infile t1.dat infile t2.dat infile t3.dat append into table student (SNAME position(01:20), SAGE position(21:23) , SEMAIL position(41:60), SPHONE position(61:80), SADDRESS position(81:100) )
這裏有不少命令的解釋
簡單實現幾個例子,稍後有時間添加多點理論知識,再邊學習邊完善了。