Spark SQL实战之UDF与UDAF的使用

1.概念:

UDF就是用户自定义的函数
UDAF就是用户自定义的聚合函数

2.代码:

(1)pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>2.1.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-sql -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.11</artifactId>
<version>2.1.0</version>
</dependency>
(2)SparkSQLUDFUDAF.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package spark.sqlshizhan
import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import org.apache.spark.sql.{ Row, SQLContext }
import org.apache.spark.sql.types.StructType
import org.apache.spark.sql.types.StructField
import org.apache.spark.sql.types.StringType
import org.apache.spark.sql.expressions.UserDefinedAggregateFunction
import org.apache.spark.sql.expressions.MutableAggregationBuffer
import org.apache.spark.sql.types.DataType
import org.apache.spark.sql.types.IntegerType
/**
* @ClassName SparkSQLUDFUDAF
* @MethodDesc: SparkSQL UDF与UDAF的使用
* @Author Movle
* @Date 5/18/20 10:44 下午
* @Version 1.0
* @Email movle_xjk@foxmail.com
**/
object SparkSQLUDFUDAF {

def main(args: Array[String]): Unit = {

val conf = new SparkConf().setMaster("local").setAppName("SparkSQLUDFUDAF")

val sc = new SparkContext(conf)

val sqlContext = new SQLContext(sc)

val bigData = Array("Spark", "Spark", "Hadoop", "spark", "Hadoop", "spark", "Hadoop", "Hadoop", "spark", "spark")

//创建Dataframe
val bigDataRDD = sc.parallelize(bigData)

val bigDataRDDRow = bigDataRDD.map(item => Row(item))

val structType = StructType(Array(
new StructField("word", StringType)))

val bigDataDF = sqlContext.createDataFrame(bigDataRDDRow, structType)

bigDataDF.createOrReplaceTempView("bigDataTable")

//UDF 最多22个输入参数
sqlContext.udf.register("computeLength",(input:String,input2:String) => input.length())

sqlContext.sql("select word,computeLength(word,word) from bigDataTable").show()

sqlContext.udf.register("wordcount", new MyUDAF)

sqlContext.sql("select word,wordcount(word) as count from bigDataTable group by word").show()


sc.stop()

}
}

class MyUDAF extends UserDefinedAggregateFunction{

/**
* 该方法指定具体输入数据的类型
* @return
*/
override def inputSchema: StructType = StructType(Array(StructField("input", StringType, true)))

/**
* 在进行聚合操作的时候所要处理的数据的结果的类型
* @return
*/
override def bufferSchema: StructType = StructType(Array(StructField("count", IntegerType, true)))

/**
* 指定UDAF函数计算后返回的结果类型
* @return
*/
override def dataType: DataType = IntegerType

/**
* 确保一致性,一般都用true
* @return
*/
override def deterministic: Boolean = true

/**
* 在Aggregate之前每组数据的初始化结果
* @param buffer
*/
override def initialize(buffer: MutableAggregationBuffer): Unit = { buffer(0) = 0 }

/**
* 在进行聚合的时候,每当有新的值进来,对分组后的聚合如何进行计算
* 本地的聚合操作,相当于Hadoop MapReduce模型中的Combiner
* @param buffer
* @param input
*/
override def update(buffer: MutableAggregationBuffer, input: Row): Unit = {
buffer(0) = buffer.getAs[Int](0) + 1
}

/**
* 最后在分布式节点进行Local Reduce完成后需要进行全局级别的Merge操作
* @param buffer1
* @param buffer2
*/
override def merge(buffer1: MutableAggregationBuffer, buffer2: Row): Unit = {
buffer1(0) = buffer1.getAs[Int](0) + buffer2.getAs[Int](0)
}

/**
* 返回UDAF最后的计算结果
* @param buffer
* @return
*/
override def evaluate(buffer: Row): Any = buffer.getAs[Int](0)
}
打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2015-2021 Movle
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信