mercredi 17 juin 2020

Scala class constructor lot of fields

I have a class with a primary constructor that takes more than 50 fields:

class HBaseEve   (val rowKey: String,
                 ...
                  val customer: String,
                 ...
                  val managedEntityKey: String,
                 ...
                  val withdrawalReasonForWithdrawal: String)

In my companion object, I declared a method called parse to create a HBaseEve object from a record in HBase table:

    object HBaseEve {
final val COLUMN_FAMILY = "cr"
  override def parse(result: Result): HBaseEve  = {
    if (result.getRow != null)
      new HBaseEve   (
        Bytes.toString(result.getRow),
      ...
        Bytes.toString(result.getValue(HBaseEve.COLUMN_FAMILY.getBytes(), "customer".getBytes())),
        ...
Bytes.toString(result.getValue(HBaseEve.COLUMN_FAMILY.getBytes(), "managedEntityKey".getBytes())),
        ...
        Bytes.toString(result.getValue(HBaseEve.COLUMN_FAMILY.getBytes(), "withdrawalReasonForWithdrawal".getBytes()))
      )
    else null
  }

However, this method parse is not very elegant because I have more than 50 fields in my class to fill in manually. I do not have any idea how to replace this method parse by other method more efficient and profissional.

Any idea on this? I searched about scala.Reflection but i do not have any ideia how to use this. Anyway, i am open to any ideias please.

Thank you





Aucun commentaire:

Enregistrer un commentaire