当前位置:网站首页>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 |
+---+
}
边栏推荐
- 如何解决ecology9.0执行导入流程流程产生的问题
- Problems and solutions of converting date into specified string in date class
- Codeforces Round #804 (Div. 2)【比赛记录】
- Pointer pointer array, array pointer
- Arduino六足机器人
- Idea远程提交spark任务到yarn集群
- DEJA_VU3D - Cesium功能集 之 055-国内外各厂商地图服务地址汇总说明
- Global and Chinese market of water heater expansion tank 2022-2028: Research Report on technology, participants, trends, market size and share
- 时间戳的拓展及应用实例
- 建立时间和保持时间的模型分析
猜你喜欢

FFT learning notes (I think it is detailed)

Problems and solutions of converting date into specified string in date class
![[binary search tree] add, delete, modify and query function code implementation](/img/38/810a83575c56f17a7a0ed428a2e02e.png)
[binary search tree] add, delete, modify and query function code implementation

权限问题:source .bash_profile permission denied

硬件及接口学习总结

关于slmgr命令的那些事

Opencv classic 100 questions

There is no network after configuring the agent by capturing packets with Fiddler mobile phones

MySql——CRUD

Location based mobile terminal network video exploration app system documents + foreign language translation and original text + guidance records (8 weeks) + PPT + review + project source code
随机推荐
FFMPEG关键结构体——AVFrame
Power Query数据格式的转换、拆分合并提取、删除重复项、删除错误、转置与反转、透视和逆透视
7.5 decorator
如何制作自己的机器人
anconda下载+添加清华+tensorflow 安装+No module named ‘tensorflow‘+KernelRestarter: restart failed,内核重启失败
Spark-SQL UDF函数
Global and Chinese markets for pressure and temperature sensors 2022-2028: Research Report on technology, participants, trends, market size and share
Spark获取DataFrame中列的方式--col,$,column,apply
Idea远程提交spark任务到yarn集群
【DesignMode】适配器模式(adapter pattern)
Codeforces round 804 (Div. 2) [competition record]
Single source shortest path exercise (I)
[Online gadgets] a collection of online gadgets that will be used in the development process
An understanding of & array names
Spark SQL空值Null,NaN判断和处理
About the slmgr command
Wechat applet -- wxml template syntax (with notes)
QT -- thread
如何解决ecology9.0执行导入流程流程产生的问题
Solve the problem of reading Chinese garbled code in sqlserver connection database