在Hive中创建视图表

时间:2017-03-08 04:40:15

标签: sql hadoop hive hiveql

我有两个数据集。第1个android设置有10列,第二个ios设置有10列,其中6列相似,但其余的4个是不同的。在每个数据集中的那4列,一列与它们各自的OS有关我的意思是android和ios.Now我需要通过组合这两个表来创建一个hive视图,这样当我运行视图查询时,最终的聚合表应该看起来像这样

View_table:

os_7|1|2|3|4|5|6|8_android_different|9_and_dif|10_and_dif|8_ios_dif|9_ios_dif|10_ios_dif|

android All the android data from 1-6 should come here
ios     All the ios data from 1-6 should come here
ios
android
android

这样可以通过将名为data的同一列包含在一个字段中来最小化列长度,并且其他不同的数据应该连接到列,如上所述。

1 个答案:

答案 0 :(得分:1)

filterByTeam

<强>演示

create view os
as
select  os
       ,common_1
       ,common_2
       ,common_3
       ,common_4
       ,common_5
       ,common_6
       ,android_1
       ,android_2
       ,android_3
       ,null    as ios_1
       ,null    as ios_2
       ,null    as ios_3 

from    os_android

union all

select  os
       ,common_1
       ,common_2
       ,common_3
       ,common_4
       ,common_5
       ,common_6
       ,null    as android_1
       ,null    as android_2
       ,null    as android_3
       ,ios_1
       ,ios_2
       ,ios_3 

from    os_ios
create table os_android
(
    os          string
   ,common_1    string
   ,common_2    string
   ,common_3    string
   ,common_4    string
   ,common_5    string
   ,common_6    string
   ,android_1   string
   ,android_2   string
   ,android_3   string
);
create table os_ios
(
    os          string
   ,common_1    string
   ,common_2    string
   ,common_3    string
   ,common_4    string
   ,common_5    string
   ,common_6    string
   ,ios_1       string
   ,ios_2       string
   ,ios_3       string
)
;
create view os
as
select  os
       ,common_1
       ,common_2
       ,common_3
       ,common_4
       ,common_5
       ,common_6
       ,android_1
       ,android_2
       ,android_3
       ,null    as ios_1
       ,null    as ios_2
       ,null    as ios_3 

from    os_android

union all

select  os
       ,common_1
       ,common_2
       ,common_3
       ,common_4
       ,common_5
       ,common_6
       ,null    as android_1
       ,null    as android_2
       ,null    as android_3
       ,ios_1
       ,ios_2
       ,ios_3 

from    os_ios
;
insert into os_android values 
    ('ANDROID_1','C1_1','C2_1','C3_1','C4_1','C5_1','C6_1','A1_1','A2_1','A3_1')
   ,('ANDROID_2','C1_2','C2_2','C3_2','C4_2','C5_2','C6_2','A1_2','A2_2','A3_2')
;

insert into os_ios values 
    ('IOS_3','C1_3','C2_3','C3_3','C4_3','C5_3','C6_3','I1_3','I2_3','I3_3')
   ,('IOS_4','C1_4','C2_4','C3_4','C4_4','C5_4','C6_4','I1_4','I2_4','I3_4')
;
select * from os_android
;

+---------------+---------------------+---------------------+---------------------+---------------------+---------------------+---------------------+----------------------+----------------------+----------------------+
| os_android.os | os_android.common_1 | os_android.common_2 | os_android.common_3 | os_android.common_4 | os_android.common_5 | os_android.common_6 | os_android.android_1 | os_android.android_2 | os_android.android_3 |
+---------------+---------------------+---------------------+---------------------+---------------------+---------------------+---------------------+----------------------+----------------------+----------------------+
| ANDROID_1     | C1_1                | C2_1                | C3_1                | C4_1                | C5_1                | C6_1                | A1_1                 | A2_1                 | A3_1                 |
| ANDROID_2     | C1_2                | C2_2                | C3_2                | C4_2                | C5_2                | C6_2                | A1_2                 | A2_2                 | A3_2                 |
+---------------+---------------------+---------------------+---------------------+---------------------+---------------------+---------------------+----------------------+----------------------+----------------------+
select * from os_ios
;
+-----------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+--------------+--------------+--------------+
| os_ios.os | os_ios.common_1 | os_ios.common_2 | os_ios.common_3 | os_ios.common_4 | os_ios.common_5 | os_ios.common_6 | os_ios.ios_1 | os_ios.ios_2 | os_ios.ios_3 |
+-----------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+--------------+--------------+--------------+
| IOS_3     | C1_3            | C2_3            | C3_3            | C4_3            | C5_3            | C6_3            | I1_3         | I2_3         | I3_3         |
| IOS_4     | C1_4            | C2_4            | C3_4            | C4_4            | C5_4            | C6_4            | I1_4         | I2_4         | I3_4         |
+-----------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+--------------+--------------+--------------+
select * from os
;