当前位置:网站首页>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 |
+---+
}
边栏推荐
- [designmode] adapter pattern
- Search (DFS and BFS)
- 如何利用Flutter框架开发运行小程序
- Go learning --- structure to map[string]interface{}
- FFmpeg学习——核心模块
- Date类中日期转成指定字符串出现的问题及解决方法
- Multithreading and high concurrency (8) -- summarize AQS shared lock from countdownlatch (punch in for the third anniversary)
- LeetCode 斐波那契序列
- DEJA_VU3D - Cesium功能集 之 055-国内外各厂商地图服务地址汇总说明
- Codeforces Round #804 (Div. 2)【比赛记录】
猜你喜欢
Permission problem: source bash_ profile permission denied
Notepad++ regular expression replacement string
[designmode] Decorator Pattern
数据分析思维分析方法和业务知识——分析方法(三)
LeetCode 1189. Maximum number of "balloons"
Introduction of motor
Browser local storage
Configuring OSPF load sharing for Huawei devices
Model analysis of establishment time and holding time
Huawei equipment configuration ospf-bgp linkage
随机推荐
FFMPEG关键结构体——AVCodecContext
Intranet Security Learning (V) -- domain horizontal: SPN & RDP & Cobalt strike
Natural language processing (NLP) - third party Library (Toolkit):allenlp [library for building various NLP models; based on pytorch]
[designmode] Decorator Pattern
MDK debug时设置数据实时更新
【DesignMode】组合模式(composite mode)
anconda下载+添加清华+tensorflow 安装+No module named ‘tensorflow‘+KernelRestarter: restart failed,内核重启失败
从底层结构开始学习FPGA----FIFO IP核及其关键参数介绍
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]
Key structure of ffmpeg - avframe
[noi simulation] Anaid's tree (Mobius inversion, exponential generating function, Ehrlich sieve, virtual tree)
MySql——CRUD
Huawei equipment is configured with OSPF and BFD linkage
如何利用Flutter框架开发运行小程序
AtCoder Beginner Contest 258【比赛记录】
Codeforces round 804 (Div. 2) [competition record]
Opencv classic 100 questions
N1 # if you work on a metauniverse product [metauniverse · interdisciplinary] Season 2 S2
Yolov5, pychar, Anaconda environment installation
Global and Chinese markets of universal milling machines 2022-2028: Research Report on technology, participants, trends, market size and share