sparkSQL中的example学习(2)
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)的更多相关文章
- sparkSQL中的example学习(1)
SparkSQLDemo.scala import org.apache.spark.sql.{Row, SparkSession} import org.apache.spark.sql.types ...
- sparkSQL中的example学习(3)
UserDefinedTypedAggregation.scala(用户可自定义类型) import org.apache.spark.sql.expressions.Aggregator impor ...
- PHP中的Libevent学习
wangbin@2012,1,3 目录 Libevent在php中的应用学习 1. Libevent介绍 2. 为什么要学习libevent 3. Php libeven ...
- JS中childNodes深入学习
原文:JS中childNodes深入学习 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <ti ...
- CNCC2017中的深度学习与跨媒体智能
CNCC2017中的深度学习与跨媒体智能 转载请注明作者:梦里茶 目录 机器学习与跨媒体智能 传统方法与深度学习 图像分割 小数据集下的深度学习 语音前沿技术 生成模型 基于贝叶斯的视觉信息编解码 珠 ...
- 【Spark篇】---SparkSQL中自定义UDF和UDAF,开窗函数的应用
一.前述 SparkSQL中的UDF相当于是1进1出,UDAF相当于是多进一出,类似于聚合函数. 开窗函数一般分组取topn时常用. 二.UDF和UDAF函数 1.UDF函数 java代码: Spar ...
- 图解BERT(NLP中的迁移学习)
目录 一.例子:句子分类 二.模型架构 模型的输入 模型的输出 三.与卷积网络并行 四.嵌入表示的新时代 回顾一下词嵌入 ELMo: 语境的重要性 五.ULM-FiT:搞懂NLP中的迁移学习 六.Tr ...
- python中confIgparser模块学习
python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...
- Scala中的类学习
Scala中的类学习 从java了解类的情况下,了解Scala的类并不难.Scala类中的字段自动带getter和setter方法,用@BeanProperty注解生成javaBean对象的getXX ...
随机推荐
- [PHP] 配置vscode的语法检测消除提示Cannot validate since no PHP executable is set
默认下载完vscode什么都不做,会提示一下信息Cannot validate since no PHP executable is set. Use the setting 'php.validat ...
- PHP转Go系列:字符串
字符串的赋值 在PHP中,字符串的赋值虽然只有一行,其实包含了两步,一是声明变量,二是赋值给变量,同一个变量可以任意重新赋值. $str = 'Hello World!'; $str = 'hia'; ...
- CSP认证201812
201812-1 #include<bits/stdc++.h> using namespace std; #define inf 0x3f3f3f3f #define ll long l ...
- MASK-RCNN(1)
MASK-RCNN是一个多用途的网络,可以用来做目标检测,实例分割或者人体姿态识别.主要结构如下. 简单的说,就是首先用Faster-RCNN获得ROI,再进行ROI Align,然后输出ROI的分类 ...
- jquery设置下拉框selected浏览器兼容方式
今天开发过程中偶然发现一个浏览器兼容性问题 当在某些浏览器下面时使用下面的语法会导致值虽然选中了,但是文本没有切换 var options = $("#select").find( ...
- Ubuntu 图形界面和终端切换
场景 在使用Ubuntu时,不小心按下了 ctrl+alt+f3,突然进入终端,好慌 解决 Ubuntu保留了纯命令行模式,按下 ctrl+alt+f2-6 可以进入纯命令行界面 之后按下 ctrl+ ...
- 14.Java基础_函数/函数重载/参数传递
Java函数和函数重载 /* 函数定义: public static 返回类型 func(参数){ 方法体: } 函数重载 在调用时,Java虚拟机会通过参数的不同来区分同名的函数 满足: 1.多个函 ...
- Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)
链接 签到题,求出位数,然后9*(位数-1)+ 从位数相同的全一开始加看能加几次的个数 #include<bits/stdc++.h> using namespace std; int m ...
- window.location.href方式提交json数据
${ctx}/vehicleFlow/to_vehflow_detail.do?strJson="+encodeURIComponent(json)
- Linux学习笔记-第13天 最近有点跟不上节奏阿
难度上来了.最近工作也忙起来了..有点跟不上节奏.加油吧