gpt4 book ai didi

scala - 在 Spark 中将 sqlContext 作为隐式参数传递

转载 作者:行者123 更新时间:2023-12-01 11:22:13 25 4
gpt4 key购买 nike

我在具有以下签名的 Scala 对象中有一个函数

def f(v1:Int)(implicit sqlContext: SQLContext)

当我尝试从 spark-shell 调用这个函数时,我这样调用它

f(1)

我希望现有的 sqlContext 被隐式传递给它,但它没有。我怎样才能让它工作,以便 sqlContext 自动传递给这个函数?

------------更新--------------------

在调用我的函数之前,我尝试在 spark-shell 中导入 sqlContext.implicits._ 但它没有帮助

最佳答案

您只需将 SQLContext 隐式添加到您正在调用函数的同一上下文中:

implicit val sqlContext = new SQLContext() // just an example
// and then
f(1)

如果您使用的是 apacha Spark,则可以使用此导入:

import sqlContext.implicits._

关于scala - 在 Spark 中将 sqlContext 作为隐式参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40792543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com