当前位置:网站首页>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 |
+---+
}
边栏推荐
- LeetCode 1598. Folder operation log collector
- Gd32f4xx UIP protocol stack migration record
- Classic CTF topic about FTP protocol
- Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
- Basic introduction and source code analysis of webrtc threads
- DEJA_VU3D - Cesium功能集 之 055-国内外各厂商地图服务地址汇总说明
- Global and Chinese market of digital serial inverter 2022-2028: Research Report on technology, participants, trends, market size and share
- An understanding of & array names
- How to solve the problems caused by the import process of ecology9.0
- Determinant learning notes (I)
猜你喜欢
LeetCode 1189. Maximum number of "balloons"
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
What are Yunna's fixed asset management systems?
Determinant learning notes (I)
选择致敬持续奋斗背后的精神——对话威尔价值观【第四期】
MySQL functions
Problems encountered in the database
硬件及接口学习总结
Detailed explanation of APP functions of door-to-door appointment service
notepad++正則錶達式替換字符串
随机推荐
剖面测量之提取剖面数据
anconda下载+添加清华+tensorflow 安装+No module named ‘tensorflow‘+KernelRestarter: restart failed,内核重启失败
7.5 装饰器
JS can really prohibit constant modification this time!
Codeforces gr19 D (think more about why the first-hand value range is 100, JLS yyds)
Power query data format conversion, Split Merge extraction, delete duplicates, delete errors, transpose and reverse, perspective and reverse perspective
FFMPEG关键结构体——AVFormatContext
notepad++正則錶達式替換字符串
DEJA_ Vu3d - cesium feature set 055 - summary description of map service addresses of domestic and foreign manufacturers
Tools to improve work efficiency: the idea of SQL batch generation tools
STM32 configuration after chip replacement and possible errors
18. (ArcGIS API for JS) ArcGIS API for JS point collection (sketchviewmodel)
FFmpeg抓取RTSP图像进行图像分析
Pointer - character pointer
【DesignMode】组合模式(composite mode)
什么叫做信息安全?包含哪些内容?与网络安全有什么区别?
Huawei equipment is configured with OSPF and BFD linkage
2022.7.5-----leetcode. seven hundred and twenty-nine
Codeforces round 804 (Div. 2) [competition record]
[designmode] composite mode