-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUdafSingle.java
59 lines (47 loc) · 1.75 KB
/
UdafSingle.java
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
package sql;
import org.apache.spark.sql.Encoder;
import org.apache.spark.sql.Encoders;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.expressions.Aggregator;
import org.apache.spark.sql.functions;
import scala.Tuple2;
public class UdafSingle {
public static void main(String[] args) {
SparkSession spark = SparkSession
.builder()
.appName("test")
.master("local")
.getOrCreate();
// 需先通过sql.Write生成数据
spark.read().json("output").createOrReplaceTempView("order");
spark.udf().register("a", functions.udaf(new UdafAggregator(), Encoders.LONG()));
spark.sql("select goodId, a(count) as avg from order group by goodId").show();
spark.close();
}
public static class UdafAggregator extends Aggregator<Long, Tuple2<Long, Long>, Double> {
@Override
public Tuple2<Long, Long> zero() {
return new Tuple2<>(0L, 0L);
}
@Override
public Tuple2<Long, Long> reduce(Tuple2<Long, Long> b, Long a) {
return new Tuple2<>(b._1() + a, b._2() + 1);
}
@Override
public Tuple2<Long, Long> merge(Tuple2<Long, Long> b1, Tuple2<Long, Long> b2) {
return new Tuple2<>(b1._1() + b2._1(), b1._2() + b2._2());
}
@Override
public Double finish(Tuple2<Long, Long> reduction) {
return 1.0 * reduction._1() / reduction._2();
}
@Override
public Encoder<Tuple2<Long, Long>> bufferEncoder() {
return Encoders.tuple(Encoders.LONG(), Encoders.LONG());
}
@Override
public Encoder<Double> outputEncoder() {
return Encoders.DOUBLE();
}
}
}