简体   繁体   English

如何将案例 class 作为变量传递给 ScalaReflection

[英]How to pass case class as a variable into ScalaReflection

I am trying to pass a case class as a variable to ScalaReflection to get the schema.我正在尝试将案例 class 作为变量传递给 ScalaReflection 以获取架构。

I am able to run the code successfully with case class name where as when I assign the case class to a variable and pass that to ScalaReflection I am getting error.我能够使用案例 class 名称成功运行代码,而当我将案例 class 分配给变量并将其传递给 ScalaReflection 时,我收到错误。

Here is my code这是我的代码

import org.apache.spark.sql.catalyst.ScalaReflection
import org.apache.spark.sql.types.StructType

case class Emp (empId: Integer, empName: String)
val myschema = ScalaReflection.schemaFor[Emp].dataType.asInstanceOf[StructType]
println(myschema)

val empModel = Emp
val myschema2 = ScalaReflection.schemaFor[empModel].dataType.asInstanceOf[StructType]

Error: error: not found: type empModel
val myschema2 = ScalaReflection.schemaFor[empModel].dataType.asInstanceOf[StructType]

Any suggestion is helpful!任何建议都是有帮助的!

Type synonyms (= aliases) should be introduced with a keyword type类型同义词(=别名)应该用关键字type引入

type empModel = Emp
val myschema2 = ScalaReflection.schemaFor[empModel].dataType.asInstanceOf[StructType]

In Scala values and types make different namespaces.在 Scala 中,值和类型构成不同的命名空间。

Top definitions with type (and val ) are not allowed in Scala 2, so they have to be inside some object Scala 2 中不允许带有type (和val )的顶级定义,因此它们必须位于某些 object 中

object App {
  type empModel = Emp
  val myschema2 = ScalaReflection.schemaFor[empModel].dataType.asInstanceOf[StructType]
}

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

相关问题 scala如何参数化案例类,并将案例类变量传递给[T &lt;:Product:TypeTag] - scala how to parameterized case class, and pass the case class variable to [T <: Product: TypeTag] 将scala(2.8)case类中的可变数量的参数传递给父构造函数 - pass variable number of arguments in scala (2.8) case class to parent constructor 如何将案例类传递给函数参数并相应地获取模式? - How to pass a case class into a function parameter and get a schema accordingly? 将案例类传递给Spark UDF - Pass case class to Spark UDF 如何使用无形中的变量访问案例类字段 - How to access case class fields using a variable in shapeless 如何使用模式匹配案例类中声明的变量? - How to use variable declared in pattern-matching case class? 将正在构造的实例传递给案例类构造函数 - Pass to a case class constructor the instance being constructed Spark 1.3.1 SQL库:scala.reflect.internal.MissingRequirementError:类org.apache.spark.sql.catalyst.ScalaReflection - Spark 1.3.1 SQL libriary: scala.reflect.internal.MissingRequirementError: class org.apache.spark.sql.catalyst.ScalaReflection 如何模拟案例类? - How to mock case class? 将数组作为案例类的单独参数传递 - Pass array as separate arguments for case class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM