• 本机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中

    步骤三:导入相关依赖

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.apache.hbasegroupId>
    4. <artifactId>hbase-clientartifactId>
    5. <version>2.4.17version>
    6. dependency>
    7. <dependency>
    8. <groupId>org.apache.hbasegroupId>
    9. <artifactId>hbase-clientartifactId>
    10. <version>2.4.17version>
    11. dependency>
    12. dependencies>

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

    1. import org.apache.hadoop.conf.Configuration;
    2. import org.apache.hadoop.hbase.HBaseConfiguration;
    3. import org.apache.hadoop.hbase.TableName;
    4. import org.apache.hadoop.hbase.client.Admin;
    5. import org.apache.hadoop.hbase.client.Connection;
    6. import org.apache.hadoop.hbase.client.ConnectionFactory;
    7. import java.io.IOException;
    8. public class connect_test
    9. {
    10. public static Connection connection = null;
    11. public static Admin admin = null;
    12. static
    13. {
    14. try
    15. {
    16. //1、获取配置信息
    17. Configuration configuration = HBaseConfiguration.create();
    18. configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/hbase");
    19. configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
    20. //2、创建连接对象
    21. connection = ConnectionFactory.createConnection(configuration);
    22. //3、创建Admin对象
    23. admin = connection.getAdmin();
    24. }
    25. catch (IOException e)
    26. {
    27. e.printStackTrace();
    28. }
    29. }
    30. //判断表是否存在
    31. public static boolean isTableExist(String tableName) throws IOException
    32. {
    33. boolean exists = admin.tableExists(TableName.valueOf(tableName));
    34. return exists;
    35. }
    36. public static void close()
    37. {
    38. if (admin != null)
    39. {
    40. try
    41. {
    42. admin.close();
    43. }
    44. catch (IOException e)
    45. {
    46. e.printStackTrace();
    47. }
    48. }
    49. if (connection != null)
    50. {
    51. try
    52. {
    53. connection.close();
    54. }
    55. catch (IOException e)
    56. {
    57. e.printStackTrace();
    58. }
    59. }
    60. }
    61. public static void main(String[] args) throws IOException
    62. {
    63. //测试hbase是否存在名为test的表
    64. System.out.println(isTableExist("test"));
    65. //关闭资源
    66. close();
    67. }
    68. }

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

    1. configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/hbase");
    2. configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");

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

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

  • 相关阅读:
    The import xxx.xxx.xxxx is never used
    vscode如何格式化超大型JS代码
    httpclient用法大全
    C#: 根据url获取https的的证书有效期
    【学习笔记35】JavaScript计算两个指定日期的时间差
    计算机毕设(附源码)JAVA-SSM基于Java的医疗器械销售系统
    (VS报错)已在 xxxxx.exe 中执行断点指令(__debugbreak()语句或类似调用)-解决方法&&C++创建对象四种方式
    实战+代码!Selenium + Phantom JS爬取天天基金数据
    常用网络协议的学习
    第56篇-某创网加速乐cookie值分析【2023-09-15】
  • 原文地址:https://blog.csdn.net/qq_62768655/article/details/134542599