当前位置:网站首页>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 |
+---+
}
边栏推荐
- OpenCV经典100题
- Data analysis thinking analysis methods and business knowledge - analysis methods (III)
- LeetCode 6006. Take out the least number of magic beans
- How to use the flutter framework to develop and run small programs
- How to solve the problems caused by the import process of ecology9.0
- STM32按键消抖——入门状态机思维
- MDK debug时设置数据实时更新
- Huawei equipment configuration ospf-bgp linkage
- An understanding of & array names
- Notepad++ regular expression replacement string
猜你喜欢
多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)
Analysis of the combination of small program technology advantages and industrial Internet
[designmode] composite mode
STM32 configuration after chip replacement and possible errors
Hudi of data Lake (1): introduction to Hudi
Classical concurrency problem: the dining problem of philosophers
Knowledge about the memory size occupied by the structure
Search (DFS and BFS)
Extracting profile data from profile measurement
MDK debug时设置数据实时更新
随机推荐
Hudi of data Lake (2): Hudi compilation
LeetCode 6004. Get operands of 0
QT -- thread
Pointer - character pointer
LeetCode 1189. Maximum number of "balloons"
常用API类及异常体系
uniapp开发,打包成H5部署到服务器
XML配置文件
Chapter 16 oauth2authorizationrequestredirectwebfilter source code analysis
Wechat applet -- wxml template syntax (with notes)
The relationship between FPGA internal hardware structure and code
Classical concurrency problem: the dining problem of philosophers
【DesignMode】组合模式(composite mode)
提升工作效率工具:SQL批量生成工具思想
Solve the problem of reading Chinese garbled code in sqlserver connection database
Problems and solutions of converting date into specified string in date class
Opencv classic 100 questions
Spark AQE
What is information security? What is included? What is the difference with network security?
建立时间和保持时间的模型分析