我在 Scala 的 main()
函数中创建了一个 hiveContext
并且我需要将这个 hiveContext
的参数传递给其他函数,这是结构:
object Project {
def main(name: String): Int = {
val hiveContext = new org.apache.spark.sql.hive.HiveContext(sc)
...
}
def read (streamId: Int, hc:hiveContext): Array[Byte] = {
...
}
def close (): Unit = {
...
}
}
但它不起作用。函数 read()
在 main()
中被调用。
有什么想法吗?
最佳答案
我将 hiveContext 声明为隐式的,这对我有用
implicit val sqlContext: HiveContext = new HiveContext(sc)
MyJob.run(conf)
在 MyJob 中定义:
override def run(config: Config)(implicit sqlContext: SQLContext): Unit = ...
但是如果你不希望它隐含,这应该是一样的
val sqlContext: HiveContext = new HiveContext(sc)
MyJob.run(conf)(sqlContext)
override def run(config: Config)(sqlContext: SQLContext): Unit = ...
此外,您的函数读取应该接收 HiveContext 作为参数 hc 的类型,而不是 hiveContext
def read (streamId: Int, hc:HiveContext): Array[Byte] =
关于scala - 如何将 hiveContext 作为参数传递给函数 spark scala,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37565980/