一、Hive Lateral View

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

语法:

LATERAL VIEW udtf(expression) tableAlias AS columnAlias (',' columnAlias)

select explode(likes) from psn2;命令可将likes数组拆分。explode拆分,只能接一个字段。

案例:
统计人员表中一共有多少种爱好、城市?

从psn2表中查询,并将结果输入到虚拟表中:
拆分likes数组,定义别名,结果放在myCol1中。
拆分address集合,定义别名,k、v分别放在myCol2和myCol3中

select count(distinct(myCol1)), count(distinct(myCol2)) from psn2
LATERAL VIEW explode(likes) myTable1 AS myCol1
LATERAL VIEW explode(address) myTable2 AS myCol2, myCol3;

二、Hive视图

特点:

  • 1.不支持物化视图
  • 2.只能查询,不能做加载数据操作(写入)
  • 3.视图的创建,只是保存一份元数据,查询视图时才执行对应的子查询
  • 4.view定义中若包含了ORDER BY/LIMIT语句,当查询视图时也进行ORDER BY/LIMIT语句操作,view当中定义的优先级更高
  • 5.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;

三、Hive索引

解决查询问题,避免全表扫描,提高检索的性能。索引具体的添加,要根据数据来定。

1.创建索引:

as:指定索引器;
in table:指定索引表,若不指定默认生成在default__psn2_t1_index__表中

create index t1_index on table psn2(name)
as 'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler' with deferred rebuild
in table t1_index_table;

或者可以这样写,不指定索引表,即默认生成default__psn2_t1_index__:

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

此时索引表中都是空的,还要生成索引信息。

2.重建索引:
创建索引之后,必须重建索引才能生效。

ALTER INDEX t1_index ON psn2 REBUILD;

索引表查询结果:

hive> select * from t1_index_table;
OK
t1_index_table.name t1_index_table._bucketname  t1_index_table._offsets t1_index_table.age
小明1 hdfs://mycluster/user/hive/warehouse/psn2/age=10/data1 [0] 10
小明2 hdfs://mycluster/user/hive/warehouse/psn2/age=10/data1 [56]    10
小明3 hdfs://mycluster/user/hive/warehouse/psn2/age=10/data1 [112]   10
小明4 hdfs://mycluster/user/hive/warehouse/psn2/age=10/data1 [168]   10
小明5 hdfs://mycluster/user/hive/warehouse/psn2/age=10/data1 [224]   10
小明6 hdfs://mycluster/user/hive/warehouse/psn2/age=10/data1 [275]   10
小明7 hdfs://mycluster/user/hive/warehouse/psn2/age=10/data1 [331]   10
小明8 hdfs://mycluster/user/hive/warehouse/psn2/age=10/data1 [381]   10
小明9 hdfs://mycluster/user/hive/warehouse/psn2/age=10/data1 [431]   10
张三1 hdfs://mycluster/user/hive/warehouse/psn2/age=20/data2 [0] 20
张三2 hdfs://mycluster/user/hive/warehouse/psn2/age=20/data2 [58]    20
张三3 hdfs://mycluster/user/hive/warehouse/psn2/age=20/data2 [116]   20
张三4 hdfs://mycluster/user/hive/warehouse/psn2/age=20/data2 [174]   20
张三5 hdfs://mycluster/user/hive/warehouse/psn2/age=20/data2 [232]   20
张三6 hdfs://mycluster/user/hive/warehouse/psn2/age=20/data2 [285]   20
张三7 hdfs://mycluster/user/hive/warehouse/psn2/age=20/data2 [343]   20
张三8 hdfs://mycluster/user/hive/warehouse/psn2/age=20/data2 [395]   20
张三9 hdfs://mycluster/user/hive/warehouse/psn2/age=20/data2 [447]   20
Time taken: 0.127 seconds, Fetched: 18 row(s)

有了索引,查询才会更快:

hive> select * from psn2 where name='小明2';
OK
psn2.id psn2.name   psn2.likes  psn2.address    psn2.age
2   小明2 ["lol","book","movie"]    {"beijing":"longze","shanghai":"pudong"}    10
Time taken: 0.227 seconds, Fetched: 1 row(s)

3.查询索引

hive> show index on psn2;
OK
idx_name    tab_name    col_names   idx_tab_name    idx_type    comment
t1_index                psn2                    name                    t1_index_table          compact
t2_index                psn2                    name                    default__psn2_t2_index__ compact
Time taken: 0.146 seconds, Fetched: 2 row(s)

