spark spark-sql提交方式及参数优化建议

Spark作为分布式的SQL查询引擎,官方测试结果比 Hive sql 快 100倍。目前spark提交命令有spark-shell、spark-submit、spark-sql三种方式,下面我就来看看spark-sql的提交方式。

与hive执行命令对比:
下面我们用hive提交的方式与spark-sql做一下比较,发现基本是一致的。但是hive底层使用mr执行速度实在不忍直视,当然如果条件允许,可以考虑安装hive on spark。
执行hive命令一般都是:

hive -e 'select * from table1'
或者 
hive -f /home/user1/xx.hql -d dt=20230301

执行spark-sql命令也可以这样执行

spark-sql --master yarn-client -e 'select * from table1'
或者
spark-sql --master yarn-client  dt=20230301 -f '/home/user1/xx.hql'

建议:spark-sql对机器内存性能要求很高,容易执行失败,如果spark-sql执行失败,出现内存溢出的情况,还是使用hive比较稳定。

spark-sql命令使用及参数说明
基础命令使用:

  1. 在spark cli 上运行spark-sql客户端,可以执行以下命令:
    spark-sql --master yarn
  2. spark-sql 运行 sql 文件,可以执行以下命令:
    spark-sql -f sql.txt --master yarn
  3. 在cli上直接运行 sql 命令,可以运行以下命令:
    spark-sql -e “show databases”

提交参数介绍:
spark-sql提交的参数与spark-submit提交的参数基本一致,
参数如下:

[localhosts]$ spark-sql --help
Usage: ./bin/spark-sql [options] [cli option]

Options:
  --master MASTER_URL         spark://host:port, mesos://host:port, yarn, or local.
  --deploy-mode DEPLOY_MODE   Whether to launch the driver program locally ("client") or
                              on one of the worker machines inside the cluster ("cluster")
                              (Default: client).
  --class CLASS_NAME          Your application's main class (for Java / Scala apps).
  --name NAME                 A name of your application.
  --jars JARS                 Comma-separated list of local jars to include on the driver
                              and executor classpaths.
  --packages                  Comma-separated list of maven coordinates of jars to include
                              on the driver and executor classpaths. Will search the local
                              maven repo, then maven central and any additional remote
                              repositories given by --repositories. The format for the
                              coordinates should be groupId:artifactId:version.
  --exclude-packages          Comma-separated list of groupId:artifactId, to exclude while
                              resolving the dependencies provided in --packages to avoid
                              dependency conflicts.
  --repositories              Comma-separated list of additional remote repositories to
                              search for the maven coordinates given with --packages.
  --py-files PY_FILES         Comma-separated list of .zip, .egg, or .py files to place
                              on the PYTHONPATH for Python apps.
  --files FILES               Comma-separated list of files to be placed in the working
                              directory of each executor.

  --conf PROP=VALUE           Arbitrary Spark configuration property.
  --properties-file FILE      Path to a file from which to load extra properties. If not
                              specified, this will look for conf/spark-defaults.conf.

  --driver-memory MEM         Memory for driver (e.g. 1000M, 2G) (Default: 1024M).
  --driver-java-options       Extra Java options to pass to the driver.
  --driver-library-path       Extra library path entries to pass to the driver.
  --driver-class-path         Extra class path entries to pass to the driver. Note that
                              jars added with --jars are automatically included in the
                              classpath.

  --executor-memory MEM       Memory per executor (e.g. 1000M, 2G) (Default: 1G).

  --proxy-user NAME           User to impersonate when submitting the application.
                              This argument does not work with --principal / --keytab.

  --help, -h                  Show this help message and exit
  --verbose, -v               Print additional debug output
  --version,                  Print the version of current Spark

 Spark standalone with cluster deploy mode only:
  --driver-cores NUM          Cores for driver (Default: 1).

 Spark standalone or Mesos with cluster deploy mode only:
  --supervise                 If given, restarts the driver on failure.
  --kill SUBMISSION_ID        If given, kills the driver specified.
  --status SUBMISSION_ID      If given, requests the status of the driver specified.

 Spark standalone and Mesos only:
  --total-executor-cores NUM  Total cores for all executors.

 Spark standalone and YARN only:
  --executor-cores NUM        Number of cores per executor. (Default: 1 in YARN mode,
                              or all available cores on the worker in standalone mode)

 YARN-only:
  --driver-cores NUM          Number of cores used by the driver, only in cluster mode
                              (Default: 1).
  --queue QUEUE_NAME          The YARN queue to submit to (Default: "default").
  --num-executors NUM         Number of executors to launch (Default: 2).
  --archives ARCHIVES         Comma separated list of archives to be extracted into the
                              working directory of each executor.
  --principal PRINCIPAL       Principal to be used to login to KDC, while running on
                              secure HDFS.
  --keytab KEYTAB             The full path to the file that contains the keytab for the
                              principal specified above. This keytab will be copied to
                              the node running the Application Master via the Secure
                              Distributed Cache, for renewing the login tickets and the
                              delegation tokens periodically.

spark-sql cli 类似 hive 提交的命令行参数

CLI options:
 -d,--define <key=value>          Variable subsitution to apply to hive
                                  commands. e.g. -d A=B or --define A=B
    --database <databasename>     Specify the database to use
 -e <quoted-query-string>         SQL from command line
 -f <filename>                    SQL from files
 -H,--help                        Print help information
    --hiveconf <property=value>   Use value for given property
    --hivevar <key=value>         Variable subsitution to apply to hive
                                  commands. e.g. --hivevar A=B
 -i <filename>                    Initialization SQL file
 -S,--silent                      Silent mode in interactive shell
 -v,--verbose                     Verbose mode (echo executed SQL to the console)

