简体   繁体   English

scala 未指定值参数

[英]scala Unspecified Value Parameters

I want to extend the SparkSession class in spark.我想在 spark 中扩展 SparkSession 类。 I copied the constructor of the original SparkSession partially reproduced here:我复制了部分复制的原始 SparkSession 的构造函数:

class SparkSession private(
    @transient val sparkContext: SparkContext,
    @transient private val existingSharedState: Option[SharedState],
    @transient private val parentSessionState: Option[SessionState],
    @transient private[sql] val extensions: SparkSessionExtensions)
  extends Serializable with Closeable with Logging { self =>

  private[sql] def this(sc: SparkContext) {
    this(sc, None, None, new SparkSessionExtensions)
  }

  // other implementations

}

Here's my attempt at extending it:这是我扩展它的尝试:

class CustomSparkSession private(
    @transient override val sparkContext: SparkContext,
    @transient private val existingSharedState: Option[SharedState],
    @transient private val parentSessionState: Option[SessionState],
    @transient override private[sql] val extensions: SparkSessionExtensions)
  extends SparkSession {

  // implementation

}

But I get an error on the SparkSession part of extends SparkSession with error:但是我在extends SparkSessionSparkSession部分extends SparkSession错误消息:

Unspecified value parameters: sc: SparkContext未指定的值参数:sc: SparkContext

I know that it's coming from the this constructor in the original SparkContext, but I'm not sure how, or if I can even extend this properly.我知道它来自原始 SparkContext 中的this构造函数,但我不确定如何,或者我是否可以正确扩展它。 Any ideas?有任何想法吗?

When you write class Foo extends Bar you are actually (1) creating a default (no-argument) constructor for class Foo , and (2) calling a default constructor of class Bar .当您编写class Foo extends Bar您实际上是(1)为类Foo创建默认(无参数)构造函数,以及(2)调用类Bar的默认构造函数。

Consequently, if you have something like class Bar(bar: String) , you can't just write class Foo extends Bar , because there is no default constructor to call, you need to pass a parameter for bar .因此,如果您有 class Bar(bar: String)类的东西,您不能只编写class Foo extends Bar ,因为没有要调用的默认构造函数,您需要为bar传递一个参数。 So, you could write something like所以,你可以写一些类似的东西

class Foo(bar: String) extends Bar(bar) 

This is why you are seeing this error - you are trying to call a constructor for SparkSession , but not passing any value for sc .这就是您看到此错误的原因 - 您正在尝试为SparkSession调用构造SparkSession ,但没有为sc传递任何值。

But you have a bigger problem.但是你有一个更大的问题。 That private keyword you see next to SparkSession (and another one before this ) means that the constructor is ... well ... private.您在SparkSession旁边看到的那个private关键字(以及在this之前的另一个)意味着构造函数是……嗯……私有的。 You cannot call it.你不能叫它。 In other words, this class cannot be subclassed (outside the sql package), so you should look for another way to achieve what you are trying to do.换句话说,这个类不能被子类化(在sql包之外),所以你应该寻找另一种方法来实现你想要做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM