1、用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();
    }
}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