A custom UDTF can be created by extending the GenericUDTF abstract class and then implementing the initialize, process, and possibly close methods. The initialize method is called by Hive to notify the UDTF the argument types to expect. The UDTF must then return an object inspector corresponding to the row objects that the UDTF will generate. Once initialize() has been called, Hive will give rows to the UDTF using the process() method. While in process(), the UDTF can produce and forward rows to other operators by calling forward(). Lastly, Hive will call the close() method when all the rows have passed to the UDTF.
create function mp_explod as "tech.mapan.hive.udtf.ExplodUDTF2"; OK Time taken: 0.029 seconds
hive (prac)> select * from test03; OK test03.id test03.name 1001 jack#ma 1002 dong#liu 1003 poney#ma Time taken: 0.522 seconds, Fetched: 3 row(s)
hive (prac)> select id,first_name,last_name from test03 lateral view mp_explod2(name,"#") temp as first_name,last_name; OK id first_name last_name 1001 jack ma 1002 dong liu 1003 poney ma Time taken: 13.393 seconds, Fetched: 3 row(s)
Lateral View Syntax
lateralView: LATERAL VIEW udtf(expression) tableAlias AS columnAlias (','columnAlias)* fromClause: FROM baseTable (lateralView)*
Lateral view is used in conjunction with user-defined table generating functions such as explode(). As mentioned in Built-in Table-Generating Functions, a UDTF generates zero or more output rows for each input row. A lateral view first applies the UDTF to each row of base table and then joins resulting output rows to the input rows to form a virtual table having the supplied table alias.