4.删除索引

DROP INDEX IF EXISTS t1_index ON psn2;

Hive Lateral View、视图、索引相关推荐

  1. Hive Lateral View + explode 详解

    hive中的函数分为3类,UDF函数.UDAF函数.UDTF函数 UDF:一进一出 UDAF:聚集函数,多进一出,类似于:count/max/min UDTF:一进多出,如explore().pose ...

  2. Hive Lateral View

    目录 Hive explode介绍: Hive posexplode介绍: Lateral View介绍: 使用多个Lateral View: outer关键字: Hive explode介绍: 它会 ...

  3. Hive lateral view 的用法

    lateral view 的语法格式 lateral view: LATERAL VIEW udtf (expression) tableAlias AS coluumAlias ( ',' , co ...

  4. Hive Lateral View使用指南

    1. 语法 lateralView: LATERAL VIEW udtf(expression) tableAlias AS columnAlias (',' columnAlias)* fromCl ...

  5. [hive]lateral view炸开两层

    一. 说明 1.lateral view 侧视图lateral view要和explode/split等UDTF函数一起使用,他可以在函数拆分成多行的基础上,将表其他字段按照拆分结果进行聚合. 2.用 ...

  6. Hive Lateral View explode字段值为空时,导致数据异常丢失解决方案

    1.问题描述 日常工作中,我们经常会用Lateral View 结合explode把非结构化数据转化成结构化数据,但是该方法对应explode的内容是有非null限制的,否则就有可能造成数据缺失. 现 ...

  7. hive lateral view explode列拆分与行转列用法

    在日常工作中,我们在处理数据时,会遇到某个列存在多个数据的情况,如果想拿到这个列里面每个数据进行后续处理的话,这种情况下有两种处理方式: 第一种:如果这个列多个数据是固定数目,可以使用split切分 ...

  8. mysql explode函数_hive中,lateral view 与 explode函数

    hive中常规处理json数据,array类型json用get_json_object(#,"$.#")这个方法足够了,map类型复合型json就需要通过数据处理才能解析. exp ...

  9. hive中实现行转列_Hive之行转列lateral view用法

    一般写sql经常会遇到行转列或者列转行之类的操作,就像concat_ws之类的函数被广泛的使用,今天这个也是经常要使用的拓展方法. Lateral View 语法 描述 横向视图与用户定义的表生成函数 ...

最新文章

  1. asp.net工程中aspx文件与codebehind文件的关联问题
  2. UnicodeDecodeError: ‘utf-8‘ codec can‘t decode bytes in position 708-709: invalid continuation byte
  3. sdut 1466 双向队列
  4. 用终端访问路由器设置端口开发_Serial for Mac(全功能串行终端管理软件)
  5. 工程和模块的关系以及继承和依赖的概念
  6. linux命令cp命令行参数,linux命令之cp命令参数及用法详解
  7. Linux free 命令详解
  8. 使用Nacos项目jar包启动抛出的yml异常
  9. Java接口自动化之Maven工具使用
  10. matlab字体设置
  11. salesforce 学习(超简介,以及传送门)
  12. Bailian2801 填词 POJ1629 ZOJ1546 Fillword【排序】
  13. MySQL 第一次练习(安装MySQL)
  14. 免费的配音软件,堪比魔云熙声音。
  15. 招行网银常见问题汇总
  16. 计算机太极之光,3000多名研究生赛太极,五大太极拳流派名家展风采
  17. Python 测试题(覆盖了大多数的基础知识和进阶)
  18. matlab怎么绘制零极点,matlab中画系统零极点的方法
  19. 基于springboot vue uniapp点餐外码系统源码(毕设)
  20. [Linux]基本体系结构

热门文章

  1. solidworks经典实例网盘下载_Solidworks自学视频教程(附源文件)讲解详细到位,成就设计高手...
  2. 单链表的插入和删除_从0开始的编程之梦——数据结构之单链表的基本运算
  3. Snackbar源码分析
  4. 【Interfacenavigation】用RecyclerView创建一个列表(4)
  5. 半导体并购停不下来 ADI拟148亿美元收购Linear
  6. saiku 3.8 二次开发代码整理步骤(20160727更新)
  7. java对象的强引用,软引用,弱引用和虚引用
  8. JS中定义式函数与变量时函数的差别
  9. 设计模式学习每天一个——Factory模式 和 Abstract Factory模式
  10. this指向总结(无栗子)