csv - 无法在 pyspark 中显示 CSV 文件(ValueError : Some of types cannot be determined by the first 100 rows, 请重试采样)

标签 csv apache-spark pyspark

我在通过 Pyspark 显示 CSV 文件时遇到错误。我已附上我使用的 PySpark 代码和 CSV 文件。

from pyspark.sql import *
spark.conf.set("fs.azure.account.key.xxocxxxxxxx","xxxxx")

time_on_site_tablepath= "wasbs://<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9cf8ebecf3fffef0f3fedcf8ebfdf8faecf3ffb2fef0f3feb2fff3eef9b2ebf5f2f8f3ebefb2f2f9e8" rel="noreferrer noopener nofollow">[email protected]</a>/time_on_site.csv"
time_on_site = spark.read.format("csv").options(header='true', inferSchema='true').load(time_on_site_tablepath)

display(time_on_site.head(50))

错误如下所示

ValueError: Some of types cannot be determined by the first 100 rows, please try again with sampling

下面附有 CSV 文件格式

time_on_site:pyspark.sql.dataframe.DataFrame

next_eventdate:timestamp
barcode:integer
eventdate:timestamp
sno:integer
eventaction:string
next_action:string
next_deviceid:integer
next_device:string
type_flag:string
site:string
location:string
flag_perimeter:integer
deviceid:integer
device:string
tran_text:string
flag:integer
timespent_sec:integer
gg:integer

下面附有 CSV 文件数据

next_eventdate,barcode,eventdate,sno,eventaction,next_action,next_deviceid,next_device,type_flag,site,location,flag_perimeter,deviceid,device,tran_text,flag,timespent_sec,gg
2018-03-16 05:23:34.000,1998296,2018-03-14 18:50:29.000,1,IN,OUT,2,AGATE-R02-AP-Vehicle_Exit,,NULL,NULL,1,1,AGATE-R01-AP-Vehicle_Entry,Access Granted,0,124385,0
2018-03-17 07:22:16.000,1998296,2018-03-16 18:41:09.000,3,IN,OUT,2,AGATE-R02-AP-Vehicle_Exit,,NULL,NULL,1,1,AGATE-R01-AP-Vehicle_Entry,Access Granted,0,45667,0
2018-03-19 07:23:55.000,1998296,2018-03-17 18:36:17.000,6,IN,OUT,2,AGATE-R02-AP-Vehicle_Exit,,NULL,NULL,1,1,AGATE-R01-AP-Vehicle_Entry,Access Granted,1,132458,1
2018-03-21 07:25:04.000,1998296,2018-03-19 18:23:26.000,8,IN,OUT,2,AGATE-R02-AP-Vehicle_Exit,,NULL,NULL,1,1,AGATE-R01-AP-Vehicle_Entry,Access Granted,0,133298,0
2018-03-24 07:33:38.000,1998296,2018-03-23 18:39:04.000,10,IN,OUT,2,AGATE-R02-AP-Vehicle_Exit,,NULL,NULL,1,1,AGATE-R01-AP-Vehicle_Entry,Access Granted,0,46474,0

如何才能成功加载 CSV 文件?

最佳答案

你的语法没有问题,工作正常。 问题出在您的 CSV 文件数据中,其中名为 type_flag 的列只有 None(null) 值,因此它无法推断它的数据类型。

所以,这里有两个选择。

  1. 无需使用 head() 即可显示数据。喜欢 显示(time_on_site)

  2. 如果你想使用head()那么你需要替换null值,这里我用空字符串('')替换它。

    time_on_site = time_on_site.fillna('') 显示(time_on_site.head(50))

关于csv - 无法在 pyspark 中显示 CSV 文件(ValueError : Some of types cannot be determined by the first 100 rows, 请重试采样),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54814546/

相关文章:

Python 写函数不写最后一个条目

scala - 更改 Spark 中的日期格式会返回不正确的结果

apache-spark - spark结构化流异常: Append output mode not supported without watermark

postgresql - Spark Dataframe 到 Postgres 使用复制命令 -pyspark

python - 如何将 pyspark 日志记录级别设置为调试?

python - Apache PySpark 丢失执行程序 - 无法创建本地目录

php - 直接将CSV文件数据插入mysql,

python - 如何合并两个表并从 Python 中每个单元格中的先前值计数中划分数字实例?

python - pandas 合并数据框并枢轴创建新列

python - Spark : why is Decimal(36, 16) 6 位数字后四舍五入?