UserDefinedUntypedAggregate.scala(默认返回类型为空,不能更改)


import org.apache.spark.sql.{Row, SparkSession}
import org.apache.spark.sql.expressions.{MutableAggregationBuffer, UserDefinedAggregateFunction}
import org.apache.spark.sql.types._ object UserDefinedUntypedAggregate { // $example on: untyped_custom_aggregations$
object MyAverage extends UserDefinedAggregateFunction { //Data types of input arguments of this aggregate function
def inputSchema: StructType = StructType(StructField("inputColumn", LongType) :: Nil) //Data types of values in the aggregation buffer
def bufferSchema: StructType = {
StructType(StructField("sum", LongType) :: StructField("count", LongType) :: Nil)
} //The data type of the returned value
def dataType: DataType = DoubleType //Whether this function always return s the same output on the identical input
def deterministic: Boolean = true // """
// |Initializes the given aggregation buffer.
// |The buffer itself is a `Row` that in addition to
// |standard method like retrieving a value at an index (e.g., get(), getBoolean()),
// |providesthe opportunity to update its values.
// |Note that arrays andmaps inside the buffer are still ummutable.
// """
def initialize(buffer: MutableAggregationBuffer): Unit = {
buffer(0) = 0L
buffer(1) = 0L } //Updates the given aggregation buffer `buffer` with new input data from `input`
def update(buffer: MutableAggregationBuffer, input: Row): Unit = {
//isNullAt() -> Checks whether the value at position i is null.
if (!input.isNullAt(0)) {
buffer(0) = buffer.getLong(0) + input.getLong(0)
buffer(1) = buffer.getLong(1) + 1
}
} //Merges two aggregation buffers and stores the updated buffer values back to `buffer1`
def merge(buffer1: MutableAggregationBuffer, buffer2: Row): Unit = {
buffer1(1) = buffer1.getLong(0) + buffer2.getLong(0)
buffer1(1) = buffer1.getLong(1) + buffer2.getLong(1)
} // Calcuates the final result
def evaluate(buffer: Row): Double = buffer.getLong(0).toDouble / buffer.getLong(1)
}
// $example off: untyped_custom_aggregation$ def main(args: Array[String]): Unit = {
val spark = SparkSession
.builder()
.master("local")
.appName("Spark SQL user-defined DataFrames aggregation example")
.getOrCreate() // $eeample on: untyped_custom_aggregation$
//Register the function to access it
spark.udf.register("myAverage", MyAverage) val df = spark.read.json("/Users/hadoop/app/spark/examples/src/main/resources/employees.json")
df.createOrReplaceTempView("employees")
df.show() val result = spark.sql("SELECT myAverage(salary) as average_salary FROM employees")
result.show() spark.stop()
}
}

sparkSQL中的example学习(2)的更多相关文章

  1. sparkSQL中的example学习(1)

    SparkSQLDemo.scala import org.apache.spark.sql.{Row, SparkSession} import org.apache.spark.sql.types ...

  2. sparkSQL中的example学习(3)

    UserDefinedTypedAggregation.scala(用户可自定义类型) import org.apache.spark.sql.expressions.Aggregator impor ...

  3. PHP中的Libevent学习

    wangbin@2012,1,3 目录 Libevent在php中的应用学习 1.      Libevent介绍 2.      为什么要学习libevent 3.      Php libeven ...

  4. JS中childNodes深入学习

    原文:JS中childNodes深入学习 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <ti ...

  5. CNCC2017中的深度学习与跨媒体智能

    CNCC2017中的深度学习与跨媒体智能 转载请注明作者:梦里茶 目录 机器学习与跨媒体智能 传统方法与深度学习 图像分割 小数据集下的深度学习 语音前沿技术 生成模型 基于贝叶斯的视觉信息编解码 珠 ...

  6. 【Spark篇】---SparkSQL中自定义UDF和UDAF,开窗函数的应用

    一.前述 SparkSQL中的UDF相当于是1进1出,UDAF相当于是多进一出,类似于聚合函数. 开窗函数一般分组取topn时常用. 二.UDF和UDAF函数 1.UDF函数 java代码: Spar ...

  7. 图解BERT(NLP中的迁移学习)

    目录 一.例子:句子分类 二.模型架构 模型的输入 模型的输出 三.与卷积网络并行 四.嵌入表示的新时代 回顾一下词嵌入 ELMo: 语境的重要性 五.ULM-FiT:搞懂NLP中的迁移学习 六.Tr ...

  8. python中confIgparser模块学习

    python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...

  9. Scala中的类学习

    Scala中的类学习 从java了解类的情况下,了解Scala的类并不难.Scala类中的字段自动带getter和setter方法,用@BeanProperty注解生成javaBean对象的getXX ...

随机推荐

  1. Web服务器—Apache

    Apache配置文件:httpd.conf文件 # 指定Apache的安装路径,此选项参数值在安装Apache时系统会自动把Apache的路径写入. ServerRoot "/www/ser ...

  2. java链接集合

    Intellij IDEA 导入eclipse web 项目详细操作 https://blog.csdn.net/deng11408205/article/details/79723213?utm_s ...

  3. 关于 from scipy.misc import imread, imresize, imsave 报错的问题

    使用 from scipy.misc import imread, imresize, imsave 时出现报错,查找后发现新版本的Scipy不再包含imread,imresize,imsave,需要 ...

  4. 自定义MVC二

    1. 什么是MVC MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写, 它是一种软件设计典范,用一种业务逻辑.数据. ...

  5. web-神盾局的秘密

    题目地址 http://web.jarvisoj.com:32768/ 首页是一张图片 查看源码,看到了一些猫腻,showing.php     c2hpZWxkLmpwZw==是base64编码   ...

  6. Python __slots__

    Python 类的特殊变量:__slots__ 使用 __slots__ 后,类中 __weakref__ 和 __dict__ 消失,同时阻止动态属性绑定 由于 __dict__ 记录着类中所有的属 ...

  7. xLua 学习

    xLua https://github.com/Tencent/xLua 文档 https://tencent.github.io/xLua/public/v1/guide/index.html FA ...

  8. SpringBoot 整合RabbitMQ错误记录

    1. 控制台报错:Exception in thread "main" java.io.IOException…… Caused by: com.rabbitmq.client.S ...

  9. Elastic 使用索引生命周期管理实现热温冷架构

    Elastic: 使用索引生命周期管理实现热温冷架构 索引生命周期管理 (ILM) 是在 Elasticsearch 6.6(公测版)首次引入并在 6.7 版正式推出的一项功能.ILM 是 Elast ...

  10. 2019-9-2-win10-uwp-判断本地ip

    原文:2019-9-2-win10-uwp-判断本地ip title author date CreateTime categories win10 uwp 判断本地ip lindexi 2019-0 ...