[英]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.