使用 spark-shell 时使用 sparkConf.set(..) 自定义 SparkContext [英] Customize SparkContext using sparkConf.set(..) when using spark-shell

查看:126
本文介绍了使用 spark-shell 时使用 sparkConf.set(..) 自定义 SparkContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Spark 中,有 3 种主要方法可以为用于创建 SparkContextSparkConf 指定选项:

In Spark, there are 3 primary ways to specify the options for the SparkConf used to create the SparkContext:

  1. 作为 conf/spark-defaults.conf 中的属性
    • 例如:spark.driver.memory 4g
  • 例如,spark-shell --driver-memory 4g ...
  • 例如,sparkConf.set( "spark.driver.memory", "4g" )

但是,当使用 spark-shell 时,SparkContext 已经在您收到 shell 提示时为您创建,位于名为 sc 的变量中.使用 spark-shell 时,如果在您有机会执行任何 Scala 语句之前已经创建了 SparkContext,您如何使用上面列表中的选项 #3 来设置配置选项?

However, when using spark-shell, the SparkContext is already created for you by the time you get a shell prompt, in the variable named sc. When using spark-shell, how do you use option #3 in the list above to set configuration options, if the SparkContext is already created before you have a chance to execute any Scala statements?

特别是,我正在尝试使用 Kyro 序列化和 GraphX.将 Kryo 与 GraphX 结合使用的规定方法是在自定义 SparkConf 实例时执行以下 Scala 语句:

In particular, I am trying to use Kyro serialization and GraphX. The prescribed way to use Kryo with GraphX is to execute the following Scala statement when customizing the SparkConf instance:

GraphXUtils.registerKryoClasses( sparkConf )

在运行 spark-shell 时如何完成此操作?

How do I accomplish this when running spark-shell?

推荐答案

Spark 2.0+

您应该能够使用 SparkSession.conf.set 方法在运行时设置一些配置选项,但它主要限于 SQL 配置.

You should be able to use SparkSession.conf.set method to set some configuration option on runtime but it is mostly limited to SQL configuration.

火花<2.0

您可以简单地停止现有上下文并创建一个新上下文:

You can simply stop an existing context and create a new one:

import org.apache.spark.{SparkContext, SparkConf}

sc.stop()
val conf = new SparkConf().set("spark.executor.memory", "4g")
val sc = new SparkContext(conf)

正如您在官方文档中所读到的那样:

一旦 SparkConf 对象被传递给 Spark,它就会被克隆,并且不能再被用户修改.Spark 不支持在运行时修改配置.

Once a SparkConf object is passed to Spark, it is cloned and can no longer be modified by the user. Spark does not support modifying the configuration at runtime.

因此,正如您所看到的,停止上下文是在 shell 启动后唯一适用的选项.

So as you can see stopping the context it is the only applicable option once shell has been started.

您始终可以使用配置文件或 --conf 参数给 spark-shell 设置将用作默认上下文的必需参数.如果是 Kryo,你应该看看:

You can always use configuration files or --conf argument to spark-shell to set required parameters which will be used be the default context. In case of Kryo you should take a look at:

  • spark.kryo.classesToRegister
  • spark.kryo.registrator

参见压缩和序列化Spark 配置.

这篇关于使用 spark-shell 时使用 sparkConf.set(..) 自定义 SparkContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