当前位置:网站首页>Spark-SQL UDF函数
Spark-SQL UDF函数
2022-07-06 00:23: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 |
+---+
}
边栏推荐
- [noi simulation] Anaid's tree (Mobius inversion, exponential generating function, Ehrlich sieve, virtual tree)
- FFMPEG关键结构体——AVCodecContext
- Go learning --- read INI file
- About the slmgr command
- XML Configuration File
- SQLServer连接数据库读取中文乱码问题解决
- 常用API类及异常体系
- [Online gadgets] a collection of online gadgets that will be used in the development process
- 7.5 decorator
- 【EI会议分享】2022年第三届智能制造与自动化前沿国际会议(CFIMA 2022)
猜你喜欢
剖面测量之提取剖面数据
Data analysis thinking analysis methods and business knowledge - analysis methods (III)
There is no network after configuring the agent by capturing packets with Fiddler mobile phones
Wechat applet -- wxml template syntax (with notes)
AtCoder Beginner Contest 254【VP记录】
notepad++正则表达式替换字符串
小程序技术优势与产业互联网相结合的分析
Configuring OSPF load sharing for Huawei devices
What are the functions of Yunna fixed assets management system?
Notepad + + regular expression replace String
随机推荐
STM32 configuration after chip replacement and possible errors
Yolov5、Pycharm、Anaconda环境安装
MySQL functions
Global and Chinese markets of POM plastic gears 2022-2028: Research Report on technology, participants, trends, market size and share
[Online gadgets] a collection of online gadgets that will be used in the development process
PV static creation and dynamic creation
LeetCode 6005. The minimum operand to make an array an alternating array
NSSA area where OSPF is configured for Huawei equipment
Multithreading and high concurrency (8) -- summarize AQS shared lock from countdownlatch (punch in for the third anniversary)
关于结构体所占内存大小知识
7.5模拟赛总结
Leetcode:20220213 week race (less bugs, top 10% 555)
Codeforces round 804 (Div. 2) [competition record]
Classic CTF topic about FTP protocol
[Chongqing Guangdong education] reference materials for Zhengzhou Vocational College of finance, taxation and finance to play around the E-era
Mysql - CRUD
Anconda download + add Tsinghua +tensorflow installation +no module named 'tensorflow' +kernelrestart: restart failed, kernel restart failed
The difference of time zone and the time library of go language
anconda下载+添加清华+tensorflow 安装+No module named ‘tensorflow‘+KernelRestarter: restart failed,内核重启失败
Upgrade openssl-1.1.1p for openssl-1.0.2k