如何在Scala中使用countDistinct和Spark?

时间:2015-11-03 13:49:46

标签: scala user-defined-functions apache-spark-sql

我已尝试使用 countDistinct 函数,该函数应根据DataBrick's blog在Spark 1.5中提供。但是,我得到以下例外:

Exception in thread "main" org.apache.spark.sql.AnalysisException: undefined function countDistinct;

我发现在Spark developers' mail list上他们建议使用 count distinct 函数来获得应该由生成的相同结果countDistinct

count(distinct <columnName>)
// Instead
countDistinct(<columnName>)

因为我从聚合函数名称列表中动态构建聚合表达式,所以我不喜欢不需要处理任何特殊情况。

那么,是否有可能通过以下方式统一它:

  • 注册新的UDAF,它将是 count(distinct columnName)的别名
  • 手动注册已在Spark CountDistinct函数中实现,该函数可能来自以下导入:

    import org.apache.spark.sql.catalyst.expressions。{CountDistinctFunction,CountDistinct}

  • 或以其他方式进行?

编辑: 示例(删除了一些本地引用和不必要的代码):

import org.apache.spark.SparkContext
import org.apache.spark.sql.{Column, SQLContext, DataFrame}
import org.apache.spark.sql.functions._

import scala.collection.mutable.ListBuffer


class Flattener(sc: SparkContext) {
  val sqlContext = new SQLContext(sc)

  def flatTable(data: DataFrame, groupField: String): DataFrame = {
    val flatteningExpressions = data.columns.zip(TypeRecognizer.getTypes(data)).
      flatMap(x => getFlatteningExpressions(x._1, x._2)).toList

    data.groupBy(groupField).agg (
      expr(s"count($groupField) as groupSize"),
      flatteningExpressions:_*
    )
  }

  private def getFlatteningExpressions(fieldName: String, fieldType: DType): List[Column] = {
    val aggFuncs = getAggregationFunctons(fieldType)

    aggFuncs.map(f => expr(s"$f($fieldName) as ${fieldName}_$f"))
  }

  private def getAggregationFunctons(fieldType: DType): List[String] = {
    val aggFuncs = new ListBuffer[String]()

    if(fieldType == DType.NUMERIC) {
      aggFuncs += ("avg", "min", "max")
    }

    if(fieldType == DType.CATEGORY) {
      aggFuncs += "countDistinct"
    }

    aggFuncs.toList
  }

}

2 个答案:

答案 0 :(得分:2)

不确定我是否真的了解您的问题,但这是countDistinct聚合函数的示例:

val values = Array((1, 2), (1, 3), (2, 2), (1, 2))
val myDf = sc.parallelize(values).toDF("id", "foo")
import org.apache.spark.sql.functions.countDistinct
myDf.groupBy('id).agg(countDistinct('foo) as 'distinctFoo) show
/**
+---+-------------------+
| id|COUNT(DISTINCT foo)|
+---+-------------------+
|  1|                  2|
|  2|                  1|
+---+-------------------+
*/

答案 1 :(得分:2)

countDistinct可以以两种不同的形式使用:

df.groupBy("A").agg(expr("count(distinct B)")

df.groupBy("A").agg(countDistinct("B"))

但是,当您想在自定义UDAF(在Spark 1.5中实现为UserDefinedAggregateFunction)的同一列上使用它们时,这两种方法都不起作用:

// Assume that we have already implemented and registered StdDev UDAF 
df.groupBy("A").agg(countDistinct("B"), expr("StdDev(B)"))

// Will cause
Exception in thread "main" org.apache.spark.sql.AnalysisException: StdDev is implemented based on the new Aggregate Function interface and it cannot be used with functions implemented based on the old Aggregate Function interface.;

由于这些限制,它看起来最合理的是将countDistinct实现为UDAF应该允许以相同的方式处理所有函数以及使用countDistinct和其他UDAF。

示例实现可能如下所示:

import org.apache.spark.sql.Row
import org.apache.spark.sql.expressions.{MutableAggregationBuffer, UserDefinedAggregateFunction}
import org.apache.spark.sql.types._

class CountDistinct extends UserDefinedAggregateFunction{
  override def inputSchema: StructType = StructType(StructField("value", StringType) :: Nil)

  override def update(buffer: MutableAggregationBuffer, input: Row): Unit = {
    buffer(0) = (buffer.getSeq[String](0).toSet + input.getString(0)).toSeq
  }

  override def bufferSchema: StructType = StructType(
      StructField("items", ArrayType(StringType, true)) :: Nil
  )

  override def merge(buffer1: MutableAggregationBuffer, buffer2: Row): Unit = {
    buffer1(0) = (buffer1.getSeq[String](0).toSet ++ buffer2.getSeq[String](0).toSet).toSeq
  }

  override def initialize(buffer: MutableAggregationBuffer): Unit = {
    buffer(0) = Seq[String]()
  }

  override def deterministic: Boolean = true

  override def evaluate(buffer: Row): Any = {
    buffer.getSeq[String](0).length
  }

  override def dataType: DataType = IntegerType
}