【问题标题】:Scala new Map column from integer and string columns来自整数和字符串列的 Scala 新 Map 列
【发布时间】:2021-05-10 21:47:30
【问题描述】:

问题陈述:

我有一个包含四列的数据框:service(字符串)、show(字符串)、country_1(整数)和 country_2(整数)。我的目标是生成一个仅包含两列的数据框:服务(字符串)和信息(地图[整数,列表[字符串]])

每个流媒体服务的地图可以包含多个键值对记录:

{
    "34521": ["The Crown", "Bridgerton", "The Queen's Gambit"],
    "49678": ["The Crown", "Bridgerton", "The Queen's Gambit"]
}

需要注意的重要一点是,将来可以添加更多国家/地区,例如输入数据框中的其他几列,例如“country_3”、“country_4”等。解决方案代码的目标也是希望能够考虑对于这些事情,而不仅仅是像我在下面尝试的解决方案中所做的那样对选定的列进行硬编码,如果这有意义的话。

输入数据框:

架构:

root
|-- service: string (nullable = true)
|-- show: string (nullable = true)
|-- country_1: integer (nullable = true)
|-- country_2: integer (nullable = true)

数据框:

service     |      show        |   country_1   |   country_2

Netflix      The Crown               34521           49678
Netflix      Bridgerton              34521           49678
Netflix      The Queen's Gambit      34521           49678
Peacock      The Office              34521           49678
Disney+      WandaVision             34521           49678 
Disney+      Marvel's 616            34521           49678
Disney+      The Mandalorian         34521           49678
Apple TV     Ted Lasso               34521           49678
Apple TV     The Morning Show        34521           49678

输出数据框:

架构:

root
|-- service: string (nullable = true)
|-- information: map (nullable = false)
|    |-- key: integer
|    |-- value: array (valueContainsNull = true)
|    |    |-- element: string (containsNull = true)

数据框:

service    |  information          

Netflix    [34521 -> [The Crown, Bridgerton, The Queen’s Gambit], 49678 -> [The Crown, Bridgerton, The Queen’s Gambit]] 
Peacock    [34521 -> [The Office], 49678 -> [The Office]]
Disney+    [34521 -> [WandaVision, Marvel’s 616, The Mandalorian], 49678 -> [WandaVision, Marvel’s 616, The Mandalorian]]
Apple TV   [34521 -> [Ted Lasso, The Morning Show], 49678 -> [Ted Lasso, The Morning Show]]

我已经尝试过的

虽然我已经使用粘贴的代码 sn-p 成功生成了我想要的输出,但我不想依赖使用非常基本的 SQL 类型的命令,因为我认为它对于大型数据集的快速计算并不总是最佳的,此外,我不想依赖在映射时通过确切名称手动选择国家/地区列的方法,因为在以后可以添加更多国家/地区列的意义上,这总是会发生变化。

有没有更好的方法来利用 udfs、foldLeft 等类型的代码或任何其他有助于优化的方法,也有助于代码更简洁而不是混乱?

val df = spark.read.parquet("filepath/*.parquet") 
val temp = df.groupBy("service", "country_1", "country_2").agg(collect_list("show").alias("show"))
val service_information = grouped.withColumn("information", map(lit($"country_1"), $"show", lit($"country_2"), $"show")).drop("country_1", "country_2", "show")

【问题讨论】:

  • 几个问题:(1)您的country_X 列是否总是对所有行具有相同的国家代码(如果不是,您的groupBy 将导致某些service 出现多行) ? (2)country_X中是否有null,在这种情况下你的代码会被破坏(因为map不能接受null键)?
  • (1) 是的,country_X 列始终具有相同的确切国家代码,因此 country_1 将具有所有相同的值,country_2 将具有相同的值,如果将来,country_3 等也会具有相同的值,以及 (2) 那里country_x 列中没有空值
  • 那么,不是每个show 都与完全相同的国家列表绑定吗?在这种情况下,仅列出一个国家/地区列表还不够吗?
  • 我不完全确定我是否理解您的评论,但是关于输入国家列表而不是每个国家/地区的单独列,这是我无法控制的,因为这就是数据的方式我正在接收正在传递的信息。我特别想得到一个包含多个记录的地图,其中每个单独的国家/地区都是一个记录(键值对)的键,并且每个节目都与该地图相关。输出模式是我需要的,以适应我的其余用例的处理方式。
  • 用示例代码更容易说明。请看我的回答。

标签: scala dataframe dictionary user-defined-functions foldleft


【解决方案1】:

根据 cmets 部分中描述的国家/地区数据“规范”(即,对于任何给定的country_X 列,国家/地区代码在所有行中都是相同且非空的),您的代码可以泛化为处理任意多个国家/地区列:

