在本地用idea连接虚拟机上的hbase集群的实现代码
轻描丶幸福的旋律 人气:01、用maven添加依赖(看清自己hbase版本)
<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.3.1</version> </dependency>
2、将虚拟机上的hbase-site.xml文件放到resourcs目录下
3、修改本机的hosts文件(在C:\Windows\System32\drivers\etc下)
添加集群的IP 名称
192.168.124.116 Master
192.168.124.115 Slave1
192.168.124.130 Slave2
4、代码举例,判断表是否存在
package com.zyb.test; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import java.io.IOException; public class TestDemo { public static Connection connection=null; public static Admin admin=null; static { try { //1、获取配置信息 Configuration configuration = HBaseConfiguration.create(); configuration.set("hbase.rootdir", "hdfs://192.168.124.116:9000/HBase"); configuration.set("hbase.zookeeper.quorum","Master,Slave1,Slave2"); //2、创建连接对象 connection= ConnectionFactory.createConnection(configuration); //3、创建Admin对象 admin = connection.getAdmin(); } catch (IOException e) { e.printStackTrace(); } } //判断表是否存在 public static boolean isTableExiat(String tableName) throws IOException { boolean exists = admin.tableExists(TableName.valueOf(tableName)); return exists; } public static void close(){ if (admin!=null){ try { admin.close(); } catch (IOException e) { e.printStackTrace(); } } if (connection!=null){ try { connection.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) throws IOException { System.out.println(isTableExiat("student")); //关闭资源 close(); } }
加载全部内容