POSEXPLODE
Description
The posexplode
function is used to expand an input array or map type expression expr
into multiple rows of data, adding a position column to each row to indicate the relative position of the element in the original array or map. This function can be used directly or in conjunction with LATERAL VIEW
to achieve data expansion in more complex queries.
Functionality
- For array type input,
posexplode
expands each element in the array into a row, adding columns namedpos
andcol
to represent the position and value of the element, respectively. - For map type input,
posexplode
expands each key-value pair in the map into a row, adding columns namedpos
,key
, andvalue
to represent the position, key, and value of the element, respectively.
Parameters
expr
: Input arrayarray<T>
or mapmap<K, V>
type.
Return Results
- For array type input, the return type is
int, T
. - For map type input, the return type is
int, K, V
.
Usage Example
- Expand array type input:
- Expand the input of the mapping type:
- Using in combination with
LATERAL VIEW
: - Expand the array and filter specific elements:
- Expand the mapping and calculate the sum of values:
By the above example, you can better understand the usage and functionality of the posexplode
function. In practical applications, you can expand and process array or map type data as needed.