Java连接Hbase的项目实践
作者:枫叶梨花
HBase是基于Hadoop的高可靠、高性能、面向列的NoSQL数据库,它提供了对海量数据的存储和实时读写的能力,本文主要介绍了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(); } }
到此这篇关于Java连接Hbase的项目实践的文章就介绍到这了,更多相关Java连接Hbase内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!