Kopīgot, izmantojot


explode_outer

Returns a new row for each element in the given array or map. Unlike explode, if the array/map is null or empty then null is produced. Uses the default column name col for elements in the array and key and value for elements in the map unless specified otherwise.

Syntax

from pyspark.sql import functions as sf

sf.explode_outer(col)

Parameters

Parameter Type Description
col pyspark.sql.Column or column name Target column to work on.

Returns

pyspark.sql.Column: one row per array item or map key value.

Examples

Example 1: Using an array column

from pyspark.sql import functions as sf
df = spark.sql('SELECT * FROM VALUES (1,ARRAY(1,2,3,NULL)), (2,ARRAY()), (3,NULL) AS t(i,a)')
df.select('*', sf.explode_outer('a')).show()
+---+---------------+----+
|  i|              a| col|
+---+---------------+----+
|  1|[1, 2, 3, NULL]|   1|
|  1|[1, 2, 3, NULL]|   2|
|  1|[1, 2, 3, NULL]|   3|
|  1|[1, 2, 3, NULL]|NULL|
|  2|             []|NULL|
|  3|           NULL|NULL|
+---+---------------+----+

Example 2: Using a map column

from pyspark.sql import functions as sf
df = spark.sql('SELECT * FROM VALUES (1,MAP(1,2,3,4,5,NULL)), (2,MAP()), (3,NULL) AS t(i,m)')
df.select('*', sf.explode_outer('m')).show(truncate=False)
+---+---------------------------+----+-----+
|i  |m                          |key |value|
+---+---------------------------+----+-----+
|1  |{1 -> 2, 3 -> 4, 5 -> NULL}|1   |2    |
|1  |{1 -> 2, 3 -> 4, 5 -> NULL}|3   |4    |
|1  |{1 -> 2, 3 -> 4, 5 -> NULL}|5   |NULL |
|2  |{}                         |NULL|NULL |
|3  |NULL                       |NULL|NULL |
+---+---------------------------+----+-----+