Spark Properties

Spark properties能够控制大部分的程序设置,并且可以为每个程序分开配置。properties能够通过SparkConf直接设置然后传入到SparkContext。SparkConf允许用户配置一些公用属性(例如:master URL和程序名称),也允许使用set()方法,传入key-value键值对来设置。例如,我们初始化一个拥有两个线程的应用程序如下:
注意:我们设置local[2],意味着两个线程。这代表着最小的并行度,能够帮助我们发现程序在分布式集群运行时可能出现的bug
?
1
2
3
4

val conf = new SparkConf()
.setMaster(“local[2]”)
.setAppName(“CountingSheep”)
val sc = new SparkContext(conf)

注意:我们能够在本地模式下设置多于一个线程,并且在某些Spark Streaming情形下,我们可能必须设置多于1个线程来防止“饥饿问题“(Spark Streaming当中,程序需要一个线程用于Receiver接收数据。如果仅仅设置一个线程,则会被Receiver占用接收数据,而处理数据则没有多余的线程进行)

Properties能够指定一个单元时间区间,格式如下:
?
1
2
3
4
5
6

25ms (milliseconds)
5s (seconds)
10m or 10min (minutes)
3h (hours)
5d (days)
1y (years)

Properties指定单元数据大小,格式如下:
?
1
2
3
4
5
6

1b (bytes)
1k or 1kb (kibibytes = 1024 bytes)
1m or 1mb (mebibytes = 1024 kibibytes)
1g or 1gb (gibibytes = 1024 mebibytes)
1t or 1tb (tebibytes = 1024 gibibytes)
1p or 1pb (pebibytes = 1024 tebibytes)
动态加载Spark Properties

在某些场景下,我们可能不想把程序配置写死到SparkConf当中去。举个栗子,如果你想要运行同一个应用程序在不同的master主机或者使用不同数量的内存。Spark允许用户简单的创建一个空的conf

val sc = new SparkContext(new SparkConf())

接下来,你就可以在运行时提供配置参数:

./bin/spark-submit –name “My app” –master local[4] –conf spark.eventLog.enabled=false
–conf “spark.executor.extraJavaOptions=-XX:+PrintGCDetails -XX:+PrintGCTimeStamps” myApp.jar

Spark shell和spark-submit支持两种方式来动态加载配置configuration。第一种,命令行参数模式,例如上面展示的–master。spark-submit还能够通过–conf flag来接收任意的Spark property,but uses special flags for properties that play a part in launching the Spark application.(不好翻译,我的理解:–conf设置的属性名是Spark程序启动的一部分,不能胡乱设置)。运行./bin/spark-submit --help查看整个可设置的属性列表。

bin/spark-submit 会读取conf/spark-defaults.conf 文件中的配置。spark-defaults.conf中每一行都是由一个key和value构成,由空格分割,(spark-defaults.conf)例如:
?
1
2
3
4

spark.master spark://5.6.7.8:7077
spark.executor.memory 4g
spark.eventLog.enabled true
spark.serializer org.apache.spark.serializer.KryoSerializer

命令行中设置设置,或者通过配置文件设置的属性都将会被直接在SparkConf指定的属性覆盖和合并(如果SparkConf中没有设置则合并)。在SparkConf中直接设置的属性具有最高优先级,然后是spark-shell或spark-submit命令行设置,最后是spark-defaults.conf文件中的配置。有部分属性名已经在新的spark版本中更名。在某些情况下,老的属性名还能够使用,但是会比新的属性名优先级低。
查看Spark Properties

Spark应用程序的web 管理页面http://:4040 下的“Environment”页面列出了所有的properties。这会非常有用,当你需要验证确保你的properties是否设置正确的时候。注意:这些属性值都是通过spark-defaults.conf, SparkConf, 或者命令行来明确指定的,对于其他的一些配置属性值,可以认为都是使用的默认值。
可用属性Available Properties

