【发布时间】: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