val df = Seq(
  ("Netflix",     "The Crown",             34521,    49678),
  ("Netflix",     "Bridgerton",            34521,    49678),
  ("Netflix",     "The Queen's Gambit",    34521,    49678),
  ("Peacock",     "The Office",            34521,    49678),
  ("Disney+",     "WandaVision",           34521,    49678),
  ("Disney+",     "Marvel's 616",          34521,    49678),
  ("Disney+",     "The Mandalorian",       34521,    49678),
  ("Apple TV",    "Ted Lasso",             34521,    49678),
  ("Apple TV",    "The Morning Show",      34521,    49678)
).toDF("service", "show", "country_1", "country_2")

val countryCols = df.columns.filter(_.startsWith("country_")).toList

val grouped = df.groupBy("service", countryCols: _*).agg(collect_list("show").as("shows"))

val service_information = grouped.withColumn(
    "information",
    map( countryCols.flatMap{ c => col(c) :: col("shows") :: Nil }: _* )
  ).drop("shows" :: countryCols: _*)

service_information.show(false)
// +--------+--------------------------------------------------------------------------------------------------------------+
// |service |information                                                                                                   |
// +--------+--------------------------------------------------------------------------------------------------------------+
// |Disney+ |[34521 -> [WandaVision, Marvel's 616, The Mandalorian], 49678 -> [WandaVision, Marvel's 616, The Mandalorian]]|
// |Peacock |[34521 -> [The Office], 49678 -> [The Office]]                                                                |
// |Netflix |[34521 -> [The Crown, Bridgerton, The Queen's Gambit], 49678 -> [The Crown, Bridgerton, The Queen's Gambit]]  |
// |Apple TV|[34521 -> [Ted Lasso, The Morning Show], 49678 -> [Ted Lasso, The Morning Show]]                              |
// +--------+--------------------------------------------------------------------------------------------------------------+

请注意,所描述的国家/地区“规范”将要求所有shows 与同一国家/地区列表相关联。例如,如果您有 3 个 country_Xs 列,并且给定 country_X 的每一行都是相同的,没有空值,这意味着每个 show 都与这 3 个国家/地区相关联。如果您的 show 仅适用于 3 个国家/地区中的 2 个,该怎么办?


如果您的数据架构可以修改,维护相关国家信息的更灵活方法是为每个 show 设置一个 ArrayType 列。

val df = Seq(
  ("Netflix",     "The Crown",             Seq(34521, 49678)),
  ("Netflix",     "Bridgerton",            Seq(34521)),
  ("Netflix",     "The Queen's Gambit",    Seq(10001, 49678)),
  ("Peacock",     "The Office",            Seq(34521, 49678)),
  ("Disney+",     "WandaVision",           Seq(10001, 20002, 34521)),
  ("Disney+",     "Marvel's 616",          Seq(49678)),
  ("Disney+",     "The Mandalorian",       Seq(34521, 49678)),
  ("Apple TV",    "Ted Lasso",             Seq(34521, 49678)),
  ("Apple TV",    "The Morning Show",      Seq(20002, 34521))
).toDF("service", "show", "countries")

val grouped = df.withColumn("country", explode($"countries")).
  groupBy("service", "country").agg(collect_list($"show").as("shows"))

val service_information = grouped.groupBy("service").
  agg(collect_list($"country").as("c_list"), collect_list($"shows").as("s_list")).
  select($"service", map_from_arrays($"c_list", $"s_list").as("information"))

service_information.show(false)
// +--------+-----------------------------------------------------------------------------------------------------------------------------------+
// |service |information                                                                                                                        |
// +--------+-----------------------------------------------------------------------------------------------------------------------------------+
// |Peacock |[34521 -> [The Office], 49678 -> [The Office]]                                                                                     |
// |Disney+ |[20002 -> [WandaVision], 49678 -> [Marvel's 616, The Mandalorian], 34521 -> [WandaVision, The Mandalorian], 10001 -> [WandaVision]]|
// |Apple TV|[34521 -> [Ted Lasso, The Morning Show], 49678 -> [Ted Lasso], 20002 -> [The Morning Show]]                                        |
// |Netflix |[49678 -> [The Crown, The Queen's Gambit], 10001 -> [The Queen's Gambit], 34521 -> [The Crown, Bridgerton]]                        |
// +--------+-----------------------------------------------------------------------------------------------------------------------------------+

【讨论】:

  • 非常感谢!您能否帮助解释使用 Nil 的目的,或者更广泛地说,您的代码正在做什么以在您提供的第一个实现中创建 val service_information?
  • @corgi123, col(c) :: col("show") :: Nil 只是表达List(col(c), col("show")) 的一种方式,允许flatMap 将每个country 与show 逐行配对作为方法map 的参数。
  • 我觉得有道理,谢谢!关于第二次实施,如果将来我收到的数据在国家和节目之间更加多样化,而不像现在这样统一,这是一个很好的方法。
猜你喜欢
  • 2018-02-21
  • 2011-09-03
  • 2016-02-14
  • 1970-01-01
  • 2015-04-30
  • 1970-01-01
  • 2023-02-02
  • 1970-01-01
  • 2019-03-09
相关资源
最近更新 更多