Address
имеет тип struct
. Вам нужно извлечь столбцы из Address
столбца, чтобы увидеть другие столбцы.
Проверьте код ниже
scala> df.printSchema
root
|-- Address: struct (nullable = true)
| |-- Address1: struct (nullable = true)
| | |-- Hno: long (nullable = true)
| | |-- pin: long (nullable = true)
| | |-- state: long (nullable = true)
| | |-- street: long (nullable = true)
| |-- Address2: struct (nullable = true)
| | |-- Hno: long (nullable = true)
| | |-- pin: long (nullable = true)
| | |-- state: long (nullable = true)
| | |-- street: long (nullable = true)
| |-- Address3: struct (nullable = true)
| | |-- Hno: long (nullable = true)
| | |-- pin: long (nullable = true)
| | |-- state: long (nullable = true)
| | |-- street: long (nullable = true)
| |-- country: string (nullable = true)
| |-- full_Address: string (nullable = true)
scala> df.select("Address.*").printSchema
root
|-- Address1: struct (nullable = true)
| |-- Hno: long (nullable = true)
| |-- pin: long (nullable = true)
| |-- state: long (nullable = true)
| |-- street: long (nullable = true)
|-- Address2: struct (nullable = true)
| |-- Hno: long (nullable = true)
| |-- pin: long (nullable = true)
| |-- state: long (nullable = true)
| |-- street: long (nullable = true)
|-- Address3: struct (nullable = true)
| |-- Hno: long (nullable = true)
| |-- pin: long (nullable = true)
| |-- state: long (nullable = true)
| |-- street: long (nullable = true)
|-- country: string (nullable = true)
|-- full_Address: string (nullable = true)
scala> df.select("Address.*").show(false)
+-----------------+---------------------+-----------------+-------+------------+
|Address1 |Address2 |Address3 |country|full_Address|
+-----------------+---------------------+-----------------+-------+------------+
|[7, 462036, 1, 5]|[0, 462037, 0, 25578]|[0, 462038, 0, 0]|India |112018 |
+-----------------+---------------------+-----------------+-------+------------+
cala> val expr = array((1 to 3).map(c => array(struct(lit(s"Address${c}").as("address_no"),col(s"Address.Address${c}").as("address_details"),col("Address.country"),col("Address.full_Address")))):_*)
scala> df.withColumn("address",explode(expr)).select(explode($"address").as("address")).select($"address.address_no",$"address.address_details.*",$"address.country",$"address.full_Address").show(false)
+----------+---+------+-----+------+-------+------------+
|address_no|Hno|pin |state|street|country|full_Address|
+----------+---+------+-----+------+-------+------------+
|Address1 |7 |462036|1 |5 |India |112018 |
|Address2 |0 |462037|0 |25578 |India |112018 |
|Address3 |0 |462038|0 |0 |India |112018 |
+----------+---+------+-----+------+-------+------------+