当前位置:网站首页>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 |
+---+
}
边栏推荐
- 2022-02-13 work record -- PHP parsing rich text
- 认识提取与显示梅尔谱图的小实验(观察不同y_axis和x_axis的区别)
- Hudi of data Lake (2): Hudi compilation
- [binary search tree] add, delete, modify and query function code implementation
- 建立时间和保持时间的模型分析
- Room cannot create an SQLite connection to verify the queries
- FFMPEG关键结构体——AVCodecContext
- Multithreading and high concurrency (8) -- summarize AQS shared lock from countdownlatch (punch in for the third anniversary)
- Single source shortest path exercise (I)
- Determinant learning notes (I)
猜你喜欢

MySQL存储引擎

STM32 configuration after chip replacement and possible errors

Upgrade openssl-1.1.1p for openssl-1.0.2k

认识提取与显示梅尔谱图的小实验(观察不同y_axis和x_axis的区别)

18. (ArcGIS API for JS) ArcGIS API for JS point collection (sketchviewmodel)

关于slmgr命令的那些事
![Choose to pay tribute to the spirit behind continuous struggle -- Dialogue will values [Issue 4]](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
Choose to pay tribute to the spirit behind continuous struggle -- Dialogue will values [Issue 4]

Notepad + + regular expression replace String

Leetcode:20220213 week race (less bugs, top 10% 555)

FPGA内部硬件结构与代码的关系
随机推荐
AtCoder Beginner Contest 258【比赛记录】
Yunna | what are the main operating processes of the fixed assets management system
Browser local storage
18. (ArcGIS API for JS) ArcGIS API for JS point collection (sketchviewmodel)
N1 # if you work on a metauniverse product [metauniverse · interdisciplinary] Season 2 S2
[designmode] composite mode
Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
[noi simulation] Anaid's tree (Mobius inversion, exponential generating function, Ehrlich sieve, virtual tree)
Doppler effect (Doppler shift)
FFT learning notes (I think it is detailed)
2022.7.5-----leetcode. seven hundred and twenty-nine
FFMPEG关键结构体——AVCodecContext
Leetcode 450 deleting nodes in a binary search tree
如何解决ecology9.0执行导入流程流程产生的问题
PHP determines whether an array contains the value of another array
7.5 decorator
MySQL global lock and table lock
Mysql - CRUD
7.5 装饰器
Key structure of ffmpeg -- AVCodecContext