私は次のケースのクラスがあります。私もはオプションにMapオブジェクトをマッピングする[反復処理可能[行]オプションを導出する[一覧[アドレス]]
case class Address (
val addressLine1: String,
val addressLine2: String,
val city: String,
val provinceCode: String,
val country: String,
val addressTypeDesc: String)
と
case class ClientData(
val title: String,
val firstName: String,
val lastName: String,
val addrList: Option[List[Address]]
)
を
import org.apache.spark.sql.Row
object ClientBuilder {
def build(client: Row, addr: Option[Iterable[Row]], addrType: Map[String, String]): ClientData = {
// The object validates that the field “ClientTitle” is empty or one of the following values only:
// "Mr.", "Ms.", "Mrs." - Otherwise the build will throw an IllegalArgumentException
val title: String =
client.getAs[String]("Title") match {
case "Mr." => "Mr."
case "Ms." => "Ms."
case "Mrs." => "Mrs."
case "" => ""
case _ => throwException("Client Title is not as expected")
}
val firstName: String = client.getAs[String]("FirstName")
val lastName: String = client.getAs[String]("LastName")
val addrList: Option[List[Address]] = // having some problem figuring this part out
ClientData(title, firstName, lastName, addrList)
}
def throwException(exceptionReason: String) = {
throw new IllegalArgumentException(s"Exception thrown due to: ${exceptionReason}")
}
}
addr: Option[Iterable[Row]]
は次の列があります。次のように
AddressID,AddressLine1,AddressLine2,City,ProvinceName,ProvinceCode,Country,ClientID,AddressTypeCode
とaddrType: Map[String, String])
は次のとおりです。
Map(1 -> "Billing", 2 -> "Home", 3 -> "Main Office", 4 -> "Primary", 5 -> "Shipping", 6 -> "Archive")
私はaddr
列AddressTypeCode
とaddrType
キーにaddrType: Map[String, String])
でaddr: Option[Iterable[Row]]
に参加したいと思いますaddressTypeDesc
を取得し、タイプAddress
のリストを作成します。