将 CSV 文件导入 Oracle External Table

Import a CSV file into an Oracle External Table

我见过各种类似的问题,但 none 的解决方案似乎对我有用。

我得到了一个在大型机上生成的 CSV 文件,我需要将其加载到 Oracle 中。我决定尝试将其映射到 Oracle 外部 table,然后使用它来将其插入 Oracle。

这是我的 CSV:

CONTRACT_NUMBER,PRODUCTCODE,TRANSACTION_NUMBER,EFFECTIVE_DATE,AMENDMENT,TERM,ACTIVE,AGENT_NUMBER,PREMIUM,ICRATE,RCRATE,IC_ALLOW,RC_ALLOW,SPRATE,TRANSACTION_CODE,TRANSACTION_DATE,AGENT_CATEGORY,AGENT_SALES_CODE,FREQ,TOT_PREMTD,REFERENCE,ALTERNATIVE_COMMISSION_METHOD,PAXUS_REF_ID
PAXUSCT1,MAA,1,07/10/2017,NB,12,Y,2905,6000,,,1,1,,T642,,,,,6000,,,
PAXUSCT1,MAA,2,07/05/2018,INC,11,Y,2905,2400,90,3,1,1,,,,,,,8400,,,
PAXUSCT2,MAA,1,01/06/2018,NB,12,Y,T1000,540,,,1,1,,,,,,,540,,,
PAXUSCT3,MAA,1,05/06/2018,NB,12,Y,T1000,1200,,,1,1,,,,,,,1200,,,

我创建了这个定义,以及它的许多其他变体,但我不断收到错误:

create table LD_CMS_BASIS_MIGRATION
(
  contract_number               VARCHAR2(8),
  productcode                   VARCHAR2(3),
  transaction_number            NUMBER,
  effective_date                DATE,
  amendment                     VARCHAR2(3),
  term                          NUMBER,
  active                        VARCHAR2(1),
  agent_number                  VARCHAR2(5),
  premium                       NUMBER,
  icrate                        NUMBER,
  rcrate                        NUMBER,
  ic_allow                      NUMBER,
  rc_allow                      NUMBER,
  sprate                        NUMBER,
  transaction_code              VARCHAR2(4),
  transaction_date              DATE,
  agent_category                VARCHAR2(4),
  agent_sales_code              VARCHAR2(4),
  freq                          VARCHAR2(1),
  tot_premtd                    NUMBER,
  reference                     VARCHAR2(40),
  alternative_commission_method VARCHAR2(40),
  paxus_ref_id                  VARCHAR2(8)
)
organization external
(
  type ORACLE_LOADER
  default directory MIGRATIONS
  access parameters 
  (
    records field names all files
    fields CSV without embedded record terminators
  )
  location (MIGRATIONS:'CMS_BASIS_MIG.csv')
)
reject limit UNLIMITED;

当我尝试从中读取时出现此错误:

这是服务器日志文件中的内容:

KUP-05004:   Warning: Intra source concurrency disabled because parallel select was not requested.

