社区原文 “Connecting to HBase in a Kerberos Enabled Cluster”
讲授如何通过 Java 或 Scala 在启用 Kerberos 的群会合毗连到 HBase。
本测试需要一个启用了kerberos的HDP集群。集群搭建参考《Ambari在当地VM(centos7.3)陈设hadoop集群》。本测试在HDP集群的C7302节点(centos7.3)长举办。首先,昆山软件开发,下载java样例代码:
$ cd /opt $ git clone https://github.com/wbwangk/hdp-test-examples
这个github库是从jjmeyer0/hdp-test-examples
库fork的。主要修改有:
src/main/java/com/jj/hbase/HBaseClient.java
中 jj 用户主体为 jj@AMBAR.APACHE.ORGI
建设keytab
在 c7302 节点用打点员账号登录 KDC,然后建设叫jj的主体,昆山软件开发,并导出 keytab:
$ kinit root/admin@AMBARI.APACHE.ORG $ kadmin -q "addprinc jj" (建设jj主体,需要输入两次暗码,暗码是1) $ ktutil ktutil: addent -password -p jj -k 1 -e RC4-HMAC Password for jj@AMBARI.APACHE.ORG: 1 ktutil: wkt jj.keytab (生成了keytab文件) ktutil: q $ scp jj.keytab /opt/hdp-test-examples/src/main/resources
hbase用户">筹备HBase用户
jj 用户必需在 HBase 中得到正确的权限。Ambari 为 HBase建设一个打点员用户,昆山软件公司,通过 keytab 查找打点员用户主体。并操作它登录,操作密钥文件登录不需要暗码:
$ klist -kt /etc/security/keytabs/hbase.headless.keytab (查察hbase处事的printcipal ) KVNO Timestamp Principal ---- ------------------- ------------------------------------------------------ 1 07/06/2017 03:53:35 hbase-hdp2610@AMBARI.APACHE.ORG $ kinit -kt /etc/security/keytabs/hbase.headless.keytab hbase-hdp2610 (实测只能用这个主体登录,纵然root/admin主体都不可) $ hbase shell hbase(main):001:0> grant 'jj','RW'
筹备设置文件
运行例子需要的文件有三个:
由于利用HDP集群的节点充当客户机,所以直接在本节点复制文件即可:
$ scp /etc/hbase/conf/hbase-site.xml /opt/htp-test-examples/src/main/resources/ $ scp /etc/krb5.conf /opt/htp-test-examples/src/main/resources/
对付测试,发起在 hbase-site.xml 中变动 “hbase.client.retries.number” 属性。默认环境下为35。这个“重试次数”这在运行测试时太大了,复制后可以修改为3。
其它修改
目次/opt/hdp-test-examples/src`下有两个目次:`main`和`test`。`main`目次安排客户端措施,而`test`目次是单位测试目次。 来到目次`/opt/hdp-test-examples/src/test/java/com/jj
下看看,发明除了hbase尚有个pig目次。假如只是测试java客户端毗连hbase,发起删除pig目次。不然在maven构建是也会执行pig的单位测试,而由于没有正确设置pig,导致一定堕落使构建失败。
代码讲授
例子的 Java 代码位于 src/main/java/com/jj/hbase/HBaseClient.java
。在代码中,首先需要做的是建设和加载 HBase 设置:
// Setting up the HBase configuration Configuration configuration = new Configuration(); configuration.addResource("src/main/resources/hbase-site.xml");
接下来指向 krb5.conf 文件并配置 Kerberos 主体和 keytab。
// Point to the krb5.conf file. System.setProperty("java.security.krb5.conf", "src/main/resources/krb5.conf"); System.setProperty("sun.security.krb5.debug", "true"); // Override these values by setting -DkerberosPrincipal and/or -DkerberosKeytab String principal = System.getProperty("kerberosPrincipal", "jj@AMBARI.APACHE.ORG"); String keytabLocation = System.getProperty("kerberosKeytab", "src/main/resources/jj.keytab");
此刻利用上面界说的主键和 keytab 登录。
UserGroupInformation.setConfiguration(configuration); UserGroupInformation.loginUserFromKeytab(principal, keytabLocation)
Maven构建、测试
$ cd /opt/hdp-test-examples $ mvn clean test -P hdp-2.6.1 (假如网络差则耗时较长) [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building hdp-test-examples 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ hdp-test-examples --- [INFO] Deleting /opt/hdp-test-examples/target [INFO] [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ hdp-test-examples --- [debug] execute contextualize [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 10 resources [INFO] [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ hdp-test-examples --- [INFO] Compiling 5 source files to /opt/hdp-test-examples/target/classes [INFO] [INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ hdp-test-examples --- [debug] execute contextualize [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ hdp-test-examples --- [INFO] Compiling 1 source file to /opt/hdp-test-examples/target/test-classes [INFO] [INFO] --- maven-surefire-plugin:2.10:test (default-test) @ hdp-test-examples --- [INFO] Surefire report directory: /opt/hdp-test-examples/target/surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.jj.hbase.HBaseClientTest Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.552 sec Results : Tests run: 4, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 5.145s [INFO] Finished at: Wed Jul 19 07:19:34 UTC 2017 [INFO] Final Memory: 38M/91M [INFO] ------------------------------------------------------------------------