当前位置:网站首页>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 |
+---+
}
边栏推荐
- Global and Chinese market of digital serial inverter 2022-2028: Research Report on technology, participants, trends, market size and share
- AtCoder Beginner Contest 258【比赛记录】
- Hudi of data Lake (1): introduction to Hudi
- 如何利用Flutter框架开发运行小程序
- MySql——CRUD
- Chapter 16 oauth2authorizationrequestredirectwebfilter source code analysis
- Power Query数据格式的转换、拆分合并提取、删除重复项、删除错误、转置与反转、透视和逆透视
- Atcoder beginer contest 258 [competition record]
- MDK debug时设置数据实时更新
- Hudi of data Lake (2): Hudi compilation
猜你喜欢
![Atcoder beginer contest 254 [VP record]](/img/13/656468eb76bb8b6ea3b6465a56031d.png)
Atcoder beginer contest 254 [VP record]

Key structure of ffmpeg - avframe

Opencv classic 100 questions

FFMPEG关键结构体——AVFormatContext

The relationship between FPGA internal hardware structure and code

Introduction of motor

Extension and application of timestamp

电机的简介

MySql——CRUD

AtCoder Beginner Contest 254【VP记录】
随机推荐
建立时间和保持时间的模型分析
LeetCode 6005. The minimum operand to make an array an alternating array
LeetCode 1598. Folder operation log collector
常用API类及异常体系
Model analysis of establishment time and holding time
Key structure of ffmpeg -- AVCodecContext
Hardware and interface learning summary
【线上小工具】开发过程中会用到的线上小工具合集
提升工作效率工具:SQL批量生成工具思想
OS i/o devices and device controllers
7.5 decorator
认识提取与显示梅尔谱图的小实验(观察不同y_axis和x_axis的区别)
FFT learning notes (I think it is detailed)
[Online gadgets] a collection of online gadgets that will be used in the development process
Codeforces round 804 (Div. 2) [competition record]
Natural language processing (NLP) - third party Library (Toolkit):allenlp [library for building various NLP models; based on pytorch]
Wechat applet -- wxml template syntax (with notes)
Multithreading and high concurrency (8) -- summarize AQS shared lock from countdownlatch (punch in for the third anniversary)
JS can really prohibit constant modification this time!
关于slmgr命令的那些事