Scala:获取过去24个月的所有组合

编程入门 行业动态 更新时间:2024-10-28 20:22:01
本文介绍了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 = datesbinations(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个月的所有组合

本文发布于:2023-10-21 17:00:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1514811.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   个月   Scala

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!