本机idea连接虚拟机中的Hbase

相关环境:

虚拟机:Centos7

hadoop版本:3.1.3 hbase版本:2.4.11 zookeeper版本:3.5.7

Java IDE:IDEA JDK:8

步骤

步骤一:在idea创建一个maven项目

步骤二:在虚拟机里找到core-site.xml和hbase-site.xml这两个文件

我的core-site.xml文件是在hadoop安装目录下的etc/hadoop中

hbase-site.xml文件在hbase安装目录下的conf中

找到后下载下来,然后放到idea的src/main/resources中

步骤三:导入相关依赖

XML 复制代码
<dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-server -->
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>2.4.17</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>2.4.17</version>
        </dependency>
</dependencies>

步骤四:写一个测试类测试一下

java 复制代码
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 connect_test
{
    public static Connection connection = null;
    public static Admin admin = null;

    static
    {
        try
        {
            //1、获取配置信息
            Configuration configuration = HBaseConfiguration.create();
            configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/hbase");
            configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
            //2、创建连接对象
            connection = ConnectionFactory.createConnection(configuration);
            //3、创建Admin对象
            admin = connection.getAdmin();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    //判断表是否存在
    public static boolean isTableExist(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
    {
        //测试hbase是否存在名为test的表
        System.out.println(isTableExist("test"));
        //关闭资源
        close();
    }
}

比较关键的是下面这两个配置信息

java 复制代码
configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/hbase");
configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
            

这两个信息可以在hbase-site.xml中可以找到

然后运行测试,结果为true,说明hbase中存在表为test的表。测试成功,成功连接hbase

相关推荐
Lubase19 分钟前
LuBase介绍&私有化部署教程
java·spring boot·低代码·gitee·vue
虫小宝30 分钟前
淘宝返利app多数据源设计:基于MyCat的分库分表与读写分离
java
sheji341631 分钟前
【开题答辩全过程】以 基于JSP的汽车租赁管理系统为例,包含答辩的问题和答案
java·开发语言·汽车
wen__xvn37 分钟前
C++ 中 std::set 的用法
java·c++·c#
多米Domi01137 分钟前
0x3f 第21天 三更java进阶1-35 hot100普通数组
java·python·算法·leetcode·动态规划
步步为营DotNet1 小时前
深深度探索.NET 中HttpClient的复用策略:提升性能与稳定性的关键度解析.NET 中IServiceCollection:构建可扩展服务体系的关键
java·网络·.net
牛马1111 小时前
WidgetsFlutterBinding.ensureInitialized()在 Flutter Web 端启动流程的影响
java·前端·flutter
宠友信息1 小时前
面向多端部署的社区平台技术方案:uniapp 与java微服务架构的工程化实践
java·微服务·微信·架构·uni-app·springboot
YanDDDeat2 小时前
Prometheus + Grafana 搭建应用监控体系
java·后端·eureka·grafana·prometheus