Pular para o conteúdo

Como utilizar o modo express do SQL*Loader para carga rápida e fácil de arquivos CSV

SQL*Loader Express

Um problema comum para os usuários do SQL*Loader é gerar o control file que a ferramenta utiliza para fazer a carga dos arquivos.

Quanto mais campos existem no arquivo de dados, maior é a chance de erros durante a criação do arquivo de controle.

Felizmente, a maioria dos arquivos a serem importados são do tipo CSV(comma-separated values). E pensando nisso, no Oracle Database 12c, foi introduzida uma nova feature chamada de express mode, que torna possível a carga de arquivos CSV de forma rápida e fácil.

No modo express, não é necessário criar o arquivo de controle.

O objetivo principal do SQL*Loader express mode é salvar tempo e diminuir o esforço.

Outro benefício do express mode, é que ele vai tentar utilizar o mecanismo mais rápido para o carregamento dos arquivos de dados: tabelas externas usando inserções paralelas com o hint append.

O express mode pode ser utilizado quando todas as colunas são do tipo character, number ou datetime. E também é necessário que as colunas estejam na mesma ordem em todos os registros.

SQL> create table EMPRESA (cod_emp NUMBER(1), nome_emp VARCHAR2(30));

Table created.

[oracle@oracle01 tmp]$ cat EMPRESA.dat 

1,Empresa 1
2,Empresa 2
3,Empresa 3
4,Empresa 4
5,Empresa 5
6,Empresa 6
7,Empresa 7
8,Empresa 8
9,Empresa 9

[oracle@oracle01 tmp]$ sqlldr teste/teste TABLE=EMPRESA

SQL*Loader: Release 12.1.0.1.0 - Production on Sat Jan 11 12:16:28 2014
Copyright (c) 1982, 2013, Oracle and/or its affiliates.  All rights reserved.

Express Mode Load, Table: EMPRESA

Path used:      External Table, DEGREE_OF_PARALLELISM=AUTO

Table EMPRESA:

 9 Rows successfully loaded.

Check the log files:

 EMPRESA.log
 EMPRESA_%p.log_xt

for more information about the load.

[oracle@oracle01 tmp]$ cat EMPRESA.log

SQL*Loader: Release 12.1.0.1.0 - Production on Sat Jan 11 12:16:28 2014
Copyright (c) 1982, 2013, Oracle and/or its affiliates.  All rights reserved.
Express Mode Load, Table: EMPRESA

Data File:      EMPRESA.dat
 Bad File:     EMPRESA_%p.bad
 Discard File:  none specified

(Allow all discards)

Number to load: ALL
Number to skip: 0
Errors allowed: 50
Continuation:    none specified
Path used:      External Table

Table EMPRESA, loaded from every logical record.

Insert option in effect for this table: APPEND

  Column Name                  Position   Len  Term Encl Datatype
------------------------------ ---------- ----- ---- ---- ---------------------
COD_EMP                             FIRST     *   ,       CHARACTER            
NOME_EMP                             NEXT     *   ,       CHARACTER            

Generated control file for possible reuse:

OPTIONS(EXTERNAL_TABLE=EXECUTE, TRIM=LRTRIM)

LOAD DATA
INFILE 'EMPRESA'

APPEND
INTO TABLE EMPRESA
FIELDS TERMINATED BY ","
(
 COD_EMP,
 NOME_EMP
)

End of generated control file for possible reuse.

created temporary directory object SYS_SQLLDR_XT_TMPDIR_00000 for path /tmp

enable parallel DML: ALTER SESSION ENABLE PARALLEL DML

creating external table "SYS_SQLLDR_X_EXT_EMPRESA"

CREATE TABLE "SYS_SQLLDR_X_EXT_EMPRESA" 
(
 "COD_EMP" NUMBER(1),
 "NOME_EMP" VARCHAR2(30)
)
ORGANIZATION external 
(
 TYPE oracle_loader
 DEFAULT DIRECTORY SYS_SQLLDR_XT_TMPDIR_00000
 ACCESS PARAMETERS 
 (
   RECORDS DELIMITED BY NEWLINE CHARACTERSET US7ASCII
   BADFILE 'SYS_SQLLDR_XT_TMPDIR_00000':'EMPRESA_%p.bad'
   LOGFILE 'EMPRESA_%p.log_xt'
   READSIZE 1048576
   FIELDS TERMINATED BY "," LRTRIM 
   REJECT ROWS WITH ALL NULL FIELDS 
   (
     "COD_EMP" CHAR(255),
     "NOME_EMP" CHAR(255)
   )
 )
 location 
 (
   'EMPRESA.dat'
 )
) REJECT LIMIT UNLIMITED

executing INSERT statement to load database table EMPRESA

INSERT /*+ append parallel(auto) */ INTO EMPRESA 
(
 COD_EMP,
 NOME_EMP
)

SELECT 
 "COD_EMP",
 "NOME_EMP"
FROM "SYS_SQLLDR_X_EXT_EMPRESA"

dropping external table "SYS_SQLLDR_X_EXT_EMPRESA"

Table EMPRESA:

 9 Rows successfully loaded.

Run began on Sat Jan 11 12:16:28 2014
Run ended on Sat Jan 11 12:16:30 2014
Elapsed time was:     00:00:01.93
CPU time was:         00:00:00.02

SQL> select * from EMPRESA;

  COD_EMP NOME_EMP
---------- ------------------------------
     1 Empresa 1
     2 Empresa 2
     3 Empresa 3
     4 Empresa 4
     5 Empresa 5
     6 Empresa 6
     7 Empresa 7
     8 Empresa 8
     9 Empresa 9

9 rows selected.

Algumas modificações de parâmetros são possíveis no express mode, como:

  • Changing the Field Terminator
  • Specifying the Order of the Fields in the Data File
  • Using a Different Data File
  • Using an Enclosure Character

Para maiores detalhes, é só consultar a referência abaixo.

Referências

Abraço

Quão útil foi este post ?

Clique em uma estrela para classificar o post

nota média 5 / 5. Contagem de votos: 29

Sem votos ! Seja o primeiro a classificar !

Marcações:

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

plugins premium WordPress