【问题标题】:How to shorten table entrees for duplications如何缩短重复的表主菜
【发布时间】:2019-12-20 22:20:23
【问题描述】:

我有一个包含 20 万条记录的源数据集。我有两列,我想计算不同的值:

我有这个:

val scr1 = spark.read.parquet("src1.parquet")
val dAppr = bp.groupBy("approver").count().toDF("name","Role1")
val cols1 = dAppr.columns.toSet

val dRevr = bp.groupBy("submitter").count().toDF("name","Role2")
val cols2 = dRevr.columns.toSet

val Signers1 = cols1 ++ cols2

def expr(myCols: Set[String], allCols: Set[String]) = {
  allCols.toList.map(x => x match {
    case x if myCols.contains(x) => col(x)
    case _ => lit(null).as(x)
  })
}

dAppr.select(expr(cols1, Signers1):_*).unionAll(dRevr.select(expr(cols2, Signers1):_*)).show()

我明白了:

+--------+---------+----------+
|    name|    Role1|     Role2|
+--------+---------+----------+
|Person A|    19421|      null|
|Person B|    41993|      null|
|Person C|    58822|      null|
|Person D|    48920|      null|
|Person A|     null|     53615|
|Person B|     null|     55904|
|Person C|     null|       118|
|Person D|     null|     59519|
+--------+---------+----------+

我想要(或者,我认为我想要):

+--------+---------+---------+
|    name|    Role1|    Role2|
+--------+---------+---------+
|Person A|    19421|    53615|
|Person B|    41993|    55904|
|Person C|    58822|      118|
|Person D|    48920|    59519|
+--------+---------+---------+

【问题讨论】:

    标签: apache-spark join union


    【解决方案1】:

    您应该在最后一行代码中使用join 而不是union

    看起来你可以在name 列上使用full join

    加入

    Joins with another :class:DataFrame, using the given join expression

    联合

    Return a new :class:DataFrame containing union of rows in this and another frame

    编辑:

    您可以删除这些行:

    def expr(myCols: Set[String], allCols: Set[String]) = {
      allCols.toList.map(x => x match {
        case x if myCols.contains(x) => col(x)
        case _ => lit(null).as(x)
      })
    }
    
    dAppr.select(expr(cols1, Signers1):_*).unionAll(dRevr.select(expr(cols2, Signers1):_*)).show()
    

    并在相关列上使用全连接和选择。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-04
      • 2020-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多