|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.spark.examples.sql; |
| 18 | + |
| 19 | +// $example on:untyped_custom_aggregation$ |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.apache.spark.sql.Dataset; |
| 24 | +import org.apache.spark.sql.Row; |
| 25 | +import org.apache.spark.sql.SparkSession; |
| 26 | +import org.apache.spark.sql.expressions.MutableAggregationBuffer; |
| 27 | +import org.apache.spark.sql.expressions.UserDefinedAggregateFunction; |
| 28 | +import org.apache.spark.sql.types.DataType; |
| 29 | +import org.apache.spark.sql.types.DataTypes; |
| 30 | +import org.apache.spark.sql.types.StructField; |
| 31 | +import org.apache.spark.sql.types.StructType; |
| 32 | +// $example off:untyped_custom_aggregation$ |
| 33 | + |
| 34 | +public class JavaUserDefinedUntypedAggregation { |
| 35 | + |
| 36 | + // $example on:untyped_custom_aggregation$ |
| 37 | + public static class MyAverage extends UserDefinedAggregateFunction { |
| 38 | + |
| 39 | + private StructType inputSchema; |
| 40 | + private StructType bufferSchema; |
| 41 | + |
| 42 | + public MyAverage() { |
| 43 | + List<StructField> inputFields = new ArrayList<>(); |
| 44 | + inputFields.add(DataTypes.createStructField("inputColumn", DataTypes.LongType, true)); |
| 45 | + inputSchema = DataTypes.createStructType(inputFields); |
| 46 | + |
| 47 | + List<StructField> bufferFields = new ArrayList<>(); |
| 48 | + bufferFields.add(DataTypes.createStructField("sum", DataTypes.LongType, true)); |
| 49 | + bufferFields.add(DataTypes.createStructField("count", DataTypes.LongType, true)); |
| 50 | + bufferSchema = DataTypes.createStructType(bufferFields); |
| 51 | + } |
| 52 | + // Data types of input arguments of this aggregate function |
| 53 | + public StructType inputSchema() { |
| 54 | + return inputSchema; |
| 55 | + } |
| 56 | + // Data types of values in the aggregation buffer |
| 57 | + public StructType bufferSchema() { |
| 58 | + return bufferSchema; |
| 59 | + } |
| 60 | + // The data type of the returned value |
| 61 | + public DataType dataType() { |
| 62 | + return DataTypes.DoubleType; |
| 63 | + } |
| 64 | + // Whether this function always returns the same output on the identical input |
| 65 | + public boolean deterministic() { |
| 66 | + return true; |
| 67 | + } |
| 68 | + // Initializes the given aggregation buffer. The buffer itself is a `Row` that in addition to |
| 69 | + // standard methods like retrieving a value at an index (e.g., get(), getBoolean()), provides |
| 70 | + // the opportunity to update its values. Note that arrays and maps inside the buffer are still |
| 71 | + // immutable. |
| 72 | + public void initialize(MutableAggregationBuffer buffer) { |
| 73 | + buffer.update(0, 0L); |
| 74 | + buffer.update(1, 0L); |
| 75 | + } |
| 76 | + // Updates the given aggregation buffer `buffer` with new input data from `input` |
| 77 | + public void update(MutableAggregationBuffer buffer, Row input) { |
| 78 | + if (!input.isNullAt(0)) { |
| 79 | + long updatedSum = buffer.getLong(0) + input.getLong(0); |
| 80 | + long updatedCount = buffer.getLong(1) + 1; |
| 81 | + buffer.update(0, updatedSum); |
| 82 | + buffer.update(1, updatedCount); |
| 83 | + } |
| 84 | + } |
| 85 | + // Merges two aggregation buffers and stores the updated buffer values back to `buffer1` |
| 86 | + public void merge(MutableAggregationBuffer buffer1, Row buffer2) { |
| 87 | + long mergedSum = buffer1.getLong(0) + buffer2.getLong(0); |
| 88 | + long mergedCount = buffer1.getLong(1) + buffer2.getLong(1); |
| 89 | + buffer1.update(0, mergedSum); |
| 90 | + buffer1.update(1, mergedCount); |
| 91 | + } |
| 92 | + // Calculates the final result |
| 93 | + public Double evaluate(Row buffer) { |
| 94 | + return ((double) buffer.getLong(0)) / buffer.getLong(1); |
| 95 | + } |
| 96 | + } |
| 97 | + // $example off:untyped_custom_aggregation$ |
| 98 | + |
| 99 | + public static void main(String[] args) { |
| 100 | + SparkSession spark = SparkSession |
| 101 | + .builder() |
| 102 | + .appName("Java Spark SQL user-defined DataFrames aggregation example") |
| 103 | + .getOrCreate(); |
| 104 | + |
| 105 | + // $example on:untyped_custom_aggregation$ |
| 106 | + // Register the function to access it |
| 107 | + spark.udf().register("myAverage", new MyAverage()); |
| 108 | + |
| 109 | + Dataset<Row> df = spark.read().json("examples/src/main/resources/employees.json"); |
| 110 | + df.createOrReplaceTempView("employees"); |
| 111 | + df.show(); |
| 112 | + // +-------+------+ |
| 113 | + // | name|salary| |
| 114 | + // +-------+------+ |
| 115 | + // |Michael| 3000| |
| 116 | + // | Andy| 4500| |
| 117 | + // | Justin| 3500| |
| 118 | + // | Berta| 4000| |
| 119 | + // +-------+------+ |
| 120 | + |
| 121 | + Dataset<Row> result = spark.sql("SELECT myAverage(salary) as average_salary FROM employees"); |
| 122 | + result.show(); |
| 123 | + // +--------------+ |
| 124 | + // |average_salary| |
| 125 | + // +--------------+ |
| 126 | + // | 3750.0| |
| 127 | + // +--------------+ |
| 128 | + // $example off:untyped_custom_aggregation$ |
| 129 | + |
| 130 | + spark.stop(); |
| 131 | + } |
| 132 | +} |
0 commit comments