大部分的properties都已经内置有默认值,一些常用的属性选项如下:
Application Properties
Property Name Default Meaning
spark.app.name (none) 应用程序名称,将会在UI和日志数据中显示
spark.driver.cores 1 driver进程使用的core数量,仅在集群模式下有用
spark.driver.maxResultSize 1g 限制每个Spark action操作(例如collect)时,所有分区,序列化后的结果集总大小。最小设置为1M,或者设置为0(表示无限制)。如果运行中结果集总大小大于了该设置值,job将会中止。而如果将该限值设置的太高,可能会在driver发生内存溢出错误(取决于spark.driver.memory和JVM内存开销)。所以需要设置一个合适的上限值,避免driver端内存溢出
spark.driver.memory 1g 分配给driver进程使用的内存容量,也就是初始化SparkContext的进程。注意:在客户端模式,这个值一定不能通过程序中SparkConf来设置。因为启动SparkConf的时候,driver的JVM已经启动。相反,请使用命令行–driver-memory 来设置,或者通过默认的配置文件。
spark.executor.memory 1g 分配给executor使用的内存容量
spark.extraListeners (none) 一个逗号分隔的SparkListener列表;当初始化SparkContext时,会实例化这些类并注册到Spark的监听组里。会优先调用接收SparkConf一个参数的构造器。如果没有,会调用一个不带参数的构造器。如果没有可以的构造器,该SparkContext会抛出一个异常并创建失败
spark.local.dir /tmp 用来存放Spark运行“痕迹”的目录,包括map结果输出文件和RDDs在磁盘上的存储。这个路径应该是一个读写很快的本地磁盘。也能够使用逗号分隔的list来指定不同磁盘上的多个目录。注意:在spark1.0及以后的版本,被更名为SPARK_LOCAL_DIRS(Standalone,Mesos)或者LOCAL_DIRS(YARN)
spark.logConf false 当SparkContext启动,日志记录SparkConf的所有INFO信息
spark.master (none) 设置要连接到的集群管理
spark.submit.deployMode (none) Spark driver程序的部署模式,“client”或者“cluster”,也就是指定运行driver程序是本地(“client”)还是在集群(“cluster”)中的一个节点上

