程序员求职经验分享与学习资料整理平台

网站首页 > 文章精选 正文

Hive Lateral View、视图、索引以及授权使用详解

balukai 2025-02-11 10:54:32 文章精选 7 ℃

Hive Lateral View、试图和索引

Lateral View

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

使用方式如下:

select id, mycol1, mycol2, mycol3 from table_name
lateral view explode(column1) mytable1 as mycol1
lateral view explode(column2) mytable2 as mycol2, mycol3

Hive视图

和关系型数据库中的普通视图一样,hive也支持视图。

特点:

  • 不支持物化视图
  • 只能查询,不能做加载数据操作
  • 视图的创建,只是保存一份元数据,查询视图时才执行对应的子查询
  • view定义中若包含了order by / limit语句,当查询视图时也进行了order by / limit语句操作,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 ..

删除视图

drop view [if exists] [db_name.]view_name

Hive索引

所以主要目的是优化查询和检索性能。

创建索引

create index t1_index on table person2(name)
as 'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler'
with defered rebuild
in table t1_index_table;
  • as:用来指定索引器
  • in table:指定索引表,若不指定默认生成在default_person_t1_index_表中

查询索引

show index on table_name

重建索引

alter index index_name on table_name rebuild;

重建完毕之后,再次使用有索引的数据,即通过select查询数据

删除索引

drop index [if exists] index_name on table_name;

Hive授权

Hive 在Hiveserver2中使用标准SQL授权

限制

  1. 启动当前认证方式之后,dfs, add, delete, compile, reset等命令被禁用
  2. 通过set命令设置hive configurations的方式被限制某些用户使用
  3. 可通过修改配置文件$HIVE_HOME/conf/hive-site.xml配置hive.security.authorization.sqlstd.confwhitelist
  4. 添加/删除函数以及宏的操作,仅为具有admin的用户开放
  5. 用户自定义函数(开放支持永久的自定义函数),可通过具有admin角色的用户创建,其他用户可以使用
  6. tranform功能被禁用

hive-site.xml配置


    hive.security.authorization.enabled
    true


    hive.server2.enable.doAs
    false


    hive.users.in.admin.role
    root


    hive.security.authorization.manager
    org.apache.hadoop.hive.ql.security.authorization.plugin.sqlstd.SQLStdHiveAuthorizerFactory


    hive.security.authenticator.manager
    org.apache.hadoop.hive.ql.secruity.SessionStateUserAuthenticator

角色操作

create role role_name; -- 创建角色
drop role role_name; -- 删除角色
set role (role_name|all|none); -- 设置角色
show current roles; -- 查看当前具有的角色
show roles; -- 查看所有存在的角色

授权

角色的授予、移除、查看

-- 将角色授予某个用户、角色
grant role_name [, role_name]... to principal_specification
[,principal_specification].. [with admin option];

-- 撤销角色
revoke [ADMIN OPTION FOR] role_name [, role_name]..
from principal_specification[, principal_specification]..


pricipal_specification:
: USER user_name
| ROLE role_name


-- 删除角色
 drop role role_name;
-- 创建角色
create role role1;
-- 授权
grant admin to role role1 with admin option;

-- 查看授予某个用户/角色的角色列表
show role grant(user|role) user_role_name;

-- 查看角色被授予的列表
show principals admin;

-- 取消授权

权限授予

权限一共分为以下几类:

  • ALL:给用户所有的权限
  • ALTER:给用户修改元数据的权限
  • UPDATE:允许用户修改物理数据的权限
  • CREATE:给用户创建的权限,包括创建数据库,表。这也有意味着能够创建分区等
  • DROP:允许用户删除数据资源
  • INDEX:允许用户创建索引
  • LOCK:允许用户在并发情况下锁定/解锁表
  • SELECT:允许用户检索数据库数据
  • SHOW_DATABASE:允许用户查看可用的数据的列表

权限授予语法

授权的操作和上面的角色授权很相似,

-- 授权
GRANT priv_type[, priv_type]..
ON table_or_view_name
to principal_specification[, principal_specification]..
[WITH GRANT OPTION];

-- 移除权限
REVOKE [GRANT OPTION FOR]
priv_type[, priv_type...]
ON table_or_view_name
FROM principal_specification[, principal_specification..]

-- 查看某个用户、角色的权限
SHOW GRANT [principal_name] ON (ALL | TABLE table_or_view_name)


pricipal_specification格式如下:
USER user_name
| ROLE role_name
最近发表
标签列表