mercredi 20 janvier 2021

Get class by name and call method

I am trying to build an ability where I can fetch a StrucType schema object using name.

For that,I have defined a trait:

package schema

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

trait ISchema {

  def schema(): StructType

}

And I am implementing this as follows:

package schema

import org.apache.spark.sql.types.{BooleanType, DateType, DecimalType, IntegerType, StringType, StructType}

object StaticPortfolio extends ISchema {

  override def schema(): StructType = new StructType()
    .add("account_number", StringType)
    .add("loan_amount", DecimalType(38,6))
    .add("disburse_date", DateType)
    .add("start_date", DateType)
    .add("disbursed_amount", DecimalType(38,6))
    .add("capitalized_interest", DecimalType(38,6))
    .add("principal_outstanding_amount", DecimalType(38,6))
    .add("principal_due", DecimalType(38,6))
    .add("principal_paid", DecimalType(38,6))
    .add("principal_not_paid", DecimalType(38,6))
    .add("interest_due", DecimalType(38,6))
    .add("interest_paid", DecimalType(38,6))
    .add("interest_outstanding_amount", DecimalType(38,6))
    .add("late_fee", DecimalType(38,6))
    .add("late_fee_amount_paid", DecimalType(38,6))
    .add("bounce_fee", DecimalType(38,6))
    .add("bounce_fee_amount_paid", DecimalType(38,6))
    .add("dpd_bucket", IntegerType)
    .add("dpd", IntegerType)
    .add("is_due_today", BooleanType)
    .add("status", StringType)
    .add("annual_rate_of_interest", DecimalType(38,6))
    .add("year", StringType)
    .add("month", StringType)
    .add("day", StringType)
}

Now I'm trying to get this StrucType schema object as follows:

val schemaObj = Class.forName("schema.StaticPortfolio")
schemaObj.schema

But I get value schema is not a member of Class[?0]

I also tried:

val schemaObj:ISchema = Class.forName("schema.StaticPortfolio")
println(schemaObj.schema)

But I get this error:

type mismatch;
 found   : Class[?0] where type ?0
 required: schema.ISchema
    val schemaObj:ISchema = Class.forName("schema.StaticPortfolio")

Aucun commentaire:

Enregistrer un commentaire