首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何更新现有的SparkSession实例或在shell中创建一个新实例?

如何更新现有的SparkSession实例或在shell中创建一个新实例?
EN

Stack Overflow用户
提问于 2018-01-05 13:10:45
回答 1查看 9.9K关注 0票数 4

当我启动spark-shell时,它会创建一个SparkSession实例。然而,我应该按以下方式创建它:

代码语言:javascript
复制
val spark = SparkSession.builder()
                        .config("es.nodes",elasticHost)
                        .config("es.port",elasticPort)
                        .config("es.nodes.wan.only","true")
                        .appName("Test")
                        .getOrCreate()

我如何更新spark-shell中现有的spark-shell,或者如上面所示创建新的?

EN

回答 1

Stack Overflow用户

发布于 2018-01-06 17:50:42

可以使用SparkSession.conf.set设置配置属性,也可以使用SparkSession.newSession创建另一个SparkSession实例,然后设置属性。

set(key: String,value: String):Unit设置给定的火花运行时配置属性。 newSession():SparkSession使用隔离的配置、临时表、注册函数启动新会话,但共享底层SparkContext和缓存数据。

这两种方法的工作原理(几乎)相同,可以暂时将属性设置为新值,并同时使用两个SparkSession

代码语言:javascript
复制
// hello property is not set
scala> spark.conf.getOption("hello")
res1: Option[String] = None

scala> spark.conf.set("hello", "world")

// hello property is set
scala> spark.conf.getOption("hello")
res3: Option[String] = Some(world)

// create a new SparkSession (so you'll have two at the same time)
val ns = spark.newSession

// hello is not set in a new session
scala> ns.conf.getOption("hello")
res4: Option[String] = None

ns.conf.set("hello", "hello in another session")

scala> ns.conf.getOption("hello")
res8: Option[String] = Some(hello in another session)

// the value of hello in the initial SparkSession remains unchanged
scala> spark.conf.getOption("hello")
res9: Option[String] = Some(world)
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48114340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档