Scala Reflection在此spark模式结构中使用嵌入式外部库实例化类问题

时间:2018-03-16 05:09:46

标签: scala reflection spark-dataframe

我看到很多关于scala反射库的网站,但是没有一个网站在运行时实例化类的对象有一个简单的答案。 例如,我有以下代码:

trait HydraTargetTable {
 val inputTables = Seq.empty[Int]
 val tableType: String
 val tableName: String
 val schema: StructType
 def className: String = this.getClass.toString
}

trait HydraIntermediateTable extends HydraTargetTable {
  val tableType = "Intermediate"

  def build(textRDD: RDD[String]): DataFrame = {
    DataframeUtils.safeParseFromSchema(textRDD, schema)
  }
}
class Table1 extends HydraIntermediateTable {
  override val inputTables: Seq[Int] = Seq(1, 2)
  override val tableName: String = ""
 override val schema: StructType = new StructType()
}

在运行时,我希望能够在给定类名作为String值的情况下实例化Table1的对象。这是我的反思代码。

object ReflectionTestApp {

  def tableBuilder(name: String): Intermediate = {
    Class.forName("hearsay.hydra.dataflow.api." + name).newInstance()
     .asInstanceOf[Intermediate]
  }

  def hydraTableBuilder(name: String): HydraTargetTable = {
    val action = Class
     .forName("hearsay.hydra.dataflow.api." + name).newInstance()
    action.asInstanceOf[HydraTargetTable]
  }

  def main(args: Array[String]): Unit = {
   hydraTableBuilder("Table1").inputTables.foreach(println)
  }

 }

1 个答案:

答案 0 :(得分:0)

以下是如何实现Object / Class的反射。

package reflection

trait Table {
  val id: Int
}

class ActivityTable extends Table {
  val id = 10
}

object ActivityTable2 extends Table {
  val id = 10
}

object Reflection extends App {

  val obj = activityTableBuilder("ActivityTable")
  println(obj.id) //output 10

  val obj2 = objectBuilder("ActivityTable2$")
  println(obj2.id) //output 10

  /*
  class reflection
   */
  def activityTableBuilder(name: String): Table = {
    val action = Class.forName("reflection." + name).newInstance()
    action.asInstanceOf[Table]
  }

  /*
  object reflection
   */
  def objectBuilder(name: String): Table = {
    val action = Class.forName("reflection." + name)
    action.getField("MODULE$").get(classOf[Table]).asInstanceOf[Table]
  }
}