2

从 CSV 文件创建 DataFrame 时,如果multiLine启用该选项,某些文件列会被错误地解析。

这里是代码执行。随着代码的进行,我将尝试显示奇怪的行为。

首先,我在两个变量中df_ok加载文件:加载不带multiLine选项的文件,同时df_ko加载multiLine启用选项的文件。我作为示例使用的文件\r\n为 EOL,它以 UTF-8 编码,并以管道作为列分隔符。

val df_ok = spark.read.format("csv").option("header", "true").option("delimiter", "|").load("/.../20180423_LSV.csv")
val df_ko = spark.read.format("csv").option("header", "true").option("delimiter", "|").option("multiLine", "true").load("/.../20180423_LSV.csv")

df_ok.printSchema
root
 |-- FILIALE: string (nullable = true)
 |-- IDMDB: string (nullable = true)
 |-- FIMAGIC: string (nullable = true)
 ...
 |-- STATUS: string (nullable = true)
 |-- LSV_TYPE: string (nullable = true)

df_ko.printSchema
root
 |-- FILIALE: string (nullable = true)
 |-- IDMDB: string (nullable = true)
 |-- FIMAGIC: string (nullable = true)
 ...
 |-- STATUS: string (nullable = true)
 : string (nullable = true)

 df_ko.columns
 res0: Array[String] = Array(FILIALE, IDMDB, FIMAGIC, FIVEHMAGIC, SITE, VEHICLE, DESC001, DESC002, LOCN, PROGRESS, CHASSIS, REGN, CUSDATE, INVDATE, STYPE, TARMAGIC, VEHMAGIC, COST, REGDATE, DELDATE, EXEC001, INVOICE, INVTOT, STATUS, "LSV")YPE

我注意到的第一件事是,使用 时multiLine,该列LSV_TYPE消失了。相反,它显示: string (nullable = true). 使用df_ko.columns它时会显示一些非常奇怪的东西:..., "LSV")YPE. 当我仅用\n作 EOL 时,不会发生这种情况。设置quote选项不会改变任何东西。

我测试的下一件事是列的选择FILIALE

df_ok.select($"FILIALE")
res1: org.apache.spark.sql.DataFrame = [FILIALE: string]

df_ok.columns.contains("FILIALE")
res2: Boolean = true

df_ko.select($"FILIALE")
org.apache.spark.sql.AnalysisException: cannot resolve '`FILIALE`' given input columns: [INVOICE, DESC001, COST, STYPE, PROGRESS, INVTOT, VEHICLE, REGDATE, TARMAGIC, STATUS, CUSDATE, LOCN, INVDATE, SITE, DELDATE, REGN, EXEC001, VEHMAGIC,, DESC002, FIVEHMAGIC, CHASSIS, FIMAGIC, FILIALE];;

所以我认为除了他的专栏名称之外还有其他内容:

df_ko.columns.head.toArray
res68: Array[Char] = Array(, F, I, L, I, A, L, E)

df_ko.columns.head.toArray.foreach(c => println(c.toInt))
65279
70
73
76
73
65
76
69

比我检查文件的方式不同:

spark.read.text("/.../20180423_LSV.csv").collect.head.getString(0).toArray.foreach(c => println(c.toInt))
70
73
76
73
65
76

然后我确定问题来自 CSV 解析。添加charset选项或使用单义性作为parseLib选项的值不会改变任何事情。

我正在使用的文件:

FILIALE|IDMDB|FIMAGIC|FIVEHMAGIC|SITE|VEHICLE|DESC001|DESC002|LOCN|PROGRESS|CHASSIS|REGN|CUSDATE|INVDATE|STYPE|TARMAGIC|VEHMAGIC|COST|REGDATE|DELDATE|EXEC001|INVOICE|INVTOT|STATUS|LSV_TYPE
XXXXXXXX|XX696209|XX696209|XX0|73|100284|XXXXXXXXXXXXX45XXXXXXXX|X|73X|X|XXX4503321X2361427|73XX100284||24X10X2005|X|696209|0|9592X7||22X10X2005|XXXX73|500228|10841X24|X|XX
XXXXXXXX|XX1454353|XX1454353|XX959136|73|100212|XXXXXXXXXXXXX45XXXXXXXXXXXXXXXXXX|XXXXXXXXXXXXX45XXXXXXXXXXXXXXXXXX|73X|X|XXX4503321X2096859|73XX100212||08X09X2005|X|1454353|959136|0|||XXXX73|500205|0|X|XX
XXXXXXXX|XX607020|XX607020|XX0|73|100097|XXXXXXXXXXXX50XXXXXXXXXXXXXX|X|73X|X|XXX4540001X0540628|8232XX33||17X02X2005|X|607020|0|10868X34|||XXXX73|500025|11750|X|XX
XXXXXXXX|XX1796002|XX1796002|XX0|73|100091|XXXXXXXXXXXX70XXXXXXXXXXX|X|73X|X|XXX4540011X072541X|73XX100091||21X01X2005|X|1796002|0|12457X44||19X01X2005|XXXX73|500010|13616X9|X|XX
XXXXXXXX|XX728637|XX728637|XX0|73|100046|XXXXXXXXXXXXX55XXXXXXXXXX|X|73X|X|XXX4503331X1326935|4059XX33||25X11X2005|X|728637|0|14425X76|22X02X2005|24X11X2005|XXXX73|500244|17500|X|XX
XXXXXXXX|XX555718|XX555718|XX0|73|100020|XXXXXXXXXXXXXXX45XXX|X|73X|X|XXX4524321X0392633|73XX100020||01X08X2005|X|555718|0|12897||29X07X2005|XXXX73|500173|17446X39|X|XX
XXXXXXXX|XX589182|XX589182|XX0|73|100270|XXXXXXXX1X1XXXXXXXX|X|73X|X|XXX4540301X0461656|73XX100270||19X09X2005|X|589182|0|13112X6||16X09X2005|XXXX73|500201|14998|X|XX
XXXXXXXX|XX1796399|XX1796399|XX0|73|100362|XXXXXXXXXXXXX45XXXXXXXXXXXXXX|XXXXXXXXXXXXX45XXXXXXXXXXXXXX|73X|X|XXX4503321X2775489|73XX100362||22X05X2006|X|1796399|0|10783X11||17X05X2006|XXXX73|500087|11976X24|X|XX
XXXXXXXX|XX1796399|XX1796399|XX0|73|100337|XXXXXXXXXXXXX45XXXXXXXXXXXXXX|XXXXXXXXXXXXX45XXXXXXXXXXXXXX|73X|X|XXX4503321X2654339|73XX100337||22X05X2006|X|1796399|0|10672X11||17X05X2006|XXXX73|500086|11976X24|X|XX
XXXXXXXX|XX340211|XX340211|XX0|73|100383|XXXXXXXX50XXXXXXXXXXX|X|73X|X|XXX4540001X0839774|2724XX33||05X06X2006|X|340211|0|13321|23X05X2006|02X06X2006|XXXX73|500099|10999X99|X|XX

我使用 Spark 2.2.0 在 HDP 2.6.4 上执行了我的代码。

有没有人有解决方法或知道发生了什么?

4

1 回答 1

0

考虑添加.option("ignoreTrailingWhiteSpace", true).

我遇到了同样的问题,它为我解决了。

于 2019-11-10T17:49:12.670 回答