当前位置:网站首页>Spark SQL UDF function
Spark SQL UDF function
2022-07-25 15:15:00 【The south wind knows what I mean】
UDF function
def main(args: Array[String]): Unit = {
val session: SparkSession = SparkSession
.builder()
.config("dfs.ha.namenodes.nameservices","xxxxx")
.appName("sql demo")
.master("local")
.enableHiveSupport()
.getOrCreate()
// Custom function
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)
// usage 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 |
+-------------+----+
// usage 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
- args参数解析
- golang复习总结
- Debounce and throttle
- MeanShift聚类-01原理分析
- Nacos2.1.0 cluster construction
- js URLEncode函数
- npm的nexus私服 E401 E500错误处理记录
- [comprehensive pen test] difficulty 4/5, classic application of line segment tree for character processing
- Award winning interaction | 7.19 database upgrade plan practical Summit: industry leaders gather, why do they come?
猜你喜欢

Run redis on docker to start in the form of configuration file, and the connection client reports an error: server closed the connection

Use the command to check the WiFi connection password under win10 system

How much memory can a program use at most?

pl/sql 创建并执行oralce存储过程,并返回结果集

NPM's nexus private server e401 E500 error handling record

Docker上运行redis以配置文件方式启动,连接客户端报错Error: Server closed the connection

Example of password strength verification

Overview of JS synchronous, asynchronous, macro task and micro task

Image cropper example

Simulate setinterval timer with setTimeout
随机推荐
如何更新更新数据库中的json值?
推荐10个堪称神器的学习网站
Spark SQL common time functions
Outline and box shadow to achieve the highlight effect of contour fillet
C, c/s upgrade update
Spark获取DataFrame中列的方式--col,$,column,apply
Example of password strength verification
浏览器工作流程(简化)
《三子棋》C语言数组应用 --n皇后问题雏形
TypeScript学习1——数据类型
用OpenPose进行单个或多个人体姿态估计
密码强度验证示例
Es5 thinking of writing inheritance
args参数解析
我的创作纪念日
Run redis on docker to start in the form of configuration file, and the connection client reports an error: server closed the connection
MeanShift聚类-01原理分析
My creation anniversary
[comprehensive pen test] difficulty 4/5, classic application of line segment tree for character processing
Introduction to raspberry Pie: initial settings of raspberry pie