Unload data from oracle
Unload data from oracle
前面說了oracle載入資料的工具:sqlload,利用direct和parallel及unrecoverable等引數,我們可以很快速的匯入資料到db中,對於unload資料,oracle提供的工具就是spool命令,結合一些set命令,這個工具可以實現一些基本的unload功能,但實用性和速度均不是非常理想,知名網友d.c.b.a提供 的sqluldr,c語言開發,利用OCI介面和oracle互動,可以支援常用的欄位分隔符,實際測試下來,速度比spool快了30%以上,很好很強大.
利用spool unload 資料
Spool.txt 內容
set feedback off pagesize 0 heading off echo off termout off
spool c:\spoolfile.txt
Select /*+ parallel(a 16) */ sessionid||chr(9)||marketingsource||chr(9)||mktkeyword||chr(9)||validkey From incoming_fact Partition(incoming_par_20090427) a Inner Join trafficsource_dimension b
On a.trafficsourcekey=b.trafficsourcekey
And a.websitekey=12
--And b.traffictype='SEM'
-- And substr(lpad(localtimekey,6,0),1,2)=03
And to_char(starttime,'hh24')<=04
And to_char(starttime,'hh24')>=02
And validkey>=0;
spool off
16:47:56 SQL> @c:\spool.txt
16:52:04 SQL>
利用sqluldr unload同樣的資料
C:\>ociuldr user=trketl/TrackING_1@dw sql=c:\sqlfile.txt field=0x09 file=c:\wokao.csv
0 rows exported at 2009-05-08 16:54:40
15468 rows exported at 2009-05-08 16:58:09
output file c:\wokao.csv closed at 15468 rows.
速度比spool快了30%以上(db放在us,通過遠端網路,從us unload資料到sh)
關於sqluldr的資訊: http://www.anysql.net/ociuldr.html,其中可以下載這個工具,也可以下載原始碼.
此外,也可以利用utl_file包,開發upload程式,不過這需要directory支援,這就導致unload的檔案也是儲存在db server上,這就有點不安全和不方便了.給出一段google來的程式碼,需要的話可以按自己的需求去修改。
create or replace function unload_csv( p_query in varchar2,
p_separator in varchar2
default ',',
p_dir in varchar2 ,
p_filename in varchar2 )
return number
AUTHID CURRENT_USER
is
l_output utl_file.file_type;
l_theCursor integer default dbms_sql.open_cursor;
l_columnValue varchar2(2000);
l_status integer;
l_colCnt number default 0;
l_separator varchar2(10) default '';
l_cnt number default 0;
begin
l_output := utl_file.fopen( p_dir, p_filename, 'w' );
dbms_sql.parse( l_theCursor, p_query, dbms_sql.native );
for i in 1 .. 255 loop
begin
dbms_sql.define_column( l_theCursor, i,
l_columnValue, 2000 );
l_colCnt := i;
exception
when others then
if ( sqlcode = -1007 ) then exit;
else
raise;
end if;
end;
end loop;
dbms_sql.define_column( l_theCursor, 1, l_columnValue,
2000 );
l_status := dbms_sql.execute(l_theCursor);
loop
exit when ( dbms_sql.fetch_rows(l_theCursor) <= 0 );
l_separator := '';
for i in 1 .. l_colCnt loop
dbms_sql.column_value( l_theCursor, i,
l_columnValue );
utl_file.put( l_output, l_separator ||
l_columnValue );
l_separator := p_separator;
end loop;
utl_file.new_line( l_output );
l_cnt := l_cnt+1;
end loop;
dbms_sql.close_cursor(l_theCursor);
utl_file.fclose( l_output );
return l_cnt;
end unload_csv;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10159839/viewspace-594559/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Unload data to a flat file from Oracle databaseOracleDatabase
- How to migrate data from Oracle to MSSQLSERVEROracleSQLServer
- How to get ORACLE_HOME from data dictionaryOracle
- Export data from a data blockExportBloC
- External Tables: Querying Data From Flat Files in OracleOracle
- Oracle的SQL*Net more data from client 等待事件分析OracleSQLclient事件
- Oracle 11g Data Guard 使用duplicate from active databaseOracleDatabase
- Get data style from ExcelExcel
- get_data_from_yml
- SQL*Net more data from clientSQLclient
- Oracle 業務資料unload恢復過程Oracle
- OAF export data from VO in xlsx formatExportORM
- Oracle 11g Data Guard 使用 duplicate from active database 建立 standby databaseOracleDatabase
- Oracle 11g Data Guard 使用duplicate from active database 建立 standby databaseOracleDatabase
- How To Replicate Data from Oracle to Postgres Using GoldenGate (Doc ID 1544137.1OracleGo
- response from oracleOracle
- 【等待事件】SQL*Net more data from dblink事件SQL
- SQL*Net more data from dblink Reference NoteSQL
- LeetCode-Find Median from Data StreamLeetCode
- Got fatal error 1236 from master when reading data from binary logGoErrorAST
- 【轉載】使用 BAPI_BUPA_CREATE_FROM_DATA 建立BPAPI
- 搭建11g data guard(duplicate from active database方式)Database
- Doing Data Science: Straight Talk from the FrontlineAI
- Import Data From MS Excel to DataSet without using COM ObjectsImportExcelObject
- Extracting DDLs from OracleOracle
- Oracle ERP From ItpubOracle
- Oracle Data BufferOracle
- oracle data guard!!Oracle
- oracle data pumpOracle
- scrapy-redis記錄之,重寫make_request_from_data和make_requests_from_urlRedis
- SQL*Net more data from dblink引起library cache pinSQL
- Oracle 建立PDB-from ScratchOracle
- oracle中copy from的用法Oracle
- Oracle Flashback Data ArchiveOracleHive
- Oracle Data Guard配置Oracle
- MySQL主從複製報錯:Got fatal error 1236 from master when reading data fromMySqlGoErrorAST
- Result Sets from Stored Procedures In Oracle (轉)Oracle
- 介紹ORACLE DATA GUARD——DATA GUARD概念和管理Oracle