当前位置:网站首页>Spark-SQL UDF函数
Spark-SQL UDF函数
2022-07-25 15:10:00 【南风知我意丿】
UDF函数
def main(args: Array[String]): Unit = {
val session: SparkSession = SparkSession
.builder()
.config("dfs.ha.namenodes.nameservices","xxxxx")
.appName("sql demo")
.master("local")
.enableHiveSupport()
.getOrCreate()
//自定义函数
val code: UserDefinedFunction = session.udf.register("code", (arg: Int) => {
if (arg > 5) "F" else "M"
})
val sql_1:String =
s""" |select * from |sparktuning.course_pay |""".stripMargin
val df: DataFrame = session.sql(sql_1)
//用法1.
df.select("chapterid")
.withColumn("flag",code(col("chapterid")))
.withColumnRenamed("chapterid","new_chapterid")
.show(false)
+-------------+----+
|new_chapterid|flag|
+-------------+----+
|4 |M |
|7 |F |
|8 |F |
|5 |M |
|4 |M |
|9 |F |
|7 |F |
|0 |M |
|5 |M |
|4 |M |
|4 |M |
|0 |M |
|1 |M |
|4 |M |
|9 |F |
+-------------+----+
//用法2.
val sql_2:String =
s""" |select | code(chapterid) Sex |from |sparktuning.course_pay |""".stripMargin
session.sql(sql_2).show(false)
+---+
|Sex|
+---+
|M |
|F |
|F |
|M |
|M |
|F |
|F |
|M |
|M |
|M |
|M |
|M |
|M |
|M |
|F |
+---+
}
边栏推荐
猜你喜欢

【JS高级】js之正则相关函数以及正则对象_02

39 简洁版小米侧边栏练习

Bridge NF call ip6tables is an unknown key exception handling

SPI传输出现数据与时钟不匹配延后问题分析与解决

oracle_12505错误解决方法

As methods for viewing and excluding dependencies

Client error: invalid param endpoint is blank

"Ask every day" reentrantlock locks and unlocks

"Ask every day" briefly talk about JMM / talk about your understanding of JMM

Nacos2.1.0 cluster construction
随机推荐
Log4j2 basic configuration
32 use of chrome debugging tools
Award winning interaction | 7.19 database upgrade plan practical Summit: industry leaders gather, why do they come?
解决asp.net上传文件时文件太大导致的错误
27 选择器的分类
Thymeleaf notes
Splice a field of the list set into a single string
Copy files / folders through Robocopy
[C题目]牛客 链表中倒数第k个结点
PHP implements non blocking (concurrent) request mode through native curl
L1 and L2 regularization
[comprehensive pen test] difficulty 4/5, classic application of line segment tree for character processing
Browser workflow (Simplified)
深入:微任务与宏任务
Detailed explanation of lio-sam operation process and code
Content type corresponding to office file
推荐10个堪称神器的学习网站
[C题目]力扣876. 链表的中间结点
"How to use" agent mode
TypeScript学习1——数据类型