当前位置:网站首页>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 |
+---+
}
边栏推荐
- 海缆探测仪TSS350(一)
- Run redis on docker to start in the form of configuration file, and the connection client reports an error: server closed the connection
- [Android] recyclerview caching mechanism, is it really difficult to understand? What level of cache is it?
- Spark002---spark任务提交,传入json作为参数
- Single or multiple human posture estimation using openpose
- Share a department design method that avoids recursion
- Maxcompute SQL 的查询结果条数受限1W
- 反射-笔记
- C, c/s upgrade update
- Fast-lio: fast and robust laser inertial odometer based on tightly coupled IEKF
猜你喜欢

树莓派入门:树莓派的初始设置

MySQL之事务与MVCC

Implementation of asynchronous FIFO
[Android] recyclerview caching mechanism, is it really difficult to understand? What level of cache is it?

ESXI6.7.0 升级到7.0U3f(2022年7月12 更新)

Spark 内存管理机制 新版

反射-笔记

Visual Studio 2022 查看类关系图

ice 100G 网卡分片报文 hash 问题

How much memory can a program use at most?
随机推荐
Object.prototype. Hasownproperty() and in
How much memory can a program use at most?
Spark-SQL UDF函数
MySQL installation and configuration super detailed tutorial and simple database and table building method
一个程序最多可以使用多少内存?
Spark 内存管理机制 新版
什么是物联网
瀑布流布局
海缆探测仪TSS350(一)
ESXI6.7.0 升级到7.0U3f(2022年7月12 更新)
继承的实现过程及ES5和ES6实现的区别
ice 100G 网卡分片报文 hash 问题
基于OpenCV和YOLOv3的目标检测实例应用
Yan required executor memory is above the max threshold (8192mb) of this cluster!
SPI传输出现数据与时钟不匹配延后问题分析与解决
sql server强行断开连接
从 join on 和 where 执行顺序认识T-sql查询执行顺序
How to solve the problem of scanf compilation error in Visual Studio
Spark AQE
【JS高级】js之正则相关函数以及正则对象_02