数据
{"name":"uzi","age":20}
{"name":"gala","age":22}
{"name":"theshy","age":21}
代码
package com.bfd.sql
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.{DoubleType, LongType, StructType}
object SparkSQL06_UDF {
def main(args: Array[String]): Unit = {
//sparkSQL
//sparkConf
val sparkConf = new SparkConf()
.setMaster("local[*]")
.setAppName("sparkSql_06")
//sparkSession
val spark=
SparkSession.builder().config(sparkConf).getOrCreate()
import spark.implicits._
val frame =
spark
.read
.json("D:\\development\\code\\2021.01\\scala1\\spark_core\\src\\main\\in\\user.json")
//注册udf
spark.udf.register("addName",(x:String) => "name:"+x)
//给表一个名字
frame.createTempView("student")
//使用udf查询
spark.sql("select addName(name) from student").show()
//释放资源
spark.stop
}
}
结果