当前位置:网站首页>Spark Learning: Add Custom Optimization Rules for Spark Sql
Spark Learning: Add Custom Optimization Rules for Spark Sql
2022-07-31 13:16:00 【I love night Laixiang A】
一、Custom optimization rules
Spark在2.2Version introduces a powerful feature,Add hooks and extension points,Allows users to customize optimization rules
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("The rules of Genghis Khan")
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包
三、结果验证

大功告成!完美!!!
边栏推荐
猜你喜欢

操作符详解

Centos7 install mysql5.7

Edge Cloud Explained in Simple Depth | 4. Lifecycle Management

MATLAB | 我也做了一套绘图配色可视化模板

EXCEL如何快速拆分合并单元格数据
![[CPU Design Practice] Simple Pipeline CPU Design](/img/83/e1dfedfe2b2cfe83a34f86e252caa7.jpg)
[CPU Design Practice] Simple Pipeline CPU Design
尚硅谷–MySQL–基础篇(P1~P95)

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

Batch大小不一定是2的n次幂!ML资深学者最新结论

ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
随机推荐
攻防演练丨赛宁红方管控平台走进广东三地 助力数字政府网络安全建设
战略进攻能力的重要性,要远远高于战略防守能力
The cluster of safe mode
五种数据提交方式的优化
操作符详解
架构实战营|模块8
ICML2022 | 面向自监督图表示学习的全粒度自语义传播
C# using NumericUpDown control
硬盘分区,拓展C盘,不重装系统,不重装D盘软件的全教程。
sqlalchemy 判断一个array 类型的字段是否和一个array有至少一个一致的数据
基本语法(二)
系统集成项目管理工程师(软考中级)知识点总结【挣值分析】【关键路径】
ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
Detailed explanation of network protocols and related technologies
IDEA连接MySQL数据库并执行SQL查询操作
Selenium IDE for Selenium Automation Testing
深入浅出边缘云 | 4. 生命周期管理
六石编程学:不论是哪个功能,你觉得再没用,会用的人都离不了,所以至少要做到99%
The importance of strategic offensive capability is much higher than strategic defensive capability
SAP e-commerce cloud Spartacus SSR Optimization Engine execution sequence of several timeouts