【问题标题】:SQL*Loader cannnot load a CSV file that contains multiple lines in one fieldSQL*Loader 无法加载一个字段中包含多行的 CSV 文件
【发布时间】:2017-05-08 23:09:38
【问题描述】:

我有一个 CSV 文件,我正在尝试使用 SQL*Loader 将其加载到 Oracle 表中:

a, ab, abcdefg
b, bc, bcdefghij
c, cd, cdefghijk-lmnop
orstuv
wxyz
d, de, defghijk

在“c”行,第 3 列有多行,当我们使用 sqlldr 加载这一行时,它无法将该列加载到表中 - 它会将其放入错误文件中。

我试图为这样的某些列包含引号字符串

a, ab, "abcdefg"
b, bc, "bcdefghij"
c, cd, "cdefghijk-lmnop
orstuv
wxyz"
d, de, "defghijk"

我尝试了多种格式的控制文件,包括:

load data
CHARACTERSET TH8TISASCII
infile '/data/csv/test3.txt'
APPEND 
PRESERVE BLANKS
into table ITEM
fields terminated by ","
TRAILING NULLCOLS
( col1,
  col2,
  col3,
  col4)

和:

load data
CHARACTERSET TH8TISASCII
infile '/data/csv/test3.txt' "str '\n'"
APPEND 
PRESERVE BLANKS
into table ITEM
fields terminated by ","
TRAILING NULLCOLS
( col1,
  col2,
  col3,
  col4)

和:

load data
CHARACTERSET TH8TISASCII
infile '/data/csv/test3.txt'
APPEND 
PRESERVE BLANKS
into table ITEM
fields terminated by "," optionally enclosed by '"'   
TRAILING NULLCOLS
( col1,
  col2,
  col3,
  col4)

和:

load data
CHARACTERSET TH8TISASCII
infile '/data/csv/test3.txt'
APPEND 
PRESERVE BLANKS
into table ITEM
fields terminated by "," optionally enclosed by '\n' 
TRAILING NULLCOLS
( col1,
  col2,
  col3,
  col4)

但还是不行。

如何从该 CSV 格式加载数据?


根据 Gary_W 推荐 我尝试添加“str x'0D'”并且数据可以正确加载到列中 但它只加载 CSV 文件的第一行并成功返回日志,如下所示

load data
CHARACTERSET TH8TISASCII
infile '/data/csv/b.csv' "str x'0D'"
APPEND
PRESERVE BLANKS
into table ITEM2
fields terminated by "," optionally enclosed by '"' 
TRAILING NULLCOLS
( col1,
  col2,
  col3,
  col4
)


--Sample CSV file
c, cd, "cdefghijk-lmnop
orstuv
wxyz"
,c
a, ab, "abcdefg", a
b, bc, "bcdefghij", b
d, de, "defghijk",d


--sql loader command
sqlldr USER/PASSWORD readsize=2000000000 bindsize=2000000000 control=file_pointing2.ctl > /data/csv/sqlldr.log

###########################LOG########################
Table ITEM:
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.

there have only 1 record.
###########################OUT_PUT########################
col1| col2|         col3            | col4
cd  | cd  |efghijk-lmnoporstuvwxyz  | c

but if I remove the option "str x'0D'", in a case of the normal CSV format all row can load currently expect the abnormal row.

load data
CHARACTERSET TH8TISASCII
infile '/data/csv/b.csv' "str x'0D'"
APPEND
PRESERVE BLANKS
into table ITEM2
fields terminated by "," optionally enclosed by '"' 
TRAILING NULLCOLS
( col1,
  col2,
  col3,
  col4
)
--Sample CSV file
c, cd, "cdefghijk-lmnop
orstuv
wxyz"
,c
a, ab, "abcdefg", a
b, bc, "bcdefghij", b
d, de, "defghijk",d



--sql loader command
sqlldr USER/PASSWORD readsize=2000000000 bindsize=2000000000 control=file_pointing2.ctl > /data/csv/sqlldr.log


there have 4 records.but the record 1(col3) is not correct
###########################OUT_PUT########################
col1| col2|         col3            | col4
c   | cd  |efghijk-lmnop            | c
a   | ab  |abcdefg                  | a
b   | bc  |bcdefghij                | b
d   | de  |defghijk                 | d

【问题讨论】:

  • 您是否尝试过重新保存 CSV,以便每个字段也包含在引号中(查看程序中有关如何保存 CSV 的选项)?
  • 亲爱的 BLaZuRE,我尝试重新保存 CSV 文件并包含引号字符串列,但它仍然无法正常工作。谢谢和最好的问候,POPs
  • 如果您现在的文件中的每个字段都用双引号括起来,请编辑您的问题以显示这一点,以及您当前使用的控制文件。您是否尝试过使用the continueif directive
  • 谢谢你,Alex,我已经编辑了我的问题,但文件格式很难管理。
  • 好的,但是您的控制文件示例都没有continueif;您是否尝试过,如我链接到的旧答案所示? (You can read more about it in the documentation)

标签: oracle sql-loader


【解决方案1】:

在第三列周围加上双引号,您需要使用 INFILE 行上的流式子句告诉 sqlldr 回车是记录字符的结尾,因此双引号内的换行符将被忽略:

infile '/data/csv/test3.txt' "str x'0D'" 

由于我已经在这篇文章中解释了所有内容,请查看此处了解更多信息:https://stackoverflow.com/a/37216660/2543416

【讨论】:

  • 非常感谢 Gary_W。我尝试添加您推荐的此选项,并且该记录可以成功加载到表中,但之后它被停止,它没有将另一个重新编码加载到表中。
  • 更多关于“停止”的信息?日志文件中有任何信息吗?
  • 仅第一行csv文件下载成功,返回日志完整无错误。我不知道为什么忽略其他行。 -------------------------------------------------- ------------------------------------------------ LOG Table ITEM: 1 行已成功加载。 0 由于数据错误而未加载行。 0 行未加载,因为所有 WHEN 子句均失败。 0 行未加载,因为所有字段均为空。
  • 您还必须显示控制文件和命令行。请在原始帖子中进行,因为评论部分不允许格式化。我注意到您在控制文件中有以分号结尾的字段,但数据显示它们应该是逗号。请务必更正。
  • 谢谢 Gary_W 我已经更新了原帖的信息
猜你喜欢
  • 2017-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-24
  • 2021-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多