除了这些,下面的一些属性(properties)在某些情形下也会使用到:
运行时环境Runtime Environment
Property Name Default Meaning
spark.driver.extraClassPath (none) 添加额外的类路径到driver。注意:在client模式,这个配置不能通过SparkConf直接设置,因为SparkContext实例化的时候JVM已经启动。请在命令行当中使用 –driver-class-path或者在默认配置文件中设置
spark.driver.extraJavaOptions (none) 传递给driver的一串设置JVM额外属性的字符串。例如,GC设置或者日志记录。注意一:不能通过这个属性来设置最大的堆大小(-Xmx)。堆的最大值需要通过spark.driver.memory(集群cluster模式)或者在客户端模式(client),命令行中使用–driver-memory设置。注意二:在client模式下,这个配置也不能够通过SparkConf来设置,也是由于初始化SparkConf时,JVM已经启动。使用只能在命令行中通过设置 –driver-java-options或者在默认配置文件中配置
spark.driver.extraLibraryPath (none) 指定运行driver JVM时需要的额外library路径。注意:在client模式,也不能通过SparkConf设置,请在命令行中调用–driver-library-path设置或者默认配置文件中设置。
spark.driver.userClassPathFirst false (实验) 加载类到drive时,是否优先使用用户的jar包覆盖Spark自身的jar包。这个特性主旨用来解决Spark包依赖和用户包依赖之间的冲突问题。在当前版本(spark-2.0.1),这个特性仍然还在试验阶段,仅在cluster集群有效。
spark.executor.extraClassPath (none) 为executors指定需要的额外classpath。这个属性仍然存在主要是为了兼容以前的spark版本。不建议用户设置这个属性,通常也不需要设置该属性。
spark.executor.extraJavaOptions (none) 为executors设置额外的JVM属性。如GC设置或其他日志记录。设置这个属性可以通过SparkConf,spark-submit命令,或者spark-defaults.con文件。**注意:不能通过该参数设置最大的堆大小(-Xmx),最大堆可以通过spark.executor.memory进行设置。
spark.executor.extraLibraryPath (none) 指定executor JVM的额外library路径。
spark.executor.logs.rolling.maxRetainedFiles (none) 设置日志文件保留数量,超过将会删除最旧的日志。该属性默认未启用。
spark.executor.logs.rolling.maxSize (none) 设置executors日志文件每次滚动的最大字节数。Rolling默认禁用。更多请见spark.executor.logs.rolling.maxRetainedFiles自动清理旧日志。
spark.executor.logs.rolling.strategy (none) 设置executors日志的滚动策略。默认禁用。能够被设置为“time”(基于时间滚动)或者“size”(基于大小滚动)。对于“time”,通过spark.executor.logs.rolling.time.interval来设置时间间隔。对于“size”,通过设置spark.executor.logs.rolling.maxSize来滚动。
spark.executor.logs.rolling.time.interval daily 设置executors日志的时间滚动间隔。默认禁用滚动。可用值:daily,hourly,minutely或者seconds。见spark.executor.logs.rolling.maxRetainedFiles来自动清理旧日志。
spark.executor.userClassPathFirst false (实验)和spark.driver.userClassPathFirst一样的功能,但是是应用在executors上。
spark.executorEnv.[EnvironmentVariableName] (none) 通过EnvironmentVariableName添加指定的环境变量到executor进程。用户可以指定多个EnvironmentVariableName,设置多个环境变量
spark.python.profile false 启用Python worker分析结果,通过sc.show_profiles()函数展示executor分析信息,或者在driver退出的时候显示。还能够通过sc.dump_profiles(path)来备份到磁盘上。如果之前已经手动展示,则退出时不会再展示。默认使用pyspark.profiler.BasicProfiler,但是能够使用profiler类作为参数构造SparkContext来进行重载。
spark.python.profile.dump (none) 在driver退出时,分析结果的备份目录。将会为每个RDD结果构建一个被分割的文件。能够通过ptats.Stats()来加载这些数据。如果这个属性被设置,分析结果则不会自动展示。
spark.python.worker.memory 512m 数据聚合期间,每个pyhthon worker的内存使用大小。格式和JVM设置格式一样(512m,2g)。如果聚合期间数据大于设定值,则会将数据溢出到磁盘。
spark.python.worker.reuse true 是否重用python worker。如果是,它将使用固定数量的Python workers,而不需要为每个任务fork()一个Python进程。如果有一个非常大的广播,这个设置将非常有用。因为,广播不需要为每个任务从JVM到Python worker传递一次。
spark.files 一串逗号分隔的文件列表,指定每个executor的防止文件的工作目录。
spark.submit.pyFiles .zip, .egg, 或者 .py 文件的逗号分隔列串,python apps的PYTHONPATH路径。
spark.jars drive和executor的本地jar包的classpaths。逗号分隔。
spark.jars.packages driver和executor的maven jars包的路径(逗号分隔)。这将会先去本地maven repo查找,然后maven远程仓库和spark.jars.ivy中给的远程仓库。格式为groupId:artifactId:version。
spark.jars.excludes 逗号分隔的一串groupId:artifactId,排除在解决spark.jars.packages依赖时带来的冲突。
spark.jars.ivy 逗号分隔的一串远程仓库,用于查询spark.jars.packages中给出的包。
shuffle相关配置(Shuffle Behavior)
Property Name Default Meaning
spark.reducer.maxSizeInFlight 48m 同时从每个task拿过来map结果的大小。每个map输出,都要求创建一个buffer去接收,这是一个固定开销。所以保持这个值尽量小些,除非你有大量的内存。
spark.reducer.maxReqsInFlight Int.MaxValue 这个配置限制每次从远程节点上同时请求的块的数量。当集群中机器越来越多,会导致同时连接多台机器时连接数过大造成某个节点加载失败。通过设置该属性,可以减轻这种状况。
spark.shuffle.compress true 是否对map结果进行压缩。一般来讲,压缩会是更好的选择。指定压缩方式使用spark.io.compression.codec。
spark.shuffle.file.buffer 32k 每个shuffle文件输出流内存内缓存的大小,单位是kb。这个缓存减少了创建中间shuffle文件中磁盘搜索和系统访问的数量。
spark.shuffle.io.maxRetries 3 (Netty only[Netty一个网络通信框架]) 由于IO原因造成的数据拉取失败会自动重试最多该属性值的次数。在大量的shuffle操作,长时间GC暂停或者网络不稳定情况下,该属性值都非常有用。
spark.shuffle.io.numConnectionsPerPeer 1 (Netty only)在主机之间可重用的连接数,用于减少在大型集群当中构建connection的性能损耗。对于主机比较少,硬盘很多的集群,这个值可能会造成读取所有磁盘时并发不足,如此就要考虑将这个值设置大些。
spark.shuffle.io.preferDirectBufs true (Netty only) 在整个shuffle和传输缓存块过程中,启用堆外缓存来减少垃圾收集。但是堆外缓存一般被严格限制,用户可能会选择关掉堆外缓存来强制Netty所有的分配都使用堆内存。
spark.shuffle.io.retryWait 5s (Netty only)拉取数据失败时,重连等待时间。默认最大延迟15s,用maxRetries * retryWait计算。
spark.shuffle.service.enabled false 启用外部shuffle服务。这个服务会保存shuffle文件到外部存储系统,以致excutors能够安全的移除。启用这个服务必须先设置spark.dynamicAllocation.enabled为ture。
spark.shuffle.service.port 7337 外部shuffle服务运行端口
spark.shuffle.sort.bypassMergeThreshold 200 (Advanced) 在基于排序的shuffle管理器中,避免合并排序中的数据,如果在map端没有进行聚合累加。并且能够最大限度的减少分区。
spark.shuffle.spill.compress true 是否压缩shuffle过程中溢出的数据。压缩将使用spark.io.compression.codec.
压缩和序列化
Property Name Default Meaning
spark.broadcast.compress true 广播变量(broadcast)传输前是否进行压缩(一般情况下压缩后传输更好)
spark.io.compression.codec lz4 数据(RDD分区,广播变量,shuffle结果等)传输时采用的压缩方式,Spark提供三种默认压缩方式:lz4, lzf, and snappy。也可以通过指定类的全名来指定压缩方式,比如: org.apache.spark.io.LZ4CompressionCodec, org.apache.spark.io.LZFCompressionCodec, and org.apache.spark.io.SnappyCompressionCodec.
spark.io.compression.lz4.blockSize 32k 采用LZ4压缩方式时,LZ4使用的块大小。一般来讲,这个值小一些,那么shuffle时使用的内存也会相应小一些
spark.io.compression.snappy.blockSize 32k 采用snappy压缩方式时,使用的块大小。一般来讲,这个值减小,shuffle使用内存也会减小
spark.kryo.classesToRegister (none) 如果使用Kryo方式序列化,可以将自己需要序列化的类名用逗号分隔,通过该参数向Kryo登记
spark.kryo.referenceTracking true (使用Spark SQL Thrift服务时该属性为false)当使用Kryo序列化时,是否要追踪相同对象的路径 。如果你的对象图当中有循环,那么该属性将非常有用。如果确定没有,也可以禁用该属性来提升运行速度。
spark.kryo.registrationRequired false 是否要求向Kryo登记。如果设置为‘true’,Kryo遇到没有登记的类序列化的时候将会抛出一个异常。如果设置为‘false’(默认),Kryo将会为每个未登记的类单独写,写类会有一个显著的性能消耗。该属性严格的要求用户不能有类省略注册。
spark.kryo.registrator (none) 如果使用Kryo序列器,可以通过该属性传递一串逗号分隔的自定义注册器类。当你需要使用自定义的方法来register你的类时,该方法就非常有用。否则未设置,spark.kryo.classesToRegister将会实现一个简单的注册器。
spark.kryoserializer.buffer.max 64m Kryo序列化允许的最大缓存。这个值一定要比你要序列化的任一对象大。如果出现”buffer limit exceeded”异常,试试增加该值。
spark.kryoserializer.buffer 64k Kryo序列化缓存的初始长度。注意:每个worker的每个核都会有一个该缓存。如果需要,这个缓存(buffer)会扩展到spark.kryoserializer.buffer.max大小
spark.rdd.compress false 是否压缩序列化后的RRD分区(例如 Java和Scala当中设置的StorageLevel.MEMORY_ONLY_SER或者Python中设置的StorageLevel.MEMORY_ONLY)。典型的时间换空间,消耗大量CPU时间来节约大量空间。
spark.serializer org.apache.spark.serializer.JavaSerializer (org.apache.spark.serializer.KryoSerializer when using Spark SQL Thrift Server) 序列化方式。默认采用Java serializationClass来序列化,但是相当慢。建议采用org.apache.spark.serializer.KryoSerializer并配置Kryo来提升速度,更多可以查看子类org.apache.spark.Serializer.
spark.serializer.objectStreamReset 100 当使用org.apache.spark.serializer.JavaSerializer,serializer 会缓存对象,并阻止GC对这些对象进行收集。通过调用‘reset’,可以手动清空该缓存。设置为-1关掉这个周期属性。默认缓存100个对象。
内存管理
Property Name Default Meaning
spark.memory.fraction 0.6 用来计算和存储的内存百分比。这个值越小,越容易发生泄露和缓存数据被置出。这个属性的目的是,预留出一部分内存保存内部系统元数据,用户数据结构等。推荐使用默认值,更多知识,例如增大该值怎么调整JVM GC等,请参考这里.
spark.memory.storageFraction 0.5 Amount of storage memory immune to eviction, expressed as a fraction of the size of the region set aside by s?park.memory.fraction. The higher this is, the less working memory may be available to execution and tasks may spill to disk more often. Leaving this at the default value is recommended. For more detail, see this description.

