在Scala中基于String选择案例类 [英] Select case class based on String in Scala
问题描述
如何基于 String
值选择 case类
?
我的代码是
val spark = SparkSession.builder()...
val rddOfJsonStrings: RDD[String] = // some json strings as RDD
val classSelector: String = ??? // could be "Foo" or "Bar", or any other String value
case class Foo(foo: String)
case class Bar(bar: String)
if (classSelector == "Foo") {
val df: DataFrame = spark.read.json(rddOfJsonStrings)
df.as[Foo]
} else if (classSelector == "Bar") {
val df: DataFrame = spark.read.json(rddOfJsonStrings)
df.as[Bar]
} else {
throw ClassUnknownException //custom Exception
}
变量 classSeletector
是一个简单的String,应用于指向同名的case类.
The variable classSeletector
is a simple String that should be used to point to the case class of the same name.
想象一下,我不仅有 Foo
和 Bar
作为案例类,而且还有更多.如何基于String调用 df.as []
语句(如果可能的话)?
Imagine I don't only have Foo
and Bar
as case classes but more then those two. How is it possible to call the df.as[]
statement based on the String (if possible at all)?
还是Scala中提供了一种完全不同的方法?
Or is there a completely different approach available in Scala?
推荐答案
如何基于String调用df.as []语句(如果可能的话)?
How is it possible to call the df.as[] statement based on the String (if possible at all)?
不是(或基于任何运行时值).您可能会注意到,所有答案仍然需要:
It isn't (or based on any runtime value). You may note that all answers still need to:
-
对
Foo
和Bar
有一个单独的分支(对于要添加的每个类,还有一个分支);
have a separate branch for
Foo
andBar
(and one more branch for each class you'll want to add);
在分支中重复两次类名.
repeat the class name twice in the branch.
您可以避免第二个:
import scala.reflect.{classTag, ClassTag}
val df: DataFrame = spark.read.json(rddOfJsonStrings)
// local function defined where df and classSelector are visible
def dfAsOption[T : Encoder : ClassTag] =
Option.when(classSelector == classTag[T].runtimeClass.simpleName)(df.as[T])
dfAsOption[Foo].dfAsOption(asOption[Bar]).getOrElse(throw ClassUnknownException)
但是首先,如果可能的话,您将需要一个宏.我猜不是.
But for the first you'd need a macro if it's possible at all. I would guess it isn't.
这篇关于在Scala中基于String选择案例类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!