Spark - 我是否正确读取了 csv?
Spark - Have I read from csv correctly?
我使用以下方法将 csv 文件读入 Spark:
df = spark.read.format(file_type).options(header='true', quote='\"',
ignoreLeadingWhiteSpace='true',inferSchema='true').load(file_location)
当我尝试使用来自另一个来源的示例 csv 数据并执行 diplsay(df) 时,它显示了一个整齐显示的 header 行,后跟数据。
当我在有 40 列和数百万行的主数据上尝试它时,它只显示前 20 列 headers 而没有数据行。
这是正常行为还是读错了?
更新:
我会将问题标记为已回答,因为以下提示很有用。但是我的结果是:
df.show(5, truncate=False)
目前显示:
+------------------------------------------------ ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ------------------------------+ |��"periodID","DAXDate","Country Name","Year","TransactionDate","QTR","Customer Number","Customer Name","Customer City","Document Type Code","Order Number" ,"Product Code","Product Description","Selling UOM","Sub Franchise Code","Sub Franchise Description","Product Major Code","Product Major Description","Product Minor Code","Product Minor Description","Invoice Number","Invoice DateTime","Class Of Trade ID","Class Of Trade","Region","AmountCurrencyType","Extended Cost","Gross Trade Sales","Net Trade Sales","Total(Ext Std Cost)","AdjustmentType","ExcludeComment","CurrencyCode","fxRate","Quantity","FileName" ,"RecordCount","Product Category","Direct","ProfitCenter","ProfitCenterRegion","ProfitCenterCountry"| +------------------------------------------------ ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ------------------------------
我将不得不回到基础知识,在文本编辑器中预览 csv,找出该文件的正确格式,从而找出问题所在。请注意,我必须将我的代码更新为以下内容以处理管道分隔符:
df = spark.read.format(file_type).options(header='true', quote='\"', delimiter='|',ignoreLeadingWhiteSpace='true',inferSchema='true').load(file_location)
是的,这是正常行为。 dataframe 函数 show() 的默认值是显示 20 行。您可以为此设置不同的值(但请记住,打印文件的所有行没有意义)并阻止它被截断。例如:
df.show(100, truncate=False)
Spark数据帧显示有限的行和列是正常的。您对数据的读取应该没有问题。但是,要确认您已正确阅读 csv,您可以尝试使用
查看 df 中的行数和列数
len(df.columns)
或
df.columns
行数
df.count()
如果您需要详细查看内容,可以使用选项 by cronoik。
这是正常现象。您可以通过不同方式查看数据内容:
show()
:以格式化的方式显示前 20 行。您可以指定要显示的行数作为参数(如果您提供的值比您的数据好得多!)。作为默认配置,列也将被截断。您可以指定 truncate=False
以显示所有列。 (就像@cronoik 在他的回答中正确地说的那样)。
head()
:与show()
相同,但它以"row"格式打印日期。不提供格式良好的 table,它对于快速完整地查看数据很有用,例如使用 head(1)
仅显示第一行。
describe().show()
:您可以显示摘要,让您深入了解数据。例如,显示元素的数量,每列的 min/max/avg 值。
我使用以下方法将 csv 文件读入 Spark:
df = spark.read.format(file_type).options(header='true', quote='\"', ignoreLeadingWhiteSpace='true',inferSchema='true').load(file_location)
当我尝试使用来自另一个来源的示例 csv 数据并执行 diplsay(df) 时,它显示了一个整齐显示的 header 行,后跟数据。
当我在有 40 列和数百万行的主数据上尝试它时,它只显示前 20 列 headers 而没有数据行。
这是正常行为还是读错了?
更新: 我会将问题标记为已回答,因为以下提示很有用。但是我的结果是:
df.show(5, truncate=False)
目前显示: +------------------------------------------------ ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ------------------------------+ |��"periodID","DAXDate","Country Name","Year","TransactionDate","QTR","Customer Number","Customer Name","Customer City","Document Type Code","Order Number" ,"Product Code","Product Description","Selling UOM","Sub Franchise Code","Sub Franchise Description","Product Major Code","Product Major Description","Product Minor Code","Product Minor Description","Invoice Number","Invoice DateTime","Class Of Trade ID","Class Of Trade","Region","AmountCurrencyType","Extended Cost","Gross Trade Sales","Net Trade Sales","Total(Ext Std Cost)","AdjustmentType","ExcludeComment","CurrencyCode","fxRate","Quantity","FileName" ,"RecordCount","Product Category","Direct","ProfitCenter","ProfitCenterRegion","ProfitCenterCountry"| +------------------------------------------------ ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ------------------------------
我将不得不回到基础知识,在文本编辑器中预览 csv,找出该文件的正确格式,从而找出问题所在。请注意,我必须将我的代码更新为以下内容以处理管道分隔符:
df = spark.read.format(file_type).options(header='true', quote='\"', delimiter='|',ignoreLeadingWhiteSpace='true',inferSchema='true').load(file_location)
是的,这是正常行为。 dataframe 函数 show() 的默认值是显示 20 行。您可以为此设置不同的值(但请记住,打印文件的所有行没有意义)并阻止它被截断。例如:
df.show(100, truncate=False)
Spark数据帧显示有限的行和列是正常的。您对数据的读取应该没有问题。但是,要确认您已正确阅读 csv,您可以尝试使用
查看 df 中的行数和列数len(df.columns)
或
df.columns
行数
df.count()
如果您需要详细查看内容,可以使用选项
这是正常现象。您可以通过不同方式查看数据内容:
show()
:以格式化的方式显示前 20 行。您可以指定要显示的行数作为参数(如果您提供的值比您的数据好得多!)。作为默认配置,列也将被截断。您可以指定 truncate=False
以显示所有列。 (就像@cronoik 在他的回答中正确地说的那样)。
head()
:与show()
相同,但它以"row"格式打印日期。不提供格式良好的 table,它对于快速完整地查看数据很有用,例如使用 head(1)
仅显示第一行。
describe().show()
:您可以显示摘要,让您深入了解数据。例如,显示元素的数量,每列的 min/max/avg 值。