(四)Hive进阶操作-复杂类型操作演示和视图介绍

1.复杂类型

这小节演示下Hive的复杂类型,也就是Array、Map和Struct
那我们先来一份数据,
cat data.txt

1001    zhangsan    a1,b1,c1    a1:88,b1:39,c1:98    bj,[email protected],18210111011
1002    lisi    a2,b2,c2    a2:99,b2:23,c2:81    tj,ls@123,com,13611211121

有了数据我们开始建表并导入数据

-- 建表
create table demo_t1(
id int,
name string,
courses array,
score map,
info struct)
row format delimited fields terminated by "\t"
collection items terminated by ","
map keys terminated by":"
lines terminated by "\n";

-- 导入数据
load data local inpath "/home/v2admin/demo/array_demo/data.txt" into table demo_t1;

查询下 select * from demo_t1;

+-------------+---------------+-------------------+----------------------------+----------------------------------------------------+
| demo_t1.id  | demo_t1.name  |  demo_t1.courses  |       demo_t1.score        |                    demo_t1.info                    |
+-------------+---------------+-------------------+----------------------------+----------------------------------------------------+
| 1001        | zhangsan      | ["a1","b1","c1"]  | {"a1":88,"b1":39,"c1":98}  | {"address":"bj","email":"[email protected]","phone":"18210111011"} |
| 1002        | lisi          | ["a2","b2","c2"]  | {"a2":99,"b2":23,"c2":81}  | {"address":"tj","email":"ls@123","phone":"com"}    |
+-------------+---------------+-------------------+----------------------------+----------------------------------------------------+
  • Array

我们查看下每个人的课程
select name,courses from demo_t1;

+-----------+-------------------+
|   name    |      courses      |
+-----------+-------------------+
| zhangsan  | ["a1","b1","c1"]  |
| lisi      | ["a2","b2","c2"]  |
+-----------+-------------------+

我们只看第一个课程
select name,courses[0] from demo_t1;

+-----------+------+
|   name    | _c1  |
+-----------+------+
| zhangsan  | a1   |
| lisi      | a2   |
+-----------+------+
  • map

我们查看下每个人的score
select name,score from demo_t1;

+-----------+----------------------------+
|   name    |           score            |
+-----------+----------------------------+
| zhangsan  | {"a1":88,"b1":39,"c1":98}  |
| lisi      | {"a2":99,"b2":23,"c2":81}  |
+-----------+----------------------------+

我们只看a1课程的成绩
select name,score['a1'] from demo_t1;

+-----------+-------+
|   name    |  _c1  |
+-----------+-------+
| zhangsan  | 88    |
| lisi      | NULL  |
+-----------+-------+
  • struct

我们看下每个人的info
select name,info from demo_t1;

+-----------+----------------------------------------------------+
|   name    |                        info                        |
+-----------+----------------------------------------------------+
| zhangsan  | {"address":"bj","email":"[email protected]","phone":"18210111011"} |
| lisi      | {"address":"tj","email":"ls@123","phone":"com"}    |
+-----------+----------------------------------------------------+

我们查看下每个人的地址
select name,info.address from demo_t1;

+-----------+----------+
|   name    | address  |
+-----------+----------+
| zhangsan  | bj       |
| lisi      | tj       |
+-----------+----------+

2 视图

Hive视图跟mysql等关系型数据库的视图类似,有以下特点
1)Hive中的视图是逻辑层面的
2)Hive中的视图只能提供查询功能,不能进行增删改查的操作
3)Hive中的视图在创建时,保存的只是元数据,只要在真正使用视图查询时,才会执行视图对应的子查询语句。

  • 创建视图

和mysql类似

create view view_demo as select  id,name from demo_t1;
  • 使用视图
select name from view_demo;
  • 删除视图
drop view view_demo;

你可能感兴趣的:(hive)