当前位置:网站首页>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包
三、结果验证
大功告成!完美!!!
边栏推荐
- C#中+=的用法
- NameNode (NN) 和SecondaryNameNode (2NN)工作机制
- Ali on three sides: MQ message loss, repetition, backlog problem, how to solve?
- C#控件 ToolStripProgressBar 用法
- Golang - gin - pprof - use and safety
- TensorRT安装及使用教程「建议收藏」
- [CPU Design Practice] Simple Pipeline CPU Design
- go中select语句
- PartImageNet物体部件分割(Semantic Part Segmentation)数据集介绍
- 抓住金三银四的尾巴,解锁程序员面试《刷题神器》
猜你喜欢
Golang - gin - pprof - use and safety
跨境电商小知识之跨境电商物流定义以及方式讲解
硬盘分区,拓展C盘,不重装系统,不重装D盘软件的全教程。
Grab the tail of gold, silver and silver, unlock the programmer interview "Artifact of Brushing Questions"
架构实战营|模块8
中望3D 2023正式发布,设计仿真制造一体化缩短产品开发周期
爱可可AI前沿推介(7.31)
CentOS7 installation MySQL graphic detailed tutorial
EXCEL如何快速拆分合并单元格数据
Architecture Camp | Module 8
随机推荐
计算机复试面试问题(计算机面试常见问题)
centos7安装mysql5.7
Flutter keyboard visibility
Using SQL Server FOR XML and FOR JSON syntax on other RDBMSs with jOOQ
C#中+=的用法
FastAPI 封装一个通用的response
numpy矩阵和向量的保存与加载,以及使用保存的向量进行相似度计算
Detailed explanation of network protocols and related technologies
NameNode故障处理的两种方法
What should I do if selenium is reversed?
The importance of strategic offensive capability is much higher than strategic defensive capability
Flutter键盘可见性
SAP 电商云 Spartacus SSR Optimization Engine 几处 timeout 的执行顺序
集群中增加数据节点与退役数据节点
阿里三面:MQ 消息丢失、重复、积压问题,怎么解决?
Edge Cloud Explained in Simple Depth | 4. Lifecycle Management
C#控件StatusStrip使用
Adding data nodes and decommissioning data nodes in the cluster
Invalid bound statement (not found)出现的原因和解决方法
go使用makefile脚本编译应用