Scala:获取过去24个月的所有组合 [英] Scala: Get every combination of the last 24 months

查看:40
本文介绍了Scala:获取过去24个月的所有组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Spark中生成一个DataFrame(但也许只是Scala就足够了),其中我拥有过去24个月的所有组合,其中第二年月总是>第一年月.

I'm trying to generate a DataFrame in Spark (but perhaps just Scala is enough) in which I have every combination of the last 24 months where the second year-month is always > the first year-month.

例如,截至本文撰写之日为2019年3月1日,我在追求类似的东西:

For example, it is the 1 March 2019 as of writing this, I'm after something like:

List(
(2017, 3, 2017, 4),
(2017, 3, 2017, 5),
(2017, 3, 2017, 6),
// ..
(2017, 3, 2019, 3),
(2017, 4, 2017, 5),
// ..
(2019, 1, 2019, 3),
(2019, 2, 2019, 3),
)

推荐答案

在不涉及Spark的情况下,使用纯Scala最容易做到这一点.首先,计算最近24个月内所有(年,月)元组的列表.这可以通过使用 java.time 和一个Stream来完成,如下所示:

This is easiest done with pure Scala without involving Spark. First, compute the list of all (year, month) tuples of the last 24 months. This can be done by using java.time and a Stream as follows:

import java.time.LocalDate

val numMonths = 24
val now = LocalDate.now()
val startTime = now.minusMonths(numMonths)

lazy val dateStream: Stream[LocalDate] = startTime #:: dateStream.map(_.plusMonths(1))
val dates = dateStream.take(numMonths + 1).toSeq.map(t => (t.getYear(), t.getMonth().getValue()))

接下来,只需找到该元组序列的所有2个组合.这将自动满足第二个月应该在第一个月之后的条件.

Next, simply find all the 2-combinations of this tuple-sequence. This will automatically fulfill the condition that the second month should be after the first.

val datePerms = dates.combinations(2).map(c => (c(0)._1, c(0)._2, c(1)._1, c(1)._2))

如有必要,您可以使用 toDF 方法轻松地将其转换为数据框.

You can easily convert this to a dataframe with the toDF method if necessary.

这篇关于Scala:获取过去24个月的所有组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