spark.jars.packages使用镜像源加速
我们知道,通过指定spark.jars.packages参数,可以添加依赖的包。而且相比于用spark.jars直接指定jar文件路径,前者还可以自动下载所需依赖,在有网络的情况下非常方便。
然而默认情况下spark会到maven的中央仓库进行下载,速度非常慢。我们可以使用国内的镜像源进行加速。以华为云镜像为例。
1. 指定ivy2配置文件路径
spark是通过ivy2进行包下载的,而其配置文件默认打包在了$SPARK_HOME/jars/ivy-2.4.0.jar
里,因此无法修改。我们通过spark配置来修改其配置文件路径。在$SPARK_HOME/conf/spark-defaults.conf
中添加:
spark.jars.ivySettings /home/mvpboss1004/.ivy2/ivysettings.xml
# add a package
spark.jars.packages org.apache.spark:spark-streaming-kafka-0-10-assembly_2.12:3.0.0
2. ivy2配置文件中添加镜像源
修改之前指定的ivysettings.xml文件,如不存在则新建。增加:
<ivysettings>
<settings defaultResolver="chain"/>
<resolvers>
<chain name="chain">
<ibiblio name="huaweicloud" root="https://mirrors.huaweicloud.com/repository/maven" m2compatible="true"/>
<ibiblio name="central" m2compatible="true"/>
</chain>
</resolvers>
</ivysettings>
这里的华为云也可以换成阿里云、阿里云等镜像源。
3. 测试
运行pyspark。如下所示,最后一行"found *** in huaweicloud"可以确认下载源已经换成了华为云。
Python 3.6.8 (default, Apr 2 2020, 13:34:55)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
:: loading settings :: file = /home/mvpboss1004/.ivy2/ivysettings.xml
Ivy Default Cache set to: /home/mvpboss1004/.ivy2/cache
The jars for the packages stored in: /home/mvpboss1004/.ivy2/jars
:: loading settings :: url = jar:file:/usr/share/spark-3.0.0-bin-hadoop2.7/jars/ivy-2.4.0.jar!/org/apache/ivy/core/settings/ivysettings.xml
org.apache.spark#spark-streaming-kafka-0-10-assembly_2.12 added as a dependency
:: resolving dependencies :: org.apache.spark#spark-submit-parent-d8756840-be94-4d96-8c99-a4b319f7b26c;1.0
confs: [default]
found org.apache.spark#spark-streaming-kafka-0-10-assembly_2.12;3.0.0 in huaweicloud