本测试需要一个启用了 kerberos 的 HDP 集群。首先,下载 java 样例代码:
$ cd /opt
$ git clone https://github.com/jjmeyer0/hdp-test-examples
用管理员账号登录 KDC,然后创建叫 webb 的主体,并导出 keytab:
$ kinit root/admin@AMBARI.APACHE.ORG
$ kadmin -q "addprinc webb" (创建webb主体,需要输入两次密码,密码是1)
$ ktutil
ktutil: addent -password -p webb -k 1 -e RC4-HMAC
Password for webb@AMBARI.APACHE.ORG: 1
ktutil: wkt webb.keytab
ktutil: q
$ scp webb.keytab root@u1403:/etc/security/keytabs/ (把生成的keytab复制到hbase节点)
回到 hbase 所在的 u1403 节点。webb 用户必须在 HBase 中获得正确的权限。要做到这一点,请执行以下操作:
$ kinit -kt /etc/security/keytabs/hbase.headless.keytab hbase-hdp1 (实测只能用这个主体登录,即使root/admin主体都不行)
$ hbase shell
hbase(main):001:0> grant 'webb','RW'
运行例子需要的文件有三个:
$ cp /etc/hbase/conf/hbase-site.xml /opt/htp-test-examples/src/main/resources/
$ cp /etc/security/keytabs/webb.keytab /opt/htp-test-examples/src/main/resources/
$ cp /etc/krb5.conf /opt/htp-test-examples/src/main/resources/
对于测试,建议在 hbase-site.xml 中更改 hbase.client.retries.number 属性。默认情况下为35.这在运行某些测试时相当高。
例子的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", "webb@AMBARI.APACHE.ORG");
String keytabLocation = System.getProperty("kerberosKeytab", "src/main/resources/webb.keytab");
现在使用上面定义的主键和 keytab 登录。
UserGroupInformation.setConfiguration(configuration);
UserGroupInformation.loginUserFromKeytab(principal, keytabLocation)
用 maven 构建:
$ cd /opt/hdp-test-examples
$ mvn clean test -P hdp-2.4.2 (耗时较长)
Tests in error:
calculatingBuildingAverageShouldProperlyStoreAverage(com.jj.pig.BuildingAvgPigTest): Unable to open iterator for alias building_avg
makeSureTestDataFromFileProperlyProducesAverage(com.jj.pig.BuildingAvgPigTest): Unable to open iterator for alias building_avg
....
[ERROR] Please refer to /opt/hdp-test-examples/target/surefire-reports for the individual test results.
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.10:test (default-test) on project hdp-test-examples: There are test failures.
Please refer to /opt/hdp-test-examples/target/surefire-reports for the individual test results.
....
这里是 完整代码( github /jjmeyer0/hdp-test-examples)。

