我有資料結構如下所示的資料集
case class AddressData(
addressId: String,
customerId: String,
address: String,
number: Option[Int],
road: Option[String],
city: Option[String],
country: Option[String]
)
case class CustomerDocument(
customerId: String,
forename: String,
surname: String,
address: Seq[AddressData]
)
架構
root
|-- customerId: string (nullable = true)
|-- forename: string (nullable = true)
|-- surname: string (nullable = true)
|-- accounts: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- customerId: string (nullable = true)
| | |-- accountId: string (nullable = true)
| | |-- balance: long (nullable = true)
|-- address: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- addressId: string (nullable = true)
| | |-- customerId: string (nullable = true)
| | |-- address: string (nullable = true)
| | |-- number: integer (nullable = true)
| | |-- road: string (nullable = true)
| | |-- city: string (nullable = true)
| | |-- country: string (nullable = true)
樣本資料:
| 客戶ID | 名字 | 姓 | 地址 |
|---|---|---|---|
| IND0222 | 查爾斯 | Piper | [[ADR285,IND0222,424, Lexington Avenue, New York, United States of America]] |
I am required to filter for a country ( highlighted item in bold, for eg. like Canada) from address list and create a new column and set the value to 'True' if the country is available or 'False' in case it is not available.
I am not sure how to apply filter condition inside the array of struct to achieve. Some form of guidance is appreciated. Thanks
uj5u.com熱心網友回復:
下面的代碼對我有用,可以從結構陣列中提取國家欄位。
val countryFlag = df.withcolumn("isPresent", array_contains($"address.country", "Canada"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/450231.html
