连接条件:
1、所有虚拟机上运行hadoop集群、运行zookeeper进程守护
2、向项目中导入
即hbase安装目录下的conf文件夹中的两个文件
3、添加maven依赖
<dependencies> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>0.96.1-hadoop2</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>2.1.0</version> </dependency> </dependencies>
或添加/hbase/lib下的所有jar包以及/hbase/lib/client-facing-thirdparty下所有jar包
4、运行代码
package Hbase.Connector; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; public class Connector { private Connection connection=null; private Admin admin=null; public Connector(){ try{ Configuration configuration = HBaseConfiguration.create(); configuration.set("hbase.rootdir", "hdfs://node1.itcast.cn:8020/hbase"); configuration.set("hbase.zookeeper.quorum","node1,node2,node3"); //与hbase-site.xml里的配置相关 //2、创建连接对象 connection= ConnectionFactory.createConnection(configuration); //3、创建Admin对象 admin = connection.getAdmin(); System.out.println("连接成功::"+connection); }catch (IOException e){ //e.printStackTrace(); System.out.println("连接失败"); } } public void Close(){ if(connection!=null){ try { connection.close(); } catch (IOException e) { throw new RuntimeException(e); } } if(admin!=null){ try{ admin.close(); }catch (IOException e) { throw new RuntimeException(e); } } } }
标签:java,20230922,hadoop,connection,import,apache,org,HBASE,hbase From: https://www.cnblogs.com/xxaxf/p/17723680.html