scala - 如何在 Spark 3.0+ 中获得一年中的一周?
问题描述
我正在尝试创建一个包含日、月等列的日历文件。以下代码可以正常工作,但我找不到一种干净的方法来提取一年中的一周(1-52)。在spark 3.0+
中,以下代码行不起作用:.withColumn("week_of_year", date_format(col("day_id"), "W"))
我知道我可以创建一个视图/表,然后在其上运行 SQL 查询以提取week_of_year
,但是没有更好的方法吗?`
df.withColumn("day_id", to_date(col("day_id"), date_fmt))
.withColumn("week_day", date_format(col("day_id"), "EEEE"))
.withColumn("month_of_year", date_format(col("day_id"), "M"))
.withColumn("year", date_format(col("day_id"), "y"))
.withColumn("day_of_month", date_format(col("day_id"), "d"))
.withColumn("quarter_of_year", date_format(col("day_id"), "Q"))
解决方案
您看到的异常,建议使用 EXTRACT SQL 函数而不是https://spark.apache.org/docs/3.0.0/api/sql/index.html#extract
val df = Seq(("2019-11-16 16:50:59.406")).toDF("input_timestamp")
df.selectExpr("input_timestamp", "extract(week FROM input_timestamp) as w").show
+--------------------+---+
| input_timestamp| w|
+--------------------+---+
|2019-11-16 16:50:...| 46|
+--------------------+---+
推荐阅读
- drupal - 是否有显示为 Drupal 核心发布的最新安全补丁的 RSS 提要
- iis - IIS 重写规则来编辑传出内容以代理 HTTP 图像文件?
- mongodb - 如何在mongodb的过滤器中使用自己的元素过滤文档
- vb.net - 如何在变量声明中使用 Type 类作为类型
- elixir - 是否应该将 :name 和/或 :app 添加到伞形应用程序的根 mix.exs 文件中的项目定义中?
- autohotkey - 仅当鼠标位于屏幕的某个坐标而不干扰我的键盘时,如何才能激活脚本?
- paypal - Paypal 智能按钮订阅状态
- c - 获取“来自调试器的消息:错误 1” - 用于简单项目(在 Xcode 11.0 11A420a 中)
- kubernetes-helm - 如何合并 helm chart 和 Vault Agent tempate?
- java - 如何在不更改其他字母的情况下从字体更改字体样式?