Field Definitions for table LD_CMS_BASIS_MIGRATION
  Record format DELIMITED BY NEWLINE
  Data in file has same endianness as the platform
  Rows with all null fields are accepted

  Fields in Data Source: 

    CONTRACT_NUMBER                 CHAR (255)
      Terminated by ","
      Enclosed by """ and """
      Trim whitespace same as SQL Loader
    PRODUCTCODE                     CHAR (255)
      Terminated by ","
      Enclosed by """ and """
      Trim whitespace same as SQL Loader
    TRANSACTION_NUMBER              CHAR (255)
      Terminated by ","
      Enclosed by """ and """
      Trim whitespace same as SQL Loader
    EFFECTIVE_DATE                  CHAR (255)
      Terminated by ","
      Enclosed by """ and """
      Trim whitespace same as SQL Loader
    AMENDMENT                       CHAR (255)
      Terminated by ","
      Enclosed by """ and """
      Trim whitespace same as SQL Loader
    TERM                            CHAR (255)
      Terminated by ","
      Enclosed by """ and """
      Trim whitespace same as SQL Loader
    ACTIVE                          CHAR (255)
      Terminated by ","
      Enclosed by """ and """
      Trim whitespace same as SQL Loader
    AGENT_NUMBER                    CHAR (255)
      Terminated by ","
      Enclosed by """ and """
      Trim whitespace same as SQL Loader
    PREMIUM                         CHAR (255)
      Terminated by ","
      Enclosed by """ and """
      Trim whitespace same as SQL Loader
    ICRATE                          CHAR (255)
      Terminated by ","
      Enclosed by """ and """
      Trim whitespace same as SQL Loader
    RCRATE                          CHAR (255)
      Terminated by ","
      Enclosed by """ and """
      Trim whitespace same as SQL Loader
    IC_ALLOW                        CHAR (255)
      Terminated by ","
      Enclosed by """ and """
      Trim whitespace same as SQL Loader
    RC_ALLOW                        CHAR (255)
      Terminated by ","
      Enclosed by """ and """
      Trim whitespace same as SQL Loader
    SPRATE                          CHAR (255)
      Terminated by ","
      Enclosed by """ and """
      Trim whitespace same as SQL Loader
    TRANSACTION_CODE                CHAR (255)
      Terminated by ","
      Enclosed by """ and """
      Trim whitespace same as SQL Loader
    TRANSACTION_DATE                CHAR (255)
      Terminated by ","
      Enclosed by """ and """
      Trim whitespace same as SQL Loader
    AGENT_CATEGORY                  CHAR (255)
      Terminated by ","
      Enclosed by """ and """
      Trim whitespace same as SQL Loader
    AGENT_SALES_CODE                CHAR (255)
      Terminated by ","
      Enclosed by """ and """
      Trim whitespace same as SQL Loader
    FREQ                            CHAR (255)
      Terminated by ","
      Enclosed by """ and """
      Trim whitespace same as SQL Loader
    TOT_PREMTD                      CHAR (255)
      Terminated by ","
      Enclosed by """ and """
      Trim whitespace same as SQL Loader
    REFERENCE                       CHAR (255)
      Terminated by ","
      Enclosed by """ and """
      Trim whitespace same as SQL Loader
    ALTERNATIVE_COMMISSION_METHOD   CHAR (255)
      Terminated by ","
      Enclosed by """ and """
      Trim whitespace same as SQL Loader
    PAXUS_REF_ID                    CHAR (255)
      Terminated by ","
      Enclosed by """ and """
      Trim whitespace same as SQL Loader
KUP-04117: Field name PAXUS_REF_ID
 was not found in the access parameter field list or table.
KUP-04093: error processing the FIELD NAMES record in data file /u02/CAMS/MIGRATIONS/dataload/CMS_BASIS_MIG.csv

非常感谢任何帮助。

谢谢, Mac

################## 编辑

下面来自 Tajesh 的回答,差不多。这是有效的。我认为 Newline 命令最有效。当我编辑 CSV 文件并在每行末尾添加一个逗号时,它会很好地选择最后一列。我也必须添加日期掩码。但是,Tajesh 解决方案意味着我不需要编辑 CSV 文件。

create table LD_CMS_BASIS_MIGRATION
(
  contract_number               VARCHAR2(8),
  productcode                   VARCHAR2(3),
  transaction_number            NUMBER,
  effective_date                DATE,
  amendment                     VARCHAR2(3),
  term                          NUMBER,
  active                        VARCHAR2(1),
  agent_number                  VARCHAR2(5),
  premium                       NUMBER,
  icrate                        NUMBER,
  rcrate                        NUMBER,
  ic_allow                      NUMBER,
  rc_allow                      NUMBER,
  sprate                        NUMBER,
  transaction_code              VARCHAR2(4),
  transaction_date              DATE,
  agent_category                VARCHAR2(4),
  agent_sales_code              VARCHAR2(4),
  freq                          VARCHAR2(1),
  tot_premtd                    NUMBER,
  reference                     VARCHAR2(40),
  alternative_commission_method VARCHAR2(40),
  paxus_ref_id                  VARCHAR2(8)
)
ORGANIZATION EXTERNAL ( TYPE ORACLE_LOADER
    DEFAULT DIRECTORY "MIGRATIONS" ACCESS PARAMETERS (
        RECORDS DELIMITED BY NEWLINE
            BADFILE 'CMS_BASIS_MIG_BAD.bad'
            LOGFILE 'CMS_BASIS_MIG_LOG.log'
            SKIP 1
            FIELDS TERMINATED BY ','
            DATE_FORMAT DATE MASK "dd/mm/yyyy"
            MISSING FIELD VALUES ARE NULL
    ) LOCATION ( 'CMS_BASIS_MIG.csv' )
) REJECT LIMIT UNLIMITED
    PARALLEL 5;

能否请您尝试使用以下创建 table 语法?

create table LD_CMS_BASIS_MIGRATION
(
  contract_number               VARCHAR2(8),
  productcode                   VARCHAR2(3),
  transaction_number            NUMBER,
  effective_date                DATE,
  amendment                     VARCHAR2(3),
  term                          NUMBER,
  active                        VARCHAR2(1),
  agent_number                  VARCHAR2(5),
  premium                       NUMBER,
  icrate                        NUMBER,
  rcrate                        NUMBER,
  ic_allow                      NUMBER,
  rc_allow                      NUMBER,
  sprate                        NUMBER,
  transaction_code              VARCHAR2(4),
  transaction_date              DATE,
  agent_category                VARCHAR2(4),
  agent_sales_code              VARCHAR2(4),
  freq                          VARCHAR2(1),
  tot_premtd                    NUMBER,
  reference                     VARCHAR2(40),
  alternative_commission_method VARCHAR2(40),
  paxus_ref_id                  VARCHAR2(8)
)
ORGANIZATION EXTERNAL ( TYPE ORACLE_LOADER
    DEFAULT DIRECTORY "MIGRATIONS" ACCESS PARAMETERS (
        RECORDS DELIMITED BY NEWLINE
            BADFILE 'CMS_BASIS_MIG_BAD.bad'
            LOGFILE 'CMS_BASIS_MIG_LOG.log'
            SKIP 1
        FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' MISSING FIELD VALUES ARE NULL
    ) LOCATION ( 'CMS_BASIS_MIG.csv' )
) REJECT LIMIT UNLIMITED
    PARALLEL 5;

如果上述代码抛出任何类型的错误 "date conversion",那么如果列的数据类型是日期,则必须指定每个列的名称及其格式。示例:conversion format

您的 csv 文件中缺少值是问题所在。您需要将它们转换为 null。只需在 ACCESS PARAMETERS

中添加 MISSING FIELD VALUES ARE NULL
 access parameters 
  (
    records field names all files
    fields CSV without embedded record Terminators
    MISSING FIELD VALUES ARE NULL
  )

如错误提示“KUP-04117:字段名称PAXUS_REF_ID 未在访问参数字段列表或 table 中找到。”,Oracle 无法找到列 PAXUS_REF_ID 的值,相反,它正在获取换行符,因为没有为此填充任何值记录最后一个逗号后的列,因此它抛出错误。

我已如下修改 CSV 文件的第二行和第三行,它正在按预期生成输出。

在第二条记录中添加值 0,在记录末尾为第三条记录添加白色 space。更改后,外部 table 可以读取这两条记录

CONTRACT_NUMBER,PRODUCTCODE,TRANSACTION_NUMBER,EFFECTIVE_DATE,AMENDMENT,TERM,ACTIVE,AGENT_NUMBER,PREMIUM,ICRATE,RCRATE,IC_ALLOW,RC_ALLOW,SPRATE,TRANSACTION_CODE,TRANSACTION_DATE,AGENT_CATEGORY,AGENT_SALES_CODE,FREQ,TOT_PREMTD,REFERENCE,ALTERNATIVE_COMMISSION_METHOD,PAXUS_REF_ID
PAXUSCT1,MAA,1,07/10/2017,NB,12,Y,2905,6000,,,1,1,,T642,,,,,6000,,,0
PAXUSCT1,MAA,2,07/05/2018,INC,11,Y,2905,2400,90,3,1,1,,,,,,,8400,,,
PAXUSCT2,MAA,1,01/06/2018,NB,12,Y,T1000,540,,,1,1,,,,,,,540,,,
PAXUSCT3,MAA,1,05/06/2018,NB,12,Y,T1000,1200,,,1,1,,,,,,,1200,,,

要按照@Hotfix 的建议解决此问题,您必须在访问参数中包含下面提到的语句

missing field values are null

此外,如果您在解释日期列数据时遇到任何问题,您可以将以下日期格式化程序添加到您的访问参数中。

date_format date mask "dd/mm/yyyy"

除此之外,您的数据集似乎与列 agent_number 以及记录 4 和 5 的值有关,其中一个数字列的值为 T1000。