当前位置:网站首页>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 |
+---+
}
边栏推荐
- PHP determines whether an array contains the value of another array
- Key structure of ffmpeg -- AVCodecContext
- Go learning --- structure to map[string]interface{}
- LeetCode 1189. Maximum number of "balloons"
- Codeforces Round #804 (Div. 2)【比赛记录】
- [designmode] adapter pattern
- LeetCode 6006. Take out the least number of magic beans
- Ffmpeg learning - core module
- Tools to improve work efficiency: the idea of SQL batch generation tools
- 电机的简介
猜你喜欢
如何解决ecology9.0执行导入流程流程产生的问题
Uniapp development, packaged as H5 and deployed to the server
What are the functions of Yunna fixed assets management system?
MDK debug时设置数据实时更新
Key structure of ffmpeg -- AVCodecContext
Room cannot create an SQLite connection to verify the queries
行列式学习笔记(一)
数据分析思维分析方法和业务知识——分析方法(二)
Ffmpeg captures RTSP images for image analysis
Single merchant v4.4 has the same original intention and strength!
随机推荐
Global and Chinese markets for hinged watertight doors 2022-2028: Research Report on technology, participants, trends, market size and share
After summarizing more than 800 kubectl aliases, I'm no longer afraid that I can't remember commands!
小程序技术优势与产业互联网相结合的分析
Intranet Security Learning (V) -- domain horizontal: SPN & RDP & Cobalt strike
Classic CTF topic about FTP protocol
Calculate sha256 value of data or file based on crypto++
LeetCode 1598. Folder operation log collector
JS 这次真的可以禁止常量修改了!
Hudi of data Lake (2): Hudi compilation
Detailed explanation of APP functions of door-to-door appointment service
Determinant learning notes (I)
Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of water heater expansion tank 2022-2028: Research Report on technology, participants, trends, market size and share
Single merchant v4.4 has the same original intention and strength!
MDK debug时设置数据实时更新
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
Priority queue (heap)
FFmpeg抓取RTSP图像进行图像分析
FFMPEG关键结构体——AVFrame
选择致敬持续奋斗背后的精神——对话威尔价值观【第四期】