spark-sql 参数说明参考:

--num-executors
设置任务executor个数,默认值为4,一般调整此参数需要同时调整并行度(参考4)。任务设置executor个数的依据是业务期望任务运行时间,可以先设置一个较小值,通过调整此参数及并行度直到运行时间达到期望。

--executor-cores
设置单个executor的core数,默认为1,建议不要超过2。任务申请的总core数为executor个数*单个executor的core数,如:申请4个executor,每个executor申请2个core,则此任务申请的总core数为8。另外,建议单个executor申请的core数与内存的比例要保持在1:4,如:单个executor申请2个core,那么内存相应需要申请到8g。

--executor-memory
设置executor内存,默认值为4g。每个executor申请的core数与内存比例要保持在1:4,如:单个executor申请2个core,那么内存相应需要申请8g。另外,单个executor内存溢出,请不要简单地调大executor内存,这样会导致任务平均内存利用率较低,如果是数据倾斜导致的内存溢出,请解决数据倾斜;如果是任务并行度设置较小导致的内存溢出,可以通过调大并行度解决(参考4)。

spark.sql.shuffle.partitions
调整stage的并行度,也就是每个stage的task个数,默认值为40。此参数一般设置为任务申请的总core数的2-4倍,如:申请100个executor,每个executor申请2个core,那么总core数为200,此参数设置的合理范围是400-800。注意,此参数不能调整某些读外部数据stage的并行度,如:读hdfs的stage,绝大多数情况它的并行度取决于需要读取的文件。

spark.shuffle.service.enabled、spark.dynamicAllocation.enabled
以上两个参数同时设置为true可开启动态资源分配,开启后可防止资源浪费情况。Spark-2.1.0默认关闭动态资源分配,Spark-2.3.3默认打开。动态资源分配开启后,当executor闲置超过60s将被回收,但executor不会低于spark.dynamicAllocation.minExecutors配置个数。当任务资源不足时,任务会自动向YARN申请资源,但executor不会超过spark.dynamicAllocation.maxExecutors配置个数。

spark.dynamicAllocation.minExecutors
此参数可以调整动态资源分配任务executor的下限,默认为0。

spark.dynamicAllocation.maxExecutors
此参数可以调整动态资源分配任务executor的上限,默认为--num-executors个数。

spark.sql.autoBroadcastJoinThreshold
在Spark关联中,小表数据量小于等于此参数,可将关联转化为BroadcastHashJoin,类似Hive中的mapjoin,此参数默认值是10M,设置为-1可以禁用BroadcastHashJoin。

spark.sql.broadcastTimeout
用来控制broadcast超时时间,默认值为5min。当广播小表时,如果广播时间超过此参数设置值,会导致任务失败。

spark.sql.join.preferSortMergeJoin
用来控制是否优先使用SortMergeJoin,默认指为true。SortMergeJoin是比较慢的Join实现方式,在shuffle过后,所有relation数据都要sort,然后再关联。此参数设置为false时,spark将优先使用ShuffleHashJoin,ShuffleHashJoin是在shuffle过后,将数据量较小的relation构建为hash表,提高关联速度。

spark.sql.adaptive.enabled(spark-2.3.3)
用来控制是否开启adaptive execution,默认为false。一直以来,Spark只能设置固定的并行度(参考4),在大促期间,数据量激增,每个task处理的数量增加,很容易出现oom的情况。在开启此项参数后,spark将会按照。
spark.sql.ataptive.shuffle.targetPostShuffleInputSize设置的每个task的目标处理数据量自动调整并行度,降低task出现oom的情况。

spark.sql.ataptive.shuffle.targetPostShuffleInputSize(spark-2.3.3)
在开启adaptive execution时,用来控制每个task处理的目标数据量,Spark将会根据此参数值动态调整task个数,默认值为64M。

spark.sql.ataptive.skewedJoin.enabled(spark-2.3.3)
在开启adaptive execution时,用来控制是否开启自动处理join时的数据倾斜,默认为false。

spark.sql.ataptive.skewedPartitionMaxSplits(spark-2.3.3)
在开启adaptive execution时,控制处理一个倾斜 Partition 的 Task 个数上限,默认值为 5。

spark.sql.ataptive.skewedPartitionRowCountThreshold(spark-2.3.3)
在开启adaptive execution时,设置一个 Partition 被视为倾斜 Partition 的行数下限,也即行数低于该值的 Partition 不会被当作倾斜 Partition 处理。其默认值为 10L * 1000 * 1000 即一千万。

spark.sql.ataptive.skewedPartitionSizeThreshold(spark-2.3.3)
在开启adaptive execution时,设置一个 Partition 被视为倾斜 Partition 的大小下限,也即大小小于该值的 Partition 不会被视作倾斜 Partition。其默认值为 64 * 1024 * 1024 也即 64MB。

spark.sql.ataptive.skewedPartitionFactor(spark-2.3.3)
在开启adaptive execution时,设置倾斜因子。如果一个 Partition 的大小大于 spark.sql.adaptive.skewedPartitionSizeThreshold 的同时大于各 Partition 大小中位数与该因子的乘积,或者行数大于 spark.sql.adaptive.skewedPartitionRowCountThreshold 的同时大于各 Partition 行数中位数与该因子的乘积,则它会被视为倾斜的 Partition。默认为10。
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值