ハイブが側面図を爆発させる
Hive Explode Lateral View
SqlServerのカーソルと同様に、フィールドの内容は行表示に変換されます。
側面図UDTF(式)tableAliasName as colAliasName
UDTF(式)は、テーブル生成関数が行から列への関数、つまり、行が展開などの複数行になる関数であることを示します。もちろん、UDFカスタム関数を使用して1行を複数行に変換することも、UDFが配列を返すこともできます。次に、爆発によって複数の行に爆発します
tableAliasNameはテーブルのエイリアスを表し、colAliasNameはテーブルの列のエイリアスを表します
原則は次のとおりです。側面図UDTF(式)関数を使用して行を複数の行に変換し、一時テーブルを生成し、データを一時テーブルに配置してから、一時テーブルとベーステーブルを内部結合に使用します。状態はオリジナルです。テーブルの関係
表のステートメント:
create table sales_info_new( Sku_id string comment 'item id', Sku_name string comment 'product name', State_map map comment 'product status information', Id_array array comment 'Product related id list' ) partitioned by( Dt string comment 'year-month-day' ) row format delimited fields terminated by '|' collection items terminated by ',' map keys terminated by ':'
ローカルからインポートした後、データをクエリします。
------
SELECT explode(id_array) AS new_id FROM sales_info where dt = '2019-04-26' -- 1 column 10 rows
------
explode(配列):
Select sku_id, sku_name from sales_info lateral view explode (id_array) table_alias as id where dt = '2019-04-26' -- 2 columns 10 rows, id column is not automatically appended in the back Select sku_id, sku_name, id from sales_info lateral view explode (id_array) table_alias as id where dt = '2019-04-26' -- 3 columns 10 rows, id column appended in the back Select * from sales_info lateral view explode (id_array) table_alias as id where dt = '2019-04-26' -- 6 columns 10 rows, id column is automatically appended in the back
側面図:フィールド名を指定した場合、直接選択された場合に結果に表示されるように選択する側面図クエリの列を記述する必要があります*、側面図クエリの列が自動的に追加されます
------
where条件は、側面図の後に書き込む必要があります。そうしないと、エラーが報告されます。
Select * from sales_info where dt = '2019-04-26' lateral view explode(id_array) table_alias as id -- error 2019-04-28 16:48:49,044 FAILED: ParseException line 1:51 missing EOF at 'lateral' Near ''2019-04-26''
配列型の後にコンマ分割を続けることはできなくなり、文字列型はできます
select * from sales_info lateral view explode(split(id_array,',')) table_alias as id where dt = '2019-04-26' -- 2019-04-28 16:26:29,224 FAILED: ClassCastException org.apache.hadoop.hive.serde2.objectinspector.StandardListObjectInspector cannot be cast to org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector
Explodeは側面図で使用する必要があり、フィールドだけに表示することはできません。
SELECT sku_id,sku_name,explode(id_array) AS new_id FROM sales_info where dt = '2019-04-26' -- 2019-04-28 15:55:41,642 FAILED: SemanticException [Error 10081]: UDTF's are not supported outside the SELECT clause, nor nested in expressions
------
explode(マップ)
キーのみ、値は2列なので、出力3列エラー
select explode(state_map) as (id,token,user_name) from sales_info where dt = '2019-04-26' -- 2019-04-28 15:57:24,080 FAILED: SemanticException [Error 10083]: The number of aliases supplied in the AS clause does not match the number of columns output by the UDTF expected 2 aliases but got 3
------
Select explode(state_map) as (key,value) from sales_info where dt = '2019-04-26' -- 2 columns 15 rows
------
Select sku_name from sales_info lateral view explode(state_map) table_alias as key,value where dt = '2019-04-26' -- only sku_name 1 column total 15 lines Select sku_name , key, value from sales_info lateral view explode (state_map) table_alias as key, value where dt = '2019-04-26' -- there are 3 columns total 15 rows
------
参照: https://blog.csdn.net/lyzx_in_csdn/article/details/85628867