当配置 org.apache.hadoop.hive.ql.security.authorization.DefaultHivemetastoreAuthorizationProvider 时,配置文档参考 Hive metaStore 认证和权限模型。第一个角色怎么创建的,用哪个账号连接,用什么命令创建成了问题。
配置 hive.users.in.admin.role在 hive-site.xml,通过配置 hive.users.in.admin.role,这些用户自动有管理员权限。
启动 metastore, HiveServer2 使用 houzhizhen 账号以 beeline 连接 HiveServer2hive.users.in.admin.role houzhizhen,admin Comma separated list of users who are in admin role for bootstrapping. More users can be added in ADMIN role later.
发现连不上,因为默认用户 houzhizhen 没有 default 数据库的 select 权限。日志如下:
22/02/17 15:59:20 [main]: WARN jdbc.HiveConnection: Failed to connect to localhost.localdomain:10000
22/02/17 15:59:20 [main]: ERROR jdbc.Utils: Unable to read HiveServer2 configs from ZooKeeper
Error: Could not open client transport for any of the Server URI's in ZooKeeper: Failed to open new session: org.apache.hadoop.hive.ql.metadata.HiveException: metaException(message:No privilege 'Select' found for inputs { database:default}) (state=08S01,code=0)
Beeline version 3.1.2 by Apache Hive
分配 houzhizhen 的 default 账号的权限
使用 houzhizhen 账号启动 hive。执行以下命令。
hive> set role admin; OK Time taken: 4756.067 seconds hive> grant all on database default to user houzhizhen;再次使用 houzhizhen 账号以 beeline 连接 HiveServer2
现在成功连接。
22/02/17 17:01:27 [main]: INFO jdbc.HiveConnection: Connected to localhost.localdomain:10000 Connected to: Apache Hive (version 3.1.2) Driver: Hive JDBC (version 3.1.2) Transaction isolation: TRANSACTION_REPEATABLE_READ Beeline version 3.1.2 by Apache Hive 0: jdbc:hive2://localhost:2181/default>代码分析 HivemetaStore$HMSHandler#init
init 里有一个同步块里。
synchronized (HMSHandler.class) {
if (currentUrl == null || !currentUrl.equals(metaStoreInit.getConnectionURL(conf))) {
createDefaultDB();
createDefaultRoles();
addAdminUsers();
currentUrl = metaStoreInit.getConnectionURL(conf);
}
createDefaultRoles
createDefaultRoles 执行以下操作:
- 创建 role admin,owner: admin.创建 role public, owner: public.把GLOBAL 分配给角色 admin
从 配置项 hive.users.in.admin.role 取出用户名,分配角色 admin,这样这些用户就有 admin 权限,但是不一定有 select 权限,需要自己给自己赋权。



