hive中的lateral view 用法詳解下篇

2021-07-11 21:10:23 字數 2412 閱讀 8464

假設我們有一張表pageads,它有兩列資料,第一列是pageid string,第二列是adid_list,即用逗號分隔的廣告id集合:

string pageid

arrayadid_list

"front_page"

[1, 2, 3]

"contact_page"

[3, 4, 5]

要統計所有廣告id在所有頁面中出現的次數。

首先分拆廣告id:

select pageid, adid from pageads lateral view explode(adid_list) adtable as adid;

執行結果如下:

string pageid

int adid

"front_page"

1"front_page"

2"front_page"

3"contact_page"

3"contact_page"

4"contact_page"5

接下來就是乙個聚合的統計:

select adid, count(1) from pageads lateral view explode(adid_list) adtable as adid

group by adid;

執行結果如下:

int adid

count(1)11

2132

4151

乙個from語句後可以跟多個lateral view語句,後面的lateral view語句能夠引用它前面的所有表和列名。 以下面的表為例:

arraycol1

arraycol2

[1, 2]

[a", "b", "c"]

[3, 4]

[d", "e", "f"]

> lateral view explode(col1) mytable1 as mycol1;

執行結果為:

int mycol1

arraycol2

1[a", "b", "c"]

2[a", "b", "c"]

3[d", "e", "f"]

4[d", "e", "f"]

加上乙個lateral view:

select mycol1, mycol2 from basetable lateral view explode(col1) mytable1 as mycol1

lateral view explode(col2) mytable2 as mycol2;

它的執行結果為:

int mycol1

string mycol2

1"a"

1"b"

1"c"

2"a"

2"b"

2"c"

3"d"

3"e"

3"f"

4"d"

4"e"

4"f"

乙個from語句後可以跟多個lateral view語句,後面的lateral view語句能夠引用它前面的所有表和列名。 以下面的表為例:

arraycol1

arraycol2

[1, 2]

[a", "b", "c"]

[3, 4]

[d", "e", "f"]

>select mycol1, col2 from basetable

lateral view explode(col1) mytable1 as mycol1;

執行結果為:

int mycol1

arraycol2

1[a", "b", "c"]

2[a", "b", "c"]

3[d", "e", "f"]

4[d", "e", "f"]

加上乙個lateral view:

select mycol1, mycol2 from basetable lateral view explode(col1) mytable1 as mycol1

lateral view explode(col2) mytable2 as mycol2;

它的執行結果為:

int mycol1

string mycol2

1"a"

1"b"

1"c"

2"a"

2"b"

2"c"

3"d"

3"e"

3"f"

4"d"

4"e"

4"f"

注意上面語句中,兩個lateral view按照出現的次序被執行。

** 

Hive 中的日誌

日誌記錄了程式執行的過程,是一種查詢問題的利器。hive中的日誌分為兩種 1.系統日誌,記錄了hive的運 況,錯誤狀況。2.job 日誌,記錄了hive 中job的執行的歷史過程。系統日誌儲存在什麼地方呢 在hive conf hive log4j.properties 檔案中記錄了hive日誌的...

Hive 中的日誌

日誌記錄了程式執行的過程,是一種查詢問題的利器。hive中的日誌分為兩種 1.系統日誌,記錄了hive的運 況,錯誤狀況。2.job 日誌,記錄了hive 中job的執行的歷史過程。系統日誌儲存在什麼地方呢 在hive conf hive log4j.properties 檔案中記錄了hive日誌的...

hive學習 hive中的資料型別

1 基本型別 資料型別 所佔位元組 開始支援版本 tinyint 1byte,128 127 smallint 2byte,32,768 32,767 int4byte,2,147,483,648 2,147,483,647 bigint 8byte,9,223,372,036,854,775,80...