大多数情况下,每周的所有日历实用程序都在星期日/星期日或星期一/星期一进行配置。
我想从星期六到星期五进行配置:
假设我每周累积11月份的数据:
var sample = Seq(("C1", "01/11/2018", 20), ("C1", "02/11/2018", 5),
("C1", "03/11/2018", 2), ("C1", "04/11/2018", 3), ("C1", "05/11/2018", 5),
("C1", "08/11/2018", 5), ("C1", "10/11/2018", 10), ("C1", "12/11/2018", 10),
("C1", "14/11/2018", 16), ("C1", "15/11/2018", 6), ("C1", "18/11/2018", 22)).toDF("id", "order_date", "orders")
sample = sample.withColumn(
"order_date",
to_date(unix_timestamp($"order_date", "dd/MM/yyyy").cast("timestamp")))
val Row(start: java.sql.Date, end: java.sql.Date) = sample.
select(min($"order_date"), max($"order_date")).
first
val weeksDF = (0L to ChronoUnit.WEEKS.between(
start.toLocalDate, end.toLocalDate)).
map(i => java.sql.Date.valueOf(start.toLocalDate.plusWeeks(i))).
toDF("order_date")
Desired Output:
02/11/2018 25
09/11/2018 15
16/11/2018 42
23/11/2018 22
非常感谢您的帮助。
答案 0 :(得分:2)
我认为您可以通过一种更简单的方式来做到这一点,只需要获取下一个星期五并将所有日期汇总到下一个星期五,这样就可以为每个日期获取下一个星期五,如下所示:
val localDate = LocalDate.now() // your date here
val nextFriday = localDate.`with`(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY))
然后,您将在下星期五添加一个新列,并对其进行分组并求和