弱类型用户自定义UDAF函数
继承UserDefinedAggregateFunction类
import org.apache.spark.SparkConf
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.expressions.{MutableAggregationBuffer, UserDefinedAggregateFunction}
import org.apache.spark.sql.types.{DataType, DoubleType, LongType, StructType}
import org.apache.spark.sql.{DataFrame, Dataset, Row, SparkSession}
object SparkSQL05_UDAF {
def main(args: Array[String]): Unit = {
//SparkSQL
//SparkConf
//创建配置对象
val sparkConf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("SparkSQL01_Demo")
val spark: SparkSession = SparkSession.builder().config(sparkConf).getOrCreate()
//进行转换之前,需要引入隐式转换规则
//这里的spark不是包名含义,是SparkSession对象的名字
import spark.implicits._
//自定义聚合函数
//创建聚合函数对象
val udaf = new MyAgeAvgFunction
//注册聚合函数
spark.udf.register("avgAge",udaf)
//使用聚合函数