Spark / Databricks 代码无法识别日期字段错误
Spark / Databricks Code not recognizing date field error
我正在查询已转换为日期类型的 spark 数据框中的列,如下所示:
SAlesByCountry2 = SAlesByCountry.withColumn("SaleDate", current_date())
转换成功见下图:
SAlesByCountry2:pyspark.sql.dataframe.DataFrame
CountryName:string
MakeName:string
ModelName:string
Cost:integer
RepairsCost:integer
PartsCost:string
TransportInCost:integer
Color:string
SalePrice:double
LineItemDiscount:string
InvoiceNumber:string
SaleDate:date
CustomerName:string
SalesDetailsID:integer`
但是,当我使用以下 sql 代码查询数据框时:
SELECT
*
FROM SAlesByCountry2
WHERE YEAR(SAlesByCountry2.SaleDate) = 2018
我没有得到任何数据,见下文
即使我查询整个数据框时确实存在 2018 年
这非常令人费解,因为它应该只显示数据,但我不明白为什么没有数据显示 2018 年的日期
你的代码有很多歧义。下面的语句不改变数据类型,它用今天的日期初始化值(所以在这种情况下,不可能获得 2018 年的数据)
SAlesByCountry2 = SAlesByCountry.withColumn("SaleDate", current_date())
我可以看到在您现有的 dataframe
中,列 SaleDate
的日期格式不同于日期格式 current_date()
return。 current_date()
return 日期格式为 yyyy-MM-dd
但您有 dd/MM/yyyy
的数据,其中 year 函数不起作用。
scala> df.withColumn("SaleDate", current_date()).select(col("SaleDate")).show
+----------+
| SaleDate|
+----------+
|2019-09-30|
|2019-09-30|
|2019-09-30|
|2019-09-30|
|2019-09-30|
|2019-09-30|
+----------+
year
函数在日期格式为 yyyy-MM-dd
.
的数据时起作用
scala> df.withColumn("SaleDate", date_format(current_date(), "dd/MM/yyyy")).filter(year(col("SaleDate")) === "2019").select(col("SaleDate")).show()
+--------+
|SaleDate|
+--------+
+--------+
scala> df.withColumn("SaleDate", date_format(current_date(), "yyyy-MM-dd")).filter(year(col("SaleDate")) === "2019").select(col("SaleDate")).show
+----------+
| SaleDate|
+----------+
|2019-09-30|
|2019-09-30|
|2019-09-30|
|2019-09-30|
|2019-09-30|
|2019-09-30|
+----------+
因此,对于您的问题的解决方案,您需要将 SaleDate
列中的日期格式更改为 yyyy-MM-dd
,如下所示,并确认 SaleDate
列对所有行具有唯一格式。
df.withColumn("SaleDate" , date_format(to_date(col("SaleDate"), "dd/MM/yyyy"), "yyyy-MM-dd")).filter(year(col("SaleDate")) === "2018")
我正在查询已转换为日期类型的 spark 数据框中的列,如下所示:
SAlesByCountry2 = SAlesByCountry.withColumn("SaleDate", current_date())
转换成功见下图:
SAlesByCountry2:pyspark.sql.dataframe.DataFrame
CountryName:string
MakeName:string
ModelName:string
Cost:integer
RepairsCost:integer
PartsCost:string
TransportInCost:integer
Color:string
SalePrice:double
LineItemDiscount:string
InvoiceNumber:string
SaleDate:date
CustomerName:string
SalesDetailsID:integer`
但是,当我使用以下 sql 代码查询数据框时:
SELECT
*
FROM SAlesByCountry2
WHERE YEAR(SAlesByCountry2.SaleDate) = 2018
我没有得到任何数据,见下文
即使我查询整个数据框时确实存在 2018 年
这非常令人费解,因为它应该只显示数据,但我不明白为什么没有数据显示 2018 年的日期
你的代码有很多歧义。下面的语句不改变数据类型,它用今天的日期初始化值(所以在这种情况下,不可能获得 2018 年的数据)
SAlesByCountry2 = SAlesByCountry.withColumn("SaleDate", current_date())
我可以看到在您现有的 dataframe
中,列 SaleDate
的日期格式不同于日期格式 current_date()
return。 current_date()
return 日期格式为 yyyy-MM-dd
但您有 dd/MM/yyyy
的数据,其中 year 函数不起作用。
scala> df.withColumn("SaleDate", current_date()).select(col("SaleDate")).show
+----------+
| SaleDate|
+----------+
|2019-09-30|
|2019-09-30|
|2019-09-30|
|2019-09-30|
|2019-09-30|
|2019-09-30|
+----------+
year
函数在日期格式为 yyyy-MM-dd
.
scala> df.withColumn("SaleDate", date_format(current_date(), "dd/MM/yyyy")).filter(year(col("SaleDate")) === "2019").select(col("SaleDate")).show()
+--------+
|SaleDate|
+--------+
+--------+
scala> df.withColumn("SaleDate", date_format(current_date(), "yyyy-MM-dd")).filter(year(col("SaleDate")) === "2019").select(col("SaleDate")).show
+----------+
| SaleDate|
+----------+
|2019-09-30|
|2019-09-30|
|2019-09-30|
|2019-09-30|
|2019-09-30|
|2019-09-30|
+----------+
因此,对于您的问题的解决方案,您需要将 SaleDate
列中的日期格式更改为 yyyy-MM-dd
,如下所示,并确认 SaleDate
列对所有行具有唯一格式。
df.withColumn("SaleDate" , date_format(to_date(col("SaleDate"), "dd/MM/yyyy"), "yyyy-MM-dd")).filter(year(col("SaleDate")) === "2018")