我有一种情况,我需要为 Java Spark 作业设置 keytab 和主体以通过代码访问 HBase。在 spark-submit 中使用 keytab 和 principal 选项是可行的,但我需要通过代码来完成。使用以下选项似乎不起作用。选项 3 出现连接关闭异常,client cannot authenticate via:[token, kerberos]前两个出现错误。第三个选项似乎很接近,但不确定连接关闭的原因。
sparkconf = new SparkConf().setAppName("Hbase With Spark")
.set("spark.kerberos.principal", principalValue)
.set("spark.kerberos.keytab", keytabFile);
选项 2:
sparkconf = new SparkConf().setAppName("Hbase With Spark")
.set("spark.yarn.principal", principalValue)
.set("spark.yarn.keytab", keytabFile);
选项 3:
conf = HBaseConfiguration.create();
conf.set("hbase.master.keytab.file",keytabFile)
conf.set("hbase.regionserver.keytab.file",keytabFile);
UserGroupInformation ugi;
ugi = UserGroupInformation.getLoginUser();
ugi.setConfiguration(conf);
ugi.loginUserFromKeytab(principalValue, keytabFile);
ugi.getLoginUser().doAs((new PrivilegedExceptionAction<SQLContext>() {
public SQLContext run() throws Exception {
sparkconf = new SparkConf().setAppName("Hbase With Spark");
jsc = new JavaSparkContext(sparkconf);
sqlContext = new org.apache.spark.sql.SQLContext(jsc);
Dataset<Row> dfEmp = sqlContext.read().format("org.apache.hadoop.hbase.spark")
.option("hbase.columns.mapping", sqlMapping)
.option("hbase.table", "employee").load();
// This is useful when issuing SQL text queries directly against the sqlContext object.
dfEmp.registerTempTable("empdata");
dfEmp.show();
return sqlContext;
}
}));