2015-05-28 59 views
0

我是Hive的初學者。我有兩個蜂巢表如下:配置單元 - 使用兩個現有的Hive表創建視圖

表A中包含的列 - date, name, age. 範圍爲從表A的時間列的值是從20150406到20150408.

表B是表A的副本 - 但一個更新的列添加 - date, name, **dept**, age 範圍爲從表B的時間列的值是從20150409到20150411.

我想用表A和B,使得

View A = 
Table A(date, name, dept as NULL, age) //for dates 20150406 to 20150408 
UNION 
Table B(date, name, dept, age) //for dates 20150409 to 20150411 
創建一個視圖

實施例:

表A

date | name | age 
20150406 | John | 21 
20150407 | Jane | 23 
20150408 | Mary | 20 

表B

date | name | dept | age 
20150409 | Claire | CSE | 25 
20150410 | Cindy | Maths | 27 
20150408 | Tom | Biology | 30 

視圖A

date | name | dept | age 
20150406 | John | NULL | 21 
20150407 | Jane | NULL | 23 
20150408 | Mary | NULL | 20 
20150409 | Claire | CSE | 25 
20150410 | Cindy | Maths | 27 
20150408 | Tom | Biology | 30 

這是可行的?如何才能做到這一點?

在此先感謝!

回答

0

見詳細的解決方案:

hive> create table tableA(date String,name string,age int) row format delimited fields terminated by '\t' stored as textfile; 
OK 
Time taken: 0.084 seconds 

hive> create table tableB(date String,name string,dept String,age int) row format delimited fields terminated by '\t' stored as textfile; 
OK 
Time taken: 0.103 seconds 

然後從本地通過負載蜂巢:

hive> load data local inpath '/home/hastimal/PracticeTableData/tableB' into table tableB; 
Loading data to table default.tableb 
Table default.tableb stats: [numFiles=1, totalSize=71] 
OK 
Time taken: 0.291 seconds 

hive> load data local inpath '/home/hastimal/PracticeTableData/tableA' into table tableA; 
Loading data to table default.tablea 
Table default.tablea stats: [numFiles=1, totalSize=51] 
OK 

在蜂巢進一步提供只是爲了讓當然:

hive> select * from tableA; 
OK 
20150406 John 21 
20150407 Jane 23 
20150408 Mary 20 
Time taken: 0.126 seconds, Fetched: 3 row(s) 

hive> select * from tableB; 
OK 
20150409 Claire CSE 25 
20150410 Cindy Maths 27 
20150408 Tom Biology 30 
Time taken: 0.11 seconds, Fetched: 3 row(s) 

最終的解決方案:)

SELECT tbA.date AS a ,tbA.name AS b ,NULL AS c,tbA.age AS d FROM tableA tbA 
UNION ALL 
SELECT tbB.date AS a ,tbB.name AS b ,tbB.dept AS c,tbB.age AS d FROM tableB tbB 

見輸出:

OK 
20150409 Claire CSE 25 
20150410 Cindy Maths 27 
20150408 Tom Biology 30 
20150406 John NULL 21 
20150407 Jane NULL 23 
20150408 Mary NULL 20 
Time taken: 43.462 seconds, Fetched: 6 row(s) 
+0

@activelearner您的解決方案,你可以這樣做:「蜂巢>創建表的表A(日期字符串,字符串名,年齡INT)以'|結尾的行格式分隔字段'以文本文件存儲';' – ChikuMiku

+0

@activelearner同時在UNION ALL中更改tableA下面的tableA,以便您可以精確地解決您的需求:) – ChikuMiku

1

就快:

create view viewA 
as 
select date, name, NULL as dept, age 
from tableA 
where date between '20150406' and '20150408' 
union all 
select date, name, dept, age 
from tableB 
where date between '20150409' and '20150411' 
相關問題