spark读取hive权限被拒绝

使用spark读取hive表时,发生如下错误:

Caused by: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.AccessControlException): Permission denied: user=xingweidong, access=READ_EXECUTE, inode="/user/hive/warehouse/ads_seer.db/user_action_from_xyio/dt=2020-12-28":hive:hive:drwxrwx--x
	at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.check(FSPermissionChecker.java:400)
	at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.checkPermission(FSPermissionChecker.java:262)
	at org.apache.sentry.hdfs.SentryINodeAttributesProvider$SentryPermissionEnforcer.checkPermission(SentryINodeAttributesProvider.java:86)

hadoop集群启用了kerberos+sentry,hive使用sentry进行权限管理,上面这个问题是用户xingweidong访问hive表ads_seer.user_action_from_xyio时,显示无权访问。
而使用hive beeline,用户是可以访问的。根据报错提示,我们可以看到,用户无权访问hive表下的文件。

对于这个问题,就涉及到spark访问hive的鉴权机制了,貌似spark访问hive需要有元数据访问权限和表文件访问权限。而此报错就是因为spark对文件没有访问权限。这种情况,只需要配置sentry就可以解决。
配置如下:
在这里插入图片描述
添加上图第二个权限配置,spark就可以正常访问hive了。
上图第一个是表权限,第二个是库权限。

数据读取正常了,但是还会报警告如下:

21/01/20 17:43:31 INFO hive.HiveMetastoreCatalog: Saving case-sensitive schema for table ads_seer.user_action_from_xyio
21/01/20 17:43:31 WARN hive.HiveMetastoreCatalog: Unable to save case-sensitive schema for table ads_seer.user_action_from_xyio
org.apache.spark.sql.AnalysisException: org.apache.hadoop.hive.ql.metadata.HiveException: Unable to alter table. User xingweidong does not have privileges for ALTERTABLE_ADDCOLS;
	at org.apache.spark.sql.hive.HiveExternalCatalog.withClient(HiveExternalCatalog.scala:108)
	at org.apache.spark.sql.hive.HiveExternalCatalog.alterTableDataSchema(HiveExternalCatalog.scala:687)
	at org.apache.spark.sql.catalyst.catalog.ExternalCatalogWithListener.alterTableDataSchema(ExternalCatalogWithListener.scala:132)
	at org.apache.spark.sql.catalyst.catalog.SessionCatalog.alterTableDataSchema(SessionCatalog.scala:386)
	at org.apache.spark.sql.hive.HiveMetastoreCatalog.updateDataSchema(HiveMetastoreCatalog.scala:264)
	at org.apache.spark.sql.hive.HiveMetastoreCatalog.org$apache$spark$sql$hive$HiveMetastoreCatalog$$inferIfNeeded(HiveMetastoreCatalog.scala:248)
	at org.apache.spark.sql.hive.HiveMetastoreCatalog$$anonfun$4$$anonfun$5.apply(HiveMetastoreCatalog.scala:167)
	at org.apache.spark.sql.hive.HiveMetastoreCatalog$$anonfun$4$$anonfun$5.apply(HiveMetastoreCatalog.scala:156)

这个警告看起来是修改表失败,也是权限问题,这个有时候会以ERROR的形式出现,主要报错信息示例如下:

User *username* does not have privileges for ALTERTABLE_ADDCOLS

这个报错主要是spark的spark.sql.hive.caseSensitiveInferenceMode配置造成的。
如下设置即可解决:

spark.conf.set("spark.sql.hive.caseSensitiveInferenceMode", "INFER_ONLY")

这个配置是spark处理hive schema大小写的策略,有三个可选值,说明如下:

选项说明
INFER_AND_SAVEspark 2.2-2.4版本的默认值。Spark推断实际文件架构并更新hive元存储中的表架构,所以需要写权限。
INFER_ONLY推断实际文件架构,但是不更新hive表属性。
NEVER_INFERspark 2.1.1,3.0版本的默认值。不推断实际文件架构。

随着spark版本的更新,这个配置的默认值从 NEVER_INFER 到 INFER_AND_SAVE 再到 NEVER_INFER 。

相关源码:

val HIVE_CASE_SENSITIVE_INFERENCE = buildConf("spark.sql.hive.caseSensitiveInferenceMode")
    .internal()
    .doc("Sets the action to take when a case-sensitive schema cannot be read from a Hive Serde " +
      "table's properties when reading the table with Spark native data sources. Valid options " +
      "include INFER_AND_SAVE (infer the case-sensitive schema from the underlying data files " +
      "and write it back to the table properties), INFER_ONLY (infer the schema but don't " +
      "attempt to write it to the table properties) and NEVER_INFER (the default mode-- fallback " +
      "to using the case-insensitive metastore schema instead of inferring).")
    .version("2.1.1")
    .stringConf
    .transform(_.toUpperCase(Locale.ROOT))
    .checkValues(HiveCaseSensitiveInferenceMode.values.map(_.toString))
    .createWithDefault(HiveCaseSensitiveInferenceMode.NEVER_INFER.toString)

参考:
https://spark.apache.org/docs/latest/sql-migration-guide.html#data-sources
https://stackoverflow.com/questions/57821080/user-does-not-have-privileges-for-altertable-addcols-while-using-spark-sql-to-re/65705509

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值