当前位置:网站首页>Spark SQL UDF function
Spark SQL UDF function
2022-07-06 00:28: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 |
+---+
}
边栏推荐
- 【文件IO的简单实现】
- Huawei equipment is configured with OSPF and BFD linkage
- Reading notes of the beauty of programming
- [EI conference sharing] the Third International Conference on intelligent manufacturing and automation frontier in 2022 (cfima 2022)
- Extracting profile data from profile measurement
- Room cannot create an SQLite connection to verify the queries
- 【EI会议分享】2022年第三届智能制造与自动化前沿国际会议(CFIMA 2022)
- FFT learning notes (I think it is detailed)
- Spark SQL空值Null,NaN判断和处理
- Hardware and interface learning summary
猜你喜欢

Notepad++ regular expression replacement string

Location based mobile terminal network video exploration app system documents + foreign language translation and original text + guidance records (8 weeks) + PPT + review + project source code

Spark SQL空值Null,NaN判断和处理

OS i/o devices and device controllers

Determinant learning notes (I)

如何解决ecology9.0执行导入流程流程产生的问题

Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot system behavior analysis and project summary (4

【NOI模拟赛】Anaid 的树(莫比乌斯反演,指数型生成函数,埃氏筛,虚树)

常用API类及异常体系

Ffmpeg captures RTSP images for image analysis
随机推荐
Intranet Security Learning (V) -- domain horizontal: SPN & RDP & Cobalt strike
Spark获取DataFrame中列的方式--col,$,column,apply
[Chongqing Guangdong education] Chongqing Engineering Vocational and Technical College
电机的简介
XML Configuration File
DEJA_VU3D - Cesium功能集 之 055-国内外各厂商地图服务地址汇总说明
Problems encountered in the database
LeetCode 1598. Folder operation log collector
Key structure of ffmpeg -- AVCodecContext
Leetcode:20220213 week race (less bugs, top 10% 555)
【DesignMode】适配器模式(adapter pattern)
Yolov5、Pycharm、Anaconda环境安装
LeetCode 斐波那契序列
N1 # if you work on a metauniverse product [metauniverse · interdisciplinary] Season 2 S2
Choose to pay tribute to the spirit behind continuous struggle -- Dialogue will values [Issue 4]
Go learning --- structure to map[string]interface{}
Ffmpeg learning - core module
Knowledge about the memory size occupied by the structure
Pointer pointer array, array pointer
How to use the flutter framework to develop and run small programs