如何使用Sentry管理Hive外部表(补充)

时间:2022-05-06
本文章向大家介绍如何使用Sentry管理Hive外部表(补充),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

温馨提示:要看高清无码套图,请使用手机打开并单击图片放大查看。

1.文档编写目的


本文文档主要讲述如何使用Sentry管理Hive/Impala外部表权限。

  • 内容概述

1.创建测试库及外部表

2.创建角色并授权

3.授权测试

4.测试总结

  • 测试环境

1.操作系统为CentOS6.5

2.CM和CDH版本为5.12.1

3.采用root用户操作

  • 前置条件

1.集群运行正常

2.集群已启用Kerberos且正常使用

3.HDFS/Hive/Impala/Hue服务已与Sentry集成

4.Hive用户为超级用户

友情提示:总结是精华。

2.创建测试库及外部表


  1. 使用hive用户登录Kerberos,并通过beeline登录HiveServer2

创建fayson数据库

0: jdbc:hive2://localhost:10000/> create database fayson;
INFO  : Compiling command(queryId=hive_20170916155353_12e7c551-6a72-4ff3-b581-353c4dbd0fb0): create database fayson
INFO  : Semantic Analysis Completed
…
INFO  : OK
No rows affected (0.232 seconds)
0: jdbc:hive2://localhost:10000/> 

2.在fayson库下创建外部表student_hive,建表语句如下

create external table if not exists student_hive(
  name string,
  age int,
  addr string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
LOCATION '/extwarehouse/student_hive';

向/extwarehouse/student_hive表put数据

[root@ip-172-31-6-148 ~]# hadoop fs -put student.txt /extwarehouse/student_hive
[root@ip-172-31-6-148 ~]# hadoop fs -ls /extwarehouse/student_hive
Found 1 items
-rw-r--r--   3 hive supergroup         59 2017-09-16 16:05 /extwarehouse/student_hive/student.txt
[root@ip-172-31-6-148 ~]# 

/extwarehouse/student_hive数据目录不存,在创建外部表时自动生成,且数据目录属主为hive。

0: jdbc:hive2://localhost:10000/> select * from student_hive;
...
INFO  : OK
+--------------------+-------------------+--------------------+--+
| student_hive.name  | student_hive.age  | student_hive.addr  |
+--------------------+-------------------+--------------------+--+
| fayson             | 23                | guangdong          |
| zhangsan           | 24                | shenzhen           |
| lisi               | 55                | guangzhou          |
+--------------------+-------------------+--------------------+--+
3 rows selected (0.216 seconds)
0: jdbc:hive2://localhost:10000/> 

3.创建角色并授权


  1. 创建faysonall角色并授权给fayson用户组

授权fayson用户组拥有fayson库所有权限

create role faysonall;
grant all on database fayson to role faysonall;
grant role faysonall to group fayson;

4.授权测试


  1. 使用fayosn用户登录Kerberos,通过beeline连接HiveServer2
[fayson@ip-172-31-6-148 root]$ beeline 
Beeline version 1.1.0-cdh5.12.1 by Apache Hive
beeline> !connect jdbc:hive2://localhost:10000/;principal=hive/ip-172-31-6-148.fayson.com@FAYSON.COM
scan complete in 2ms
Connecting to jdbc:hive2://localhost:10000/;principal=hive/ip-172-31-6-148.fayson.com@FAYSON.COM
Connected to: Apache Hive (version 1.1.0-cdh5.12.1)
Driver: Hive JDBC (version 1.1.0-cdh5.12.1)
Transaction isolation: TRANSACTION_REPEATABLE_READ
0: jdbc:hive2://localhost:10000/> 

2.切换至fayson数据库对student_hive表操作

可以向表中插入数据

0: jdbc:hive2://localhost:10000/> insert into student_hive values('lisi', 22, 'beijing');
...
INFO  : OK
No rows affected (22.501 seconds)
0: jdbc:hive2://localhost:10000/>

可以查询表数据

0: jdbc:hive2://localhost:10000/> select * from student_hive;
...
INFO  : OK
+--------------------+-------------------+--------------------+--+
| student_hive.name  | student_hive.age  | student_hive.addr  |
+--------------------+-------------------+--------------------+--+
| lisi               | 22                | beijing            |
| fayson             | 23                | guangdong          |
| zhangsan           | 24                | shenzhen           |
| lisi               | 55                | guangzhou          |
+--------------------+-------------------+--------------------+--+
4 rows selected (0.215 seconds)
0: jdbc:hive2://localhost:10000/> 

3.HDFS验证

fayson用户可以浏览student_hive的数据目录,查看数据目录下文件内容,但没有delete和put文件的权限。

[fayson@ip-172-31-6-148 ~]$ hadoop fs -ls /extwarehouse/student_hive
Found 2 items
-rwxr-xr-x   3 hive supergroup         16 2017-09-16 16:16 /extwarehouse/student_hive/000000_0
-rw-r--r--   3 hive supergroup         59 2017-09-16 16:05 /extwarehouse/student_hive/student.txt
[fayson@ip-172-31-6-148 ~]$ hadoop fs -rmr /extwarehouse/student_hive/student.txt
rmr: DEPRECATED: Please use 'rm -r' instead.
rmr: Failed to move to trash: hdfs://ip-172-31-6-148.fayson.com:8020/extwarehouse/student_hive/student.txt: Permission denied: user=fayson, access=WRITE, inode="/extwarehouse/student_hive":hive:supergroup:drwxr-xr-x
[fayson@ip-172-31-6-148 ~]$ hadoop fs -put student1.txt /extwarehouse/student_hive/
put: Permission denied: user=fayson, access=WRITE, inode="/extwarehouse/student_hive":hive:supergroup:drwxr-xr-x
[fayson@ip-172-31-6-148 ~]$ 

测试总结:

hive创建的外部表,通过Sentry授权后,fayson用户组使用beeline和Hue能对该表进行查询和插入操作。但不能对HDFS和Hue FileBrowser上的数据目录进行新增和删除操作,由于fayson用户无操作数据目录的权限。

5.测试总结


如果这个外部表的目录没有在cm里配置成需要sentry管理的目录,通过Sentry赋权后,是没法做ACL同步的,不建议在生产系统中这样使用。如果你需要管理外部表,那么你就需要按照之前的标准文档来操作。如何使用Sentry管理Hive外部表权限

醉酒鞭名马,少年多浮夸! 岭南浣溪沙,呕吐酒肆下!挚友不肯放,数据玩的花! 温馨提示:要看高清无码套图,请使用手机打开并单击图片放大查看。


推荐关注Hadoop实操,第一时间,分享更多Hadoop干货,欢迎转发和分享。

原创文章,欢迎转载,转载请注明:转载自微信公众号Hadoop实操