当前位置:网站首页>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包
三、结果验证

大功告成!完美!!!
边栏推荐
- 基于去噪自编码器的故障隔离与识别方法
- Indoor real-time laser SLAM control method based on biological excitation neural network
- IDEA如何运行web程序
- Character Functions and String Functions
- Fully Dynamically Constrained Robot Efficient Time-Optimal Trajectory Planning
- AMBA APB学习记录(AMBA 3/4)
- C#控件 ToolStripProgressBar 用法
- SAP e-commerce cloud Spartacus SSR Optimization Engine execution sequence of several timeouts
- 关于我放弃考研这件事儿
- golang-gin-优雅重启
猜你喜欢
![[CPU Design Practice] Simple Pipeline CPU Design](/img/83/e1dfedfe2b2cfe83a34f86e252caa7.jpg)
[CPU Design Practice] Simple Pipeline CPU Design

Introduction to using NPM

一文吃透哈希表

架构实战营|模块8

PyQt5快速开发与实战 9.7 UI层的自动化测试

The 2nd activity of the TOGAF10 Standard Reading Club continues wonderfully, and the highlights will be reviewed!

365天挑战LeetCode1000题——Day 044 最大层内元素和 层次遍历

2022年最新重庆建筑安全员模拟题库及答案

深入浅出边缘云 | 4. 生命周期管理

Edge Cloud Explained in Simple Depth | 4. Lifecycle Management
随机推荐
golang八股文整理(持续搬运)
Flutter keyboard visibility
基于模糊预测与扩展卡尔曼滤波的野值剔除方法
中望3D 2023正式发布,设计仿真制造一体化缩短产品开发周期
Invalid bound statement (not found)出现的原因和解决方法
sqlalchemy determines whether a field of type array has at least one consistent data with an array
C# List用法 List介绍
报错IDEA Terminated with exit code 1
0x80070570 The file or directory is damaged and cannot be deleted (how to delete 0x80070091)
AMBA APB学习记录(AMBA 3/4)
Talk about the message display mechanism on the SAP product UI
vivado里那些看不懂的原语
AMBA APB学习记录(AMBA 2.0)
分布式监视 Zabbix 和 Prometheus 到底怎么选?千万别用错了!
电商rpa是什么意思?跟电商rpi是一个意思吗?
Centos7 install mysql5.7
架构实战营|模块8
FastAPI 封装一个通用的response
CentOS7 installation MySQL graphic detailed tutorial
基本语法(二)