Hive视图和索引

和关系型数据库中的普通视图一样,hive也支持视图
将部分查询结果,用视图view表示出来,方便查询

索引:提高检索效率
要求,唯一性约束、非空、unique
经常查的列添加索引!
索引建的好,效率提高

Hive View视图

  1. 不支持物化视图,物理文件并不存在!虚拟表也是表,但能显示出来
  2. 只能查询,不能做加载数据操作
  3. 视图的创建,只是保存一份元数据,查询视图时才执行对应的子查询
  4. view定义中若包含了ORDER BY/LIMIT语句,当查询视图时也进行ORDER BY/LIMIT语句操作,view当中定义的优先级更高
  5. view支持迭代视图

视图View语法:

创建视图
CREATE VIEW [IF NOT EXISTS] [db_name.]view_name 
  [(column_name [COMMENT column_comment], ...) ]
  [COMMENT view_comment]
  [TBLPROPERTIES (property_name = property_value, ...)]
  AS SELECT ... ;

查询视图:
select colums from view;
删除视图:
DROP VIEW [IF EXISTS] [db_name.]view_name;

视图例子

creat view v_psn AS select id, name from psn;
select * from v_psn;
drop view v_psn

物理磁盘/user/hive/warehouse中不存在v_psn
元数据库TBLS表,存在v_psn视图信息
show tables也能看得见

《Hive视图和索引》 创建视图View

Hive索引

优化查询以及检索性能

创建索引:
create index t1_index on table psn(name) 
as 'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler' with deferred rebuild 
in table t1_index_table;
as:指定索引器
in table:指定索引表,若不指定默认生成在default__psn_t1_index__表中

create index t2_index on table psn(name) 
as 'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler' with deferred rebuild;


重建索引(建立索引之后必须重建索引才能生效)执行map-reduce,真正建索引往表里面加载数据
ALTER INDEX t1_index ON psn REBUILD;

查看存在的索引
show index on psn;

删除索引、是索引名,不是索引表名称!
会将对应的表删除
DROP INDEX IF EXISTS t1_index ON psn;

show tables也能看得见,索引表中没有数据;rebuild后才有数据
没有索引全局扫描,有索引后找偏移量!

hive> select * from t1_index_table;
OK
t1_index_table.name t1_index_table._bucketname  t1_index_table._offsets
小明1 hdfs://mycluster/user/hive/warehouse/psn/psn.data   [0]
小明2 hdfs://mycluster/user/hive/warehouse/psn/psn.data   [56]
小明3 hdfs://mycluster/user/hive/warehouse/psn/psn.data   [112]
小明4 hdfs://mycluster/user/hive/warehouse/psn/psn.data   [168]
小明5 hdfs://mycluster/user/hive/warehouse/psn/psn.data   [219]
小明6 hdfs://mycluster/user/hive/warehouse/psn/psn.data   [275]
小明7 hdfs://mycluster/user/hive/warehouse/psn/psn.data   [331]
小明8 hdfs://mycluster/user/hive/warehouse/psn/psn.data   [386]
小明9 hdfs://mycluster/user/hive/warehouse/psn/psn.data   [442]
Time taken: 0.256 seconds, Fetched: 9 row(s)
hive> show index on psn;
OK
idx_name    tab_name    col_names   idx_tab_name    idx_type    comment
t1_index                psn                     name                    t1_index_table          compact         
t2_index                psn                     name                    default__psn_t2_index__ compact

Hive Lateral View

  1. Lateral View
    于和UDTF函数(explodesplit)结合来使用。
  2. 首先通过UDTF函数拆分成多行,再将多行结果组合成一个支持别名的虚拟表。
  3. 主要解决在select使用UDTF做查询过程中,查询只能包含单个UDTF,不能包含其他字段、以及多个UDTF的问题
语法:
  • LATERAL VIEW udtf(expression) tableAlias AS columnAlias (',' columnAlias)

统计人员表中共有多少种爱好、多少个城市?

hive> select explode(likes) from psn;

hive> select count(distinct(col1)), count(distinct(col2)) from psn
LATERAL VIEW explode(likes) psn AS col1 
LATERAL VIEW explode(address) psn AS col2, col3;
…… ……
Stage-Stage-1: Map: 1  Reduce: 1   Cumulative CPU: 2.91 sec   HDFS Read: 12601 HDFS Write: 4 SUCCESS
Total MapReduce CPU Time Spent: 2 seconds 910 msec
OK
_c0 _c1
4   2

地址是map=key-value

参考资料

Hadoop集群上搭建Hive
Hive建表并加载数据
Hive参数和动态分区
Hive分桶

    原文作者:geekAppke
    原文地址: https://www.jianshu.com/p/e84a81d488f9
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