package sparkSql
import org.apache.spark.sql.SparkSession
/**
* @Author yqq
* @Date 2021/12/14 14:12
* @Version 1.0
*/
object SparkSQLUDF {
def main(args: Array[String]): Unit = {
val session = SparkSession.builder().master("local").appName("test01").getOrCreate()
val list: List[String] = List[String]("科比", "詹姆斯", "杜兰特", "霍华德")
import session.implicits._
val frame = list.toDF("name")
frame.createTempView("t")
/**
* 自定义nameLength函数,计算name长度
*/
session.udf.register("nameLength",(n:String)=>{
n.length()
})
session.sql(
"""
|select name,nameLength(name) as nl from t order by nl desc
|""".stripMargin).show()
}
}