The correct syntax is
select name, color
from data
lateral view explode(names) exploded_names as name
lateral view explode(colors) exploded_colors as color
The reason why Rashid's answer did not work is that it did not "name" the table generated by LATERAL VIEW
.
Explanation
Think of it this way: LATERAL VIEW
works like an implicit JOIN
with with an ephemeral table created for every row from the structs
in the collection being "viewed". So, the way to parse the syntax is:
LATERAL VIEW table_generation_function(collection_column) table_name AS col1, ...
Multiple output columns
If you use a table generating function such as posexplode()
then you still have one output table but with multiple output columns:
LATERAL VIEW posexplode(orders) exploded_orders AS order_number, order
Nesting
You can also "nest" LATERAL VIEW
by repeatedly exploding nested collections, e.g.,
LATERAL VIEW posexplode(orders) exploded_orders AS order_number, order
LATERAL VIEW posexplode(order.items) exploded_items AS item_number, item
Performance considerations
While we are on the topic of LATERAL VIEW
it is important to note that using it via SparkSQL is more efficient than using it via the DataFrame
DSL, e.g., myDF.explode()
. The reason is that SQL can reason accurately about the schema while the DSL API has to perform type conversion between a language type and the dataframe row. What the DSL API loses in terms of performance, however, it gains in flexibility as you can return any supported type from explode
, which means that you can perform a more complicated transformation in one step.
Update
In recent versions of Spark, row-level explode via df.explode()
has been deprecated in favor of column-level explode via df.select(..., explode(...).as(...))
. There is also an explode_outer()
, which will produce output rows even if the input to be exploded is null
. Column-level exploding does not suffer from the performance issues of row-level exploding mentioned above as Spark can perform the transformation entirely using internal row data representations.