Hive - 使用两个现有的 Hive 表创建视图

Hive - Create a view using two existing Hive tables

我是 Hive 的初学者。我有两个 Hive 表如下:

Table A 包含列 - date, name, age. Table A 日期列中值的范围是从 20150406 到 20150408。

Table B 是 Table A 的副本 - 但又添加了一个新列 - date, name, **dept**, age Table B 日期列中值的范围是从 20150409 到 20150411。

我想使用 Tables 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

示例:

Table一个

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

TableB

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

这可行吗?如何做到这一点?

提前致谢!

你快完成了:

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'

查看详细解决方案:

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 中进一步可用只是为了确保:

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)