public static void main(String[] args) {
    SparkSession spark = SparkSession
      .builder()
      .appName("Java Spark SQL user-defined DataFrames aggregation example")
      .getOrCreate();

    // $example on:untyped_custom_aggregation$
    // Register the function to access it
    spark.udf().register("myAverage", new MyAverage());

    Dataset<Row> df = spark.read().json("examples/src/main/resources/employees.json");
    df.createOrReplaceTempView("employees");
    df.show();
    // +-------+------+
    // |   name|salary|
    // +-------+------+
    // |Michael|  3000|
    // |   Andy|  4500|
    // | Justin|  3500|
    // |  Berta|  4000|
    // +-------+------+

    Dataset<Row> result = spark.sql("SELECT myAverage(salary) as average_salary FROM employees");
    result.show();
    // +--------------+
    // |average_salary|
    // +--------------+
    // |        3750.0|
    // +--------------+
    // $example off:untyped_custom_aggregation$

    spark.stop();
  }
public static void main ( String [ ] args ) { SparkSession spark = SparkSession . builder ( ) . appName ( "Java Spark SQL user-defined DataFrames aggregation example" ) . getOrCreate ( ) ; spark . udf ( ) . register ( "myAverage" , new MyAverage ( ) ) ; Dataset < Row > df = spark . read ( ) . json ( "examples/src/main/resources/employees.json" ) ; df . createOrReplaceTempView ( "employees" ) ; df . show ( ) ; Dataset < Row > result = spark . sql ( "SELECT myAverage(salary) as average_salary FROM employees" ) ; result . show ( ) ; spark . stop ( ) ; }
$example off:untyped_custom_aggregation$