spark.memory.offHeap.enabled false If true, Spark will attempt to use off-heap memory for certain operations. If off-heap memory use is enabled, then spark.memory.offHeap.size must be positive.
spark.memory.offHeap.size 0 The absolute amount of memory in bytes which can be used for off-heap allocation. This setting has no impact on heap memory usage, so if your executors’ total memory consumption must fit within some hard limit then be sure to shrink your JVM heap size accordingly. This must be set to a positive value when spark.memory.offHeap.enabled=true.
spark.memory.useLegacyMode false ?Whether to enable the legacy memory management mode used in Spark 1.5 and before. The legacy mode rigidly partitions the heap space into fixed-size regions, potentially leading to excessive spilling if the application was not tuned. The following deprecated memory fraction configurations are not read unless this is enabled: spark.shuffle.memoryFraction
spark.storage.memoryFraction
spark.storage.unrollFraction
spark.shuffle.memoryFraction 0.2 (deprecated) This is read only if spark.memory.useLegacyMode is enabled. Fraction of Java heap to use for aggregation and cogroups during shuffles. At any given time, the collective size of all in-memory maps used for shuffles is bounded by this limit, beyond which the contents will begin to spill to disk. If spills are often, consider increasing this value at the expense of spark.storage.memoryFraction.
spark.storage.memoryFraction 0.6 (deprecated) This is read only if spark.memory.useLegacyMode is enabled. Fraction of Java heap to use for Spark’s memory cache. This should not be larger than the “old” generation of objects in the JVM, which by default is given 0.6 of the heap, but you can increase it if you configure your own old generation size.
spark.storage.unrollFraction 0.2 (deprecated) This is read only if spark.memory.useLegacyMode is enabled. Fraction of spark.storage.memoryFraction to use for unrolling blocks in memory. This is dynamically allocated by dropping existing blocks when there is not enough free storage space to unroll the new block in its entirety.

