当前位置: 首页 > news >正文

使用Java连接Hbase

我在网上试 了很多代码,但是大部分都不能实现,Java连接Hbase,一直报一个错

java.util.concurrent.ExecutionException: org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /hbase/hbaseid

一直也不清楚为什么。后面各种尝试,最后尝试出来了,能正常连接hbase,但是也不知道为啥能出来,所以该代码可能只能解决我自己的问题,其他人的问题请谨慎参考,不保证准确与否,因为我也不知道为什么????

引入Jar,本公司使用的Hbase是2.0.2.1.7.x,所以我使用的Jar也是2.0.2版本 ,根据实际情况引入,然后还用了log4j2的日志

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.hbase</groupId><artifactId>hbase-operation</artifactId><version>0.0.1</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-server</artifactId><version>2.0.2</version><exclusions><exclusion><artifactId>servlet-api</artifactId><groupId>javax.servlet</groupId></exclusion><exclusion><artifactId>slf4j-log4j12</artifactId><groupId>org.slf4j</groupId></exclusion></exclusions></dependency><dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-client</artifactId><version>2.0.2</version><exclusions><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId></exclusion><exclusion><groupId>log4j</groupId><artifactId>log4j</artifactId></exclusion><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.22</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.32</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-core</artifactId><version>2.17.1</version><exclusions><exclusion><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-api</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-api</artifactId><version>2.17.1</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-slf4j-impl</artifactId><version>2.17.0</version><exclusions><exclusion><artifactId>log4j-api</artifactId><groupId>org.apache.logging.log4j</groupId></exclusion><exclusion><artifactId>log4j-core</artifactId><groupId>org.apache.logging.log4j</groupId></exclusion><exclusion><artifactId>slf4j-api</artifactId><groupId>org.slf4j</groupId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><version>3.0.0</version><configuration><archive><manifest><mainClass>com.hbase.HbaseSearch</mainClass></manifest></archive><descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs></configuration><executions><execution><id>make-assembly</id><phase>package</phase><goals><goal>single</goal></goals></execution></executions></plugin></plugins></build>
</project>

日志配置

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG"><Appenders><Console name="Console" target="SYSTEM_OUT"><PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/></Console></Appenders><Loggers><Root level="info"><AppenderRef ref="Console"/></Root></Loggers>
</Configuration>

实际代码

package com.hbase;import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.util.List;/*** @author panlf* @date 2023/11/1*/
@Slf4j
public class HbaseSearch {public static void main(String[] args) throws Exception {log.info("start111");// 创建HBase配置对象Configuration config = HBaseConfiguration.create();config.set("hbase.zookeeper.quorum", "fdp-master,fdp-slave1,fdp-slave2,fdp-slave3"); // 设置ZooKeeper地址config.set("hbase.rootdir","hdfs://fdp-master:8020/apps/hbase/data");config.set("zookeeper.znode.parent","/hbase-unsecure");// 创建HBase连接Connection connection = ConnectionFactory.createConnection(config);Admin admin = connection.getAdmin();// 获取所有表名List<TableDescriptor> tableDescriptors = admin.listTableDescriptors();for (TableDescriptor tableDescriptor : tableDescriptors) {System.out.println("表名: " + Bytes.toString(tableDescriptor.getTableName().getName()));}System.out.println("over");// 关闭资源admin.close();connection.close();}
}

以上即为解决的代码。

http://www.lryc.cn/news/242750.html

相关文章:

  • OCR是什么意思,有哪些好用的OCR识别软件?
  • Springmvc实现增删改差
  • CentOS 7 使用cJSON 库
  • Linux——使用kill结束进程并恢复进程
  • 【Linux虚拟内存的配置】
  • 基于C#实现外排序
  • HTML CSS登录网页设计
  • dos 命令 判断路径中包含某字符并移动文件
  • electron+vue3全家桶+vite项目搭建【26】electron本地安装Vue Devtool插件,安装浏览器扩展
  • Modbus TCP
  • 基于人工兔算法优化概率神经网络PNN的分类预测 - 附代码
  • 微服务学习(十二):安装Minio
  • SpringCloud实用-OpenFeign整合okHttp
  • Python 异步套接字编程
  • 今年的校招薪资真的让人咋舌!
  • debian 设置系统默认以命令行方式启动,关闭x windows
  • AMEYA360:蔡司新能源汽车解决方案驱动产业未来
  • C#面试问题整理
  • 微信小程序 基于Android的共享付费自习室座位选座系统uniAPP
  • Java中类的类型判断技巧以及没有无参构造函数时的应对策略。isInstance()方法解析
  • 基于微信小程序的员工宿舍报修系统
  • 机器学习探索计划——KNN算法流程的简易了解
  • ES6之class类
  • 17 redis集群方案
  • [数据结构]—栈和队列
  • 【GridSearch】 简单实现并记录运行效果
  • SecureCRT出现Key exchange failed.No compatible key exchange method. 错误解决方法
  • Android RGB转YUV的算法
  • Spring事务底层原理(待完善)
  • 微信小程序 修改默认单选,多选按钮样式