当前位置:网站首页>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包
三、结果验证
大功告成!完美!!!
边栏推荐
- CWE4.8 -- 2022年危害最大的25种软件安全问题
- 深入浅出边缘云 | 4. 生命周期管理
- Productivity Tools and Plugins
- ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
- Google Chrome(谷歌浏览器)安装使用
- 网络层重点协议——IP协议
- P5019 [NOIP2018 提高组] 铺设道路
- Two methods of NameNode failure handling
- golang-gin-pprof-使用以及安全问题
- SAP message TK 248 solved
猜你喜欢
LRU缓存[线性表 -> 链表 -> hash定位 -> 双向链表]
Error: npm ERR code EPERM
[Niu Ke brush questions - SQL big factory interview questions] NO3. E-commerce scene (some east mall)
CentOS7 installation MySQL graphic detailed tutorial
PyQt5 rapid development and actual combat 10.2 compound interest calculation && 10.3 refresh blog clicks
NameNode (NN) 和SecondaryNameNode (2NN)工作机制
分布式监视 Zabbix 和 Prometheus 到底怎么选?千万别用错了!
五种数据提交方式的优化
Golang - gin - pprof - use and safety
ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
随机推荐
centos7安装mysql5.7
基于模糊预测与扩展卡尔曼滤波的野值剔除方法
知名无人驾驶公司:文远知行内推
文本相似度计算(中英文)详解实战
生产力工具和插件
Flutter键盘可见性
0X7FFFFFFF,0X80000000「建议收藏」
selenium被反爬了怎么办?
C# 中的Async 和 Await 的用法详解
求一份常见Oracle故障模拟场景
PartImageNet物体部件分割(Semantic Part Segmentation)数据集介绍
Centos7 install mysql5.7
滑窗法切分数据
抓住金三银四的尾巴,解锁程序员面试《刷题神器》
SAP message TK 248 solved
Introduction to the PartImageNet Semantic Part Segmentation dataset
尚硅谷–MySQL–基础篇(P1~P95)
pytorch gpu版本安装最新
Network layer key protocol - IP protocol
NPM 使用介绍