当前位置:网站首页>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 |
+---+
}
边栏推荐
- Wechat applet -- wxml template syntax (with notes)
- LeetCode 斐波那契序列
- 7.5 simulation summary
- Gd32f4xx UIP protocol stack migration record
- Determinant learning notes (I)
- wx.getLocation(Object object)申请方法,最新版
- Configuring OSPF load sharing for Huawei devices
- Leetcode 450 deleting nodes in a binary search tree
- Hardware and interface learning summary
- 从底层结构开始学习FPGA----FIFO IP核及其关键参数介绍
猜你喜欢

Key structure of ffmpeg - avformatcontext

MySql——CRUD

Senparc. Weixin. Sample. MP source code analysis

小程序技术优势与产业互联网相结合的分析

Browser local storage

FFMPEG关键结构体——AVCodecContext

wx.getLocation(Object object)申请方法,最新版

Miaochai Weekly - 8

Basic introduction and source code analysis of webrtc threads

【EI会议分享】2022年第三届智能制造与自动化前沿国际会议(CFIMA 2022)
随机推荐
常用API类及异常体系
Choose to pay tribute to the spirit behind continuous struggle -- Dialogue will values [Issue 4]
DEJA_VU3D - Cesium功能集 之 055-国内外各厂商地图服务地址汇总说明
关于slmgr命令的那些事
How much do you know about the bank deposit business that software test engineers must know?
Key structure of ffmpeg - avformatcontext
FFT 学习笔记(自认为详细)
Global and Chinese markets of universal milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
wx. Getlocation (object object) application method, latest version
7.5模拟赛总结
Pointer - character pointer
PHP determines whether an array contains the value of another array
多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)
2022.7.5-----leetcode.729
Ffmpeg learning - core module
小程序技术优势与产业互联网相结合的分析
Calculate sha256 value of data or file based on crypto++
数据分析思维分析方法和业务知识——分析方法(二)
Global and Chinese markets for hinged watertight doors 2022-2028: Research Report on technology, participants, trends, market size and share
Permission problem: source bash_ profile permission denied