简体   繁体   English

在 Spark/Scala 中使用 date_format 将时间戳转换为星期几字符串

[英]Convert timestamp to day-of-week string with date_format in Spark/Scala

I keep on getting error, that I pass too many arguments and not sure why, as I am following the exact examples from:我不断收到错误,我传递了太多参数但不知道为什么,因为我正在遵循以下确切示例:

command-629173529675356:9: error: too many arguments for method apply: (colName: String)org.apache.spark.sql.Column in class Dataset
val df_2 = date_format.withColumn("day_of_week", date_format(col("date"), "EEEE"))

My code:我的代码:

val date_format = df_filter.withColumn("date", to_date(col("pickup_datetime")))
val df_2 = date_format.withColumn("day_of_week", date_format(col("date"), "EEEE"))

Thank you for the help!感谢您的帮助!

dayofweek是你正在寻找的功能,所以像这样

date_format.withColumn("day_of_week", dayofweek(col("date")))

You get your error because you named your first dataframe date_format , which is the same name as the Spark's built-in function you want to use.您收到错误是因为您将第一个数据帧命名为date_format ,该名称与您要使用的 Spark 内置函数的名称相同。 So when you call date_format , you're retrieving your dataframe instead of date_format built-in function.因此,当您调用date_format ,您正在检索数据帧而不是date_format内置函数。

To solve this, you should either rename your first dataframe:要解决此问题,您应该重命名您的第一个数据框:

val df_1 = df_filter.withColumn("date", to_date(col("pickup_datetime")))
val df_2 = df_1.withColumn("day_of_week", date_format(col("date"), "EEEE"))

Or ensure that you're calling right date_format by importing functions and then call functions.date_format when extracting day of week:或者确保您通过导入functions调用正确的date_format ,然后在提取星期几时调用functions.date_format

import org.apache.spark.sql.functions

val date_format = df_filter.withColumn("date", to_date(col("pickup_datetime")))
val df_2 = date_format.withColumn("day_of_week", functions.date_format(col("date"), "EEEE"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM