【问题标题】:join spark df with list [duplicate]将 spark df 加入列表 [重复]
【发布时间】:2018-07-10 15:57:14
【问题描述】:

如何在 List 中使用 join/filter out spark RDD/DF

我有列表和火花 RDD

val list = List(12345,222222,333333,444444,555555,666666)

val friendPF=Seq(("bob", "2015-01-13", 12345), ("alicsdsdse", "2015-04-23",112120),("alice", "2015-04-23",1021212),("alsddsdsice", "2015-04-23",112120),("four", "2015-04-23",44444),("three", "2015-04-23",333333),("two", "2015-04-23",222222),("five", "2015-04-23",555555),("otowowo", "2015-04-23",1121210),("six", "2015-04-23",666666)).toDF("name","date","id")

friendPF.show

+-----------+----------+-------+
|       name|      date|     id|
+-----------+----------+-------+
|        bob|2015-01-13|  12345|
| alicsdsdse|2015-04-23| 112120|
|      alice|2015-04-23|1021212|
|alsddsdsice|2015-04-23| 112120|
|       four|2015-04-23|  44444|
|      three|2015-04-23| 333333|
|        two|2015-04-23| 222222|
|       five|2015-04-23| 555555|
|    otowowo|2015-04-23|1121210|
|        six|2015-04-23| 666666|
+-----------+----------+-------+

如何使用 join 从给定的 rdd 中获取匹配的 id?

【问题讨论】:

  • 您可以将列表转换为字符串并使用 lit 和 contains 进行过滤。这是最简单的解决方案,否则您将不得不使用 udf 函数

标签: scala apache-spark apache-spark-sql


【解决方案1】:

将您的list RDD 转换为数据框,如下所示

val listDF = List(12345,222222,333333,444444,555555,666666).toDF("id")

现在加入两个数据框

friendPF.as("rel").
    join(listDF.as("ids"),  $"ids.id" === $"rel.id").
    select( $"rel.name", $"rel.date",$"rel.id").show()

【讨论】:

    【解决方案2】:

    你不需要加入,使用isin:

    friendsPF
    .where($"id".isin(list:_*))
    .show()
    

    【讨论】:

      猜你喜欢
      • 2019-05-30
      • 1970-01-01
      • 2017-03-04
      • 1970-01-01
      • 2018-12-11
      • 2018-04-07
      • 2022-12-24
      • 2018-12-13
      • 2020-02-07
      相关资源
      最近更新 更多