简体   繁体   English

用元组触发aggregateByKey

[英]spark aggregateByKey with tuple

New to the RDD api of spark - thanks to Spark migrate sql window function to RDD for better performance - I managed to generate the following table: spark的RDD api的新功能 -感谢Spark将sql窗口功能迁移到RDD以获得更好的性能 -我设法生成了下表:

+-----------------+---+
|               _1| _2|
+-----------------+---+
|  [col3TooMany,C]|  0|
|         [col1,A]|  0|
|         [col2,B]|  0|
|  [col3TooMany,C]|  1|
|         [col1,A]|  1|
|         [col2,B]|  1|
|[col3TooMany,jkl]|  0|
|         [col1,d]|  0|
|         [col2,a]|  0|
|  [col3TooMany,C]|  0|
|         [col1,d]|  0|
|         [col2,g]|  0|
|  [col3TooMany,t]|  1|
|         [col1,A]|  1|
|         [col2,d]|  1|
|  [col3TooMany,C]|  1|
|         [col1,d]|  1|
|         [col2,c]|  1|
|  [col3TooMany,C]|  1|
|         [col1,c]|  1|
+-----------------+---+

with an initial input of 初始输入为

val df = Seq(
    (0, "A", "B", "C", "D"),
    (1, "A", "B", "C", "D"),
    (0, "d", "a", "jkl", "d"),
    (0, "d", "g", "C", "D"),
    (1, "A", "d", "t", "k"),
    (1, "d", "c", "C", "D"),
    (1, "c", "B", "C", "D")
  ).toDF("TARGET", "col1", "col2", "col3TooMany", "col4")

  val columnsToDrop = Seq("col3TooMany")
  val columnsToCode = Seq("col1", "col2")
  val target = "TARGET"

import org.apache.spark.sql.functions._

  val exploded = explode(array(
    (columnsToDrop ++ columnsToCode).map(c =>
      struct(lit(c).alias("k"), col(c).alias("v"))): _*
  )).alias("level")

  val long = df.select(exploded, $"TARGET")

  import org.apache.spark.util.StatCounter

then 然后

long.as[((String, String), Int)].rdd.aggregateByKey(StatCounter())(_ merge _, _ merge _).collect.head
res71: ((String, String), org.apache.spark.util.StatCounter) = ((col2,B),(count: 3, mean: 0,666667, stdev: 0,471405, max: 1,000000, min: 0,000000))

is aggregating statistics of all the unique values for each column. 汇总每列所有唯一值的统计信息。

How can I add to the count (which is 3 for B in col2 ) a second count (maybe as a tuple) which represents the number of B in col2 where TARGET == 1 . 我如何将第二个计数(可能是一个元组)加到countcol2 B值为3 ),它表示col2B的数量,其中TARGET == 1 In this case, it should be 2 . 在这种情况下,应该为2

You shouldn't need additional aggregate here. 您无需在此处添加其他汇总。 With binary target column, mean is just an empirical probability of target being equal 1: 对于二进制target列, mean只是target等于1的经验概率:

  • number of 1 - count * mean 1的数量- count * mean
  • number of 0 - count * (1 - mean ) 数字0- count *(1- mean

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM