Spark SQL用户自定义函数UDF及Apache日志实例

本文介绍了如何在Spark SQL中创建临时UDF,并提供了基于Apache日志的查询实例。通过分析Apache日志,理解日志格式,包括客户端IP、请求方法、状态代码等关键信息,实现日志数据的解析与处理。
摘要由CSDN通过智能技术生成

临时UDF

创建临时UDF,在SQL中使用:
(注:涉及文档上一篇博文《Spark SQL基础笔记及简单案例》给出》

//创建DataFrame
case class Student(id: String, name : String, age: Int)
val rdd=sc.textFile("hdfs://node01:9000/sqldata/students.txt").map(_.split(",")).map(p => Student(p(0), p(1), p(2).trim.toInt))
val students = rdd.toDF()

//定义函数,将表中三个列合并为一个列,使用','分割
def allInOne(seq: Seq[Any], sep: String): String = seq.mkString(sep)

def allInOne1(s1:String, s2:String,s3:Int): String = s1+s2+s3.toString

//注册函数allInOne
sqlContext.udf.register("allInOne", allInOne _)
sqlContext.udf.register("allInOne1", allInOne1 _)
//或者直接使用匿名函数做参数
sqlContext.udf.register( "allInOne", (seq: Seq[Any], sep: String)=>seq.mkString(sep)  )

//使用函数allInOne:在DataFrame的接口中使用
import org.apache.spark.sql.functions.{udf,array,lit}
val myFunc = udf(allInOne _)
val cols = array("id","name","age")
val sep = lit(",")

students.select(myFunc(cols,sep).alias("co
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值