/apps/app/spark-1.6.1-bin-hadoop2.6/bin/spark-submit

–class com.zdhy.zoc2.sparksql.core.JavaSparkSqlLogRegularApp

–files /apps/app/apache-hive-1.2.1-bin/conf/hive-site.xml

–driver-class-path /apps/app/apache-hive-1.2.1-bin/lib/mysql-connector-java-5.1.21.jar:/apps/app/spark-1.6.1-bin-hadoop2.6/lib/c3p0-0.9.1.2.jar:/apps/app/spark-1.6.1-bin-hadoop2.6/lib/commons-dbutils-1.3.jar

–master spark://master:7077 /root/sparkapps/zoc2-0.0.1-SNAPSHOT.jar /zoc2_test/syslog1 event “SELECT * FROM event” /zoc2/parquetfile/

–jars /apps/app/spark-1.6.1-bin-hadoop2.6/lib/c3p0-0.9.1.2.jar,/apps/app/spark-1.6.1-bin-hadoop2.6/lib/commons-dbutils-1.3.jar

1.submit提交命令;

2.对应jar文件的程序入口类;

3.配置文件路径,一般为hive的配置文件

4.driver所依赖的包,多个包之间用冒号(:)分割

​​​​5.master:spark集群提交路径、jar程序路径、jar程序参数(参数有空格用“”引起来)

6.​driver和executor都需要的包,多个包之间用逗号(,)分割

附spark-submit的完整命令:​​

~/spark$ bin/spark-submit Usage: spark-submit [options] [app arguments] Usage: spark-submit --kill [submission ID] --master [spark://…] Usage: spark-submit --status [submission ID] --master [spark://…] 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.

–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. 1M, 2G) (Default: 512M).

–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. 1M, 2G) (Default: 1G).

–proxy-user NAME User to impersonate when submitting the application. --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.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值