HbaseConnKer.java
1 package BigData.conn; 2 3 import BigData.utils.resource.ResourcesUtils; 4 import org.apache.hadoop.conf.Configuration; 5 import org.apache.hadoop.hbase.HBaseConfiguration; 6 import org.apache.hadoop.hbase.client.Connection; 7 import org.apache.hadoop.hbase.client.ConnectionFactory; 8 import org.apache.hadoop.security.UserGroupInformation; 9 10 import java.io.IOException; 11 import java.io.InputStream; 12 import java.nio.file.Files; 13 import java.nio.file.Paths; 14 import java.util.Properties; 15 16 public class HbaseConnKer { 17 18 private static Connection connectionKer = null; 19 20 private static volatile HbaseConnKer instanceKer; 21 22 private HbaseConnKer() { 23 try { 24 Properties pro = ResourcesUtils.getResourceAsProperties("Kerberos/HbaseConfigKer.properties"); 25 26 /* 这个配置文件主要是记录 kerberos的相关配置信息,例如KDC是哪个IP?默认的realm是哪个? 27 这个文件是从远程服务器上copy下来的*/ 28 System.setProperty("java.security.krb5.conf", pro.getProperty("java.security.krb5.conf")); 29 30 Configuration conf = HBaseConfiguration.create(); 31 32 // -----Kerberos配置----- 33 InputStream hbaseSiteInputStream = Files.newInputStream(Paths.get(pro.getProperty("hbase.site"))); 34 if (hbaseSiteInputStream == null) { 35 System.out.println("The hbase-site.xml is null!"); 36 } 37 conf.addResource(hbaseSiteInputStream); 38 conf.set("hadoop.security.authentication", pro.getProperty("hadoop.security.authentication")); 39 40 // 这个可以理解成用户名信息,也就是Principal 41 conf.set("kerberos.principal", pro.getProperty("kerberos.principal")); 42 43 UserGroupInformation.setConfiguration(conf); 44 UserGroupInformation.loginUserFromKeytab(pro.getProperty("kerberos.principal"), 45 pro.getProperty("kerberos.keytab")); 46 // ---------- 47 48 //显示连接的服务器 49 System.out.println("连接服务器:"+conf.get("hbase.zookeeper.quorum")); 50 51 connectionKer = ConnectionFactory.createConnection(conf); 52 System.out.println("创建connection成功!"); 53 } catch (IOException e) { 54 e.printStackTrace(); 55 } 56 57 } 58 59 public static Connection getConnection() { 60 return connectionKer; 61 } 62 63 public static HbaseConnKer getIstance() { 64 // 定义一个共有的静态方法,返回该类型实例 65 if (instanceKer == null) { 66 // 对象实例化时与否判断(不使用同步代码块,instance不等于null时,直接返回对象,提高运行效率) 67 synchronized (HbaseConnKer.class) { 68 // 同步代码块(对象未初始化时,使用同步代码块,保证多线程访问时对象在第一次创建后,不再重复被创建) 69 if (instanceKer == null) { 70 // 未初始化,则初始instance变量 71 instanceKer = new HbaseConnKer(); 72 } 73 } 74 } 75 return instanceKer; 76 } 77 }
HbaseConfigKer.properties
# HBase config
hbase.zookeeper.quorum=20.1.1.93
hbase.zookeeper.property.clientPort=2181
java.security.krb5.conf=src/main/resources/Kerberos/krb5.conf
hadoop.security.authentication=kerberos
hbase.site=src/main/resources/Kerberos/hbase-site.xml
kerberos.keytab=src/main/resources/Kerberos/XXX.keytab
[email protected]