当前位置:网站首页>Spark学习:为Spark Sql添加自定义优化规则
Spark学习:为Spark Sql添加自定义优化规则
2022-07-31 13:03:00 【我爱夜来香A】
一、自定义优化规则
Spark在2.2版本引入了一个强大的特性,添加钩子和拓展点,允许用户自定义优化规则
1、实现自定义规则 (静默规则,通过 set spark.sql.planChangeLog.level=WARN,确认执行到就行)
case class MyPushDown(spark: SparkSession) extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan transform {
.... }
}
2、创建自己的 Extension 并注入
class MySparkSessionExtension extends (SparkSessionExtensions => Unit) {
override def apply(extensions: SparkSessionExtensions): Unit = {
extensions.injectOptimizerRule {
session =>
new MyPushDown(session)
}
}
}
3、通过 spark.sql.extensions 提交
bin/spark-sql --jars my.jar --conf MySparkSessionExtension
二、代码实现
1、pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Spark</groupId>
<artifactId>Spark</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.12</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.12</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
<executions>
<execution>
<id>compile-scala</id>
<phase>compile</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile-scala</id>
<phase>test-compile</phase>
<goals>
<goal>add-source</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<scalaVersion>2.12.15</scalaVersion>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass></mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<includeProjectDependencies>true</includeProjectDependencies>
<includePluginDependencies>false</includePluginDependencies>
<classpathScope>compile</classpathScope>
<mainClass>cn.spark.study.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、class
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
import org.apache.spark.sql.catalyst.rules.Rule
case class MyRule(spark: SparkSession) extends Rule[LogicalPlan] {
logWarning("成吉思汗的规则")
override def apply(plan: LogicalPlan): LogicalPlan = plan
}
import org.apache.spark.sql.SparkSessionExtensions
class MySparkSessionExtension extends (SparkSessionExtensions => Unit) {
override def apply(extensions: SparkSessionExtensions): Unit = {
extensions.injectOptimizerRule {
session => MyRule(session)
}
}
}
3、使用maven打成jar包
三、结果验证

大功告成!完美!!!
边栏推荐
- 五种数据提交方式的优化
- 亲测可用!!!WPF中遍历整个窗口的所有TextBox组件,对每个输入框做非空判断。
- sqlalchemy determines whether a field of type array has at least one consistent data with an array
- log4j2的使用
- FIFO深度计算学习记录(汇总)
- centos7安装mysql5.7
- EXCEL如何快速拆分合并单元格数据
- ADS与C#通信
- 深入浅出边缘云 | 4. 生命周期管理
- 365-day challenge LeetCode1000 questions - Day 044 Maximum element in the layer and level traversal
猜你喜欢

IDEA版Postman插件Restful Fast Request,细节到位,功能好用

centos7安装mysql5.7步骤(图解版)

PyQt5 rapid development and actual combat 10.1 Get city weather forecast

Ali on three sides: MQ message loss, repetition, backlog problem, how to solve?

【牛客刷题-SQL大厂面试真题】NO3.电商场景(某东商城)

Character Functions and String Functions

ECCV2022:在Transformer上进行递归,不增参数,计算量还少!

AMBA APB学习记录(AMBA 3/4)

Anaconda安装labelImg图像标注软件

一文吃透哈希表
随机推荐
抓住金三银四的尾巴,解锁程序员面试《刷题神器》
尚硅谷–MySQL–基础篇(P1~P95)
Optimization of five data submission methods
聊聊 SAP 产品 UI 上的消息显示机制
golang八股文整理(持续搬运)
LRU缓存[线性表 -> 链表 -> hash定位 -> 双向链表]
golang-gin-优雅重启
Centos7 install mysql5.7
基本语法(一)
电脑重要文件很多,如何备份比较安全?
log4j2的使用
基本语法(二)
PyQt5快速开发与实战10.2 复利计算 && 10.3 刷新博客点击量
【CPU设计实战】简单流水线CPU设计
报错IDEA Terminated with exit code 1
TensorRT安装及使用教程「建议收藏」
The cluster of safe mode
EXCEL如何快速拆分合并单元格数据
深入浅出边缘云 | 4. 生命周期管理
How does the SAP ABAP OData service support the $filter (filter) operation trial version