Hbaseconnker.java
1 PackageBigdata.conn;2 3 ImportBigData.utils.resource.ResourcesUtils;4 Importorg.apache.hadoop.conf.Configuration;5 Importorg.apache.hadoop.hbase.HBaseConfiguration;6 Importorg.apache.hadoop.hbase.client.Connection;7 Importorg.apache.hadoop.hbase.client.ConnectionFactory;8 Importorg.apache.hadoop.security.UserGroupInformation;9 Ten Importjava.io.IOException; One ImportJava.io.InputStream; A ImportJava.nio.file.Files; - Importjava.nio.file.Paths; - Importjava.util.Properties; the - Public classHbaseconnker { - - Private StaticConnection Connectionker =NULL; + - Private Static volatileHbaseconnker Instanceker; + A PrivateHbaseconnker () { at Try { -Properties Pro = Resourcesutils.getresourceasproperties ("Kerberos/hbaseconfigker.properties"); - - /*This configuration file mainly records the relevant configuration information of Kerberos, such as which IP is the KDC? Which realm is the default? - This file was copied from the remote server.*/ -System.setproperty ("java.security.krb5.conf", Pro.getproperty ("java.security.krb5.conf")); in -Configuration conf =hbaseconfiguration.create (); to + //-----Kerberos Configuration----- -InputStream Hbasesiteinputstream = Files.newinputstream (Paths.get (Pro.getproperty ("Hbase.site")))); the if(Hbasesiteinputstream = =NULL) { *System.out.println ("The Hbase-site.xml is null!"); $ }Panax Notoginseng Conf.addresource (hbasesiteinputstream); -Conf.set ("Hadoop.security.authentication", Pro.getproperty ("Hadoop.security.authentication")); the + //This can be understood as user name information, which is principal AConf.set ("Kerberos.principal", Pro.getproperty ("Kerberos.principal")); the + usergroupinformation.setconfiguration (conf); -Usergroupinformation.loginuserfromkeytab (Pro.getproperty ("Kerberos.principal"), $Pro.getproperty ("Kerberos.keytab")); $ // ---------- - - //Show connected Servers theSYSTEM.OUT.PRINTLN ("Connect server:" +conf.get ("Hbase.zookeeper.quorum"))); - WuyiConnectionker =connectionfactory.createconnection (conf); theSYSTEM.OUT.PRINTLN ("Create connection Success! "); -}Catch(IOException e) { Wu e.printstacktrace (); - } About $ } - - Public StaticConnection getconnection () { - returnConnectionker; A } + the Public StaticHbaseconnker getistance () { - //defines a common static method that returns an instance of the type $ if(Instanceker = =NULL) { the //when an object is instantiated (without using a synchronous block of code, instance is not equal to NULL, the object is returned directly, improving operational efficiency) the synchronized(Hbaseconnker.class) { the //Synchronizing code blocks (when an object is not initialized, use a synchronous block of code to guarantee that the object will not be created again after the first creation of the multi-threaded access) the if(Instanceker = =NULL) { - //Uninitialized , the initial instance variable inInstanceker =NewHbaseconnker (); the } the } About } the returnInstanceker; the } the}
Hbaseconfigker.properties
# HBase Confighbase.zookeeper.quorum=20.1.1.93hbase.zookeeper.property.clientport=2181java.security.krb5.conf= src/main/resources/kerberos/krb5.confhadoop.security.authentication=kerberoshbase.site=src/main/resources/ Kerberos/hbase-site.xmlkerberos.keytab=src/main/resources/kerberos/xxx.keytab[email protected]
HBase Java API remote Access Kerberos authentication