当前位置:网站首页>Spark SQL UDF function
Spark SQL UDF function
2022-07-06 00:28:00 【The south wind knows what I mean】
UDF function
def main(args: Array[String]): Unit = {
val session: SparkSession = SparkSession
.builder()
.config("dfs.ha.namenodes.nameservices","xxxxx")
.appName("sql demo")
.master("local")
.enableHiveSupport()
.getOrCreate()
// Custom function
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)
// usage 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 |
+-------------+----+
// usage 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 |
+---+
}
边栏推荐
- Idea远程提交spark任务到yarn集群
- STM32按键消抖——入门状态机思维
- NLP basic task word segmentation third party Library: ICTCLAS [the third party library with the highest accuracy of Chinese word segmentation] [Chinese Academy of Sciences] [charge]
- XML配置文件
- OS i/o devices and device controllers
- Global and Chinese markets for pressure and temperature sensors 2022-2028: Research Report on technology, participants, trends, market size and share
- uniapp开发,打包成H5部署到服务器
- 【线上小工具】开发过程中会用到的线上小工具合集
- Introduction of motor
- Codeforces Round #804 (Div. 2)【比赛记录】
猜你喜欢
Multithreading and high concurrency (8) -- summarize AQS shared lock from countdownlatch (punch in for the third anniversary)
Go learning - dependency injection
Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
如何利用Flutter框架开发运行小程序
Detailed explanation of APP functions of door-to-door appointment service
如何解决ecology9.0执行导入流程流程产生的问题
Classic CTF topic about FTP protocol
Arduino六足机器人
Calculate sha256 value of data or file based on crypto++
Date类中日期转成指定字符串出现的问题及解决方法
随机推荐
Go learning - dependency injection
Global and Chinese markets of universal milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
Priority queue (heap)
Huawei equipment is configured with OSPF and BFD linkage
[Chongqing Guangdong education] Chongqing Engineering Vocational and Technical College
The global and Chinese markets of dial indicator calipers 2022-2028: Research Report on technology, participants, trends, market size and share
LeetCode 斐波那契序列
如何解决ecology9.0执行导入流程流程产生的问题
Global and Chinese markets for pressure and temperature sensors 2022-2028: Research Report on technology, participants, trends, market size and share
uniapp开发,打包成H5部署到服务器
Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
2022.7.5-----leetcode. seven hundred and twenty-nine
LeetCode 8. String conversion integer (ATOI)
Atcoder beginer contest 258 [competition record]
Basic introduction and source code analysis of webrtc threads
如何制作自己的机器人
Set data real-time update during MDK debug
Hudi of data Lake (2): Hudi compilation
Global and Chinese market of water heater expansion tank 2022-2028: Research Report on technology, participants, trends, market size and share
PHP determines whether an array contains the value of another array