package sparksql.day01
import org.apache.log4j.{Level, Logger}
import org.apache.spark.SparkConf
import org.apache.spark.sql.{Row, SparkSession}
import org.apache.spark.sql.expressions.{MutableAggregationBuffer, UserDefinedAggregateFunction}
import org.apache.spark.sql.types.{DataType, LongType, StructField, StructType}
object sparkUDAF {
def main(args: Array[String]): Unit = {
Logger.getLogger("org").setLevel(Level.ERROR)
System.setProperty("hadoop.home.dir", "D:\\spark")
val conf = new SparkConf().setAppName("spakrsql").setMaster("local[*]")
val spark = SparkSession.builder().config(conf).getOrCreate()
import spark.implicits._
val df = spark.read.json("data/user.json")
//df.show()
df.createOrReplaceTempView("user")
spark.udf.register("ageavg",new MyAgeAVGUDAF())
spark.sql("select ageavg(age) from user").show()
// spark.sql("se
sparkSQL 自定义UDAF函数(弱类型的方式)spark1.x spark2.x
最新推荐文章于 2024-06-27 17:03:37 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)