FiHBaseConnectionServiceImpl.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package com.primeton.damp.bigdata;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.hbase.HBaseConfiguration;
  5. import org.apache.hadoop.hbase.TableName;
  6. import org.apache.hadoop.hbase.client.Admin;
  7. import org.apache.hadoop.hbase.client.Connection;
  8. import org.apache.hadoop.hbase.client.ConnectionFactory;
  9. import org.apache.hadoop.hbase.client.Table;
  10. import org.apache.hadoop.hbase.exceptions.HBaseException;
  11. import java.io.Closeable;
  12. import java.io.IOException;
  13. import java.util.Properties;
  14. import java.util.Set;
  15. /**
  16. *
  17. * 华为 FI HBASE 认证方式获得连接
  18. *
  19. *
  20. * <pre>
  21. *
  22. * Created by zhaopx.
  23. * User: zhaopx
  24. * Date: 2020/4/22
  25. * Time: 18:02
  26. *
  27. * </pre>
  28. *
  29. * @author zhaopx
  30. */
  31. @Slf4j
  32. public class FiHBaseConnectionServiceImpl implements HBaseConnectionService, Closeable {
  33. private static final String ZOOKEEPER_DEFAULT_LOGIN_CONTEXT_NAME = "Client";
  34. private static final String ZOOKEEPER_SERVER_PRINCIPAL_KEY = "zookeeper.server.principal";
  35. private static final String ZOOKEEPER_DEFAULT_SERVER_PRINCIPAL = "zookeeper/hadoop.hadoop.com";
  36. /**
  37. * Hive 数据源
  38. */
  39. final Properties hbaseResource;
  40. /**
  41. * HBase 链接
  42. */
  43. Connection connection;
  44. public FiHBaseConnectionServiceImpl(Properties hbaseResource) {
  45. this.hbaseResource = hbaseResource;
  46. }
  47. @Override
  48. public boolean doAuth() {
  49. //KrbUser = "hadoop/cdh-node1@HADOOP.COM";
  50. AuthPrincipalCreator authPrincipalCreator = AuthPrincipalCreator.useExtractorConf(hbaseResource.getProperty("authBasePath"));
  51. Set<String> principals = authPrincipalCreator.listPrincipals();
  52. log.info("find existed principals: {}", principals);
  53. String authUser = hbaseResource.getProperty("authUser");
  54. AuthPrincipal kerberosPrincipal = authPrincipalCreator.getKerberosPrincipal(authUser);
  55. String userKeytab = kerberosPrincipal.getUserKeytabFile().getAbsolutePath();
  56. String krb5File = kerberosPrincipal.getKrb5File().getAbsolutePath();
  57. String krbUser = kerberosPrincipal.getPrincipal();
  58. // 分别加载 core、hdfs、hbase site 文件
  59. Configuration conf = new Configuration();
  60. try {
  61. if (kerberosPrincipal.getCoreSite() != null) {
  62. conf.addResource(kerberosPrincipal.getCoreSite().toURL());
  63. log.info("add config: {}", kerberosPrincipal.getCoreSite().getAbsolutePath());
  64. }
  65. if (kerberosPrincipal.getHdfsSite() != null) {
  66. conf.addResource(kerberosPrincipal.getHdfsSite().toURL());
  67. log.info("add config: {}", kerberosPrincipal.getHdfsSite().getAbsolutePath());
  68. }
  69. conf.reloadConfiguration();
  70. Configuration hbaseConf = HBaseConfiguration.create(conf);
  71. if (kerberosPrincipal.getHBaseSite() != null) {
  72. hbaseConf.addResource(kerberosPrincipal.getHBaseSite().toURL());
  73. log.info("add config: {}", kerberosPrincipal.getHBaseSite().getAbsolutePath());
  74. }
  75. hbaseConf.reloadConfiguration();
  76. /*
  77. * Huawei Fi Hbase,认证
  78. *
  79. * if need to connect zk, please provide jaas info about zk. of course,
  80. * you can do it as below:
  81. * System.setProperty("java.security.auth.login.config", confDirPath +
  82. * "jaas.conf"); but the demo can help you more : Note: if this process
  83. * will connect more than one zk cluster, the demo may be not proper. you
  84. * can contact us for more help
  85. */
  86. LoginUtil.setJaasConf(ZOOKEEPER_DEFAULT_LOGIN_CONTEXT_NAME, krbUser, userKeytab);
  87. LoginUtil.setZookeeperServerPrincipal(ZOOKEEPER_SERVER_PRINCIPAL_KEY,
  88. ZOOKEEPER_DEFAULT_SERVER_PRINCIPAL);
  89. LoginUtil.login(krbUser, userKeytab, krb5File, hbaseConf);
  90. connection = ConnectionFactory.createConnection(hbaseConf);
  91. log.info("fi hbase kerberos 认证通过。");
  92. return true;
  93. } catch (Exception e) {
  94. throw new SecurityException(e);
  95. }
  96. }
  97. @Override
  98. public Admin getConnection() throws HBaseException {
  99. try {
  100. return connection.getAdmin();
  101. } catch (Exception e) {
  102. throw new HBaseException("连接 HBase 异常。", e);
  103. }
  104. }
  105. @Override
  106. public Table getTable(String tableName) throws HBaseException {
  107. try {
  108. return connection.getTable(TableName.valueOf(tableName));
  109. } catch (IOException e) {
  110. throw new HBaseException("无法获取Hbase " + tableName + " 表链接。", e);
  111. }
  112. }
  113. @Override
  114. public void close() throws IOException {
  115. log.info("关闭 HBase 连接。");
  116. if(connection != null) {
  117. connection.close();
  118. }
  119. }
  120. }