0


SpringBoot集成Hbase

Springboot整合HBase数据库
1、增加依赖

<dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-client</artifactId><version>2.5.8</version></dependency>

2、增加配置文件

hbase:
  quorum: 192.168.*.* //自己的zookeeper地址
  client-port: 2181
  master: 192.168.*.*:16010

3、连接Hbase库的工具类

packagecom.xxx.xxx.util;importjakarta.annotation.PostConstruct;importlombok.extern.slf4j.Slf4j;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.HBaseConfiguration;importorg.apache.hadoop.hbase.TableName;importorg.apache.hadoop.hbase.client.*;importorg.apache.hadoop.hbase.util.Bytes;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.stereotype.Component;importjava.io.IOException;/**
 * @description: HBase数据库操作工具类
 */@Component@Slf4jpublicclassHbaseUtil{@Value("${hbase.master}")privateString master;@Value("${hbase.client-port}")privateString clientPort;@Value("${hbase.quorum}")privateString quorum;privateConnection connection;@PostConstructprivatevoidinit(){try{// Hadoop配置对象,用于创建HBase配置Configuration configuration =HBaseConfiguration.create();
            configuration.set("hbase.zookeeper.property.clientPort", clientPort);
            configuration.set("hbase.zookeeper.quorum", quorum);// 设置zookeeper的主机
            configuration.set("hbase.master", master);
            connection =ConnectionFactory.createConnection(configuration);}catch(Exception ex){
            log.error("初始化异常");
            ex.printStackTrace();}}/**
     * 插入数据
     * @param tableName
     * @param rowKey
     * @param columnFamily
     * @param value
     */publicvoidput(String tableName,String rowKey,String columnFamily,String value){// 获取指定表的Table对象try(Table table = connection.getTable(TableName.valueOf(tableName))){// 创建Put对象,设置行键Put put =newPut(Bytes.toBytes(rowKey));// 设置列族、列和值
            put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(rowKey),Bytes.toBytes(value));}catch(IOException io){
            log.error("插入数据时发生异常:"+ io);}}/**
     * 获取数据
     * @param tableName
     * @param rowKey
     * @param columnFamily
     * @param columnQualifier
     * @return String
     * @throws DqmException
     */publicStringget(String tableName,String rowKey,String columnFamily,String columnQualifier){String resultValue ="";// 获取指定表的Table对象try(Table table = connection.getTable(TableName.valueOf(tableName))){// 创建Get对象,设置行键Get get =newGet(Bytes.toBytes(rowKey));Result result = table.get(get);// 获取指定列的值byte[] valueBytes = result.getValue(Bytes.toBytes(columnFamily),Bytes.toBytes(columnQualifier));// 将值转换为字符串并返回
            resultValue =Bytes.toString(valueBytes);}catch(IOException io){
            log.error("插入数据时发生异常:"+ io);}return resultValue;}}

4、Hbase中增加数据
Hbase数据库简单操作:

Hbase数据库简单操作:
1、创建数据库表的命令:create ‘表名’, ‘列族名1’,‘列族名2’,‘列族名N’
2、 查看表的结构,命令:describe ‘表名’或desc ‘表名’
3、 Hbase 增加一行数据: put ’表名’, ‘rowKey’, ’列族:列’ , ‘值’ eg:put ‘test_table’, ‘0001’, ‘data:name’,‘lisi’
4、 Hbase 查询数据: scan ’表名’来查看表的所有记录,{参数key=>参数value}
5、 Hbase 获取数据: get ‘表名’ , ‘rowKey’ 来查看某个rowKey下的所有记录
6、 Hbase 删除数据单元格: delete ’表名’,’行名’,’列族:列’ 来删除某个记录
eg:delete ‘Student’,‘0001’,‘StuInfo:Name’

# 相应实际操作
hbase:008:0> create 'test_table','data','text'
Created table test_table
Took 1.3111 seconds
=> Hbase::Table - test_table
hbase:009:0> list
TABLE
test_table
1 row(s)
Took 0.0435 seconds
=>["test_table"]
hbase:010:0> put 'test_table', '0001', 'data:name','lisi'
Took 0.2450 seconds
hbase:011:0> scan 'test_table'
ROW                                                             COLUMN+CELL
 0001                                                           column=data:name, timestamp=2024-07-05T15:31:48.082, value=lisi
1 row(s)
Took 0.0811 seconds

5、调用get方法获取数据

importjakarta.annotation.Resource;importlombok.extern.slf4j.Slf4j;importcom.xxx.xxx.HbaseUtil;@Slf4j@ServicepublicclassTest{@ResourceprivateHbaseUtil hbaseUtil;publicStringgetHbaseData(){String result = hbaseUtil.get("test_table","0001","data","name");
        log.info("数据结果: "+ result);return result;}}// 运行结果2024-07-05T17:22:21.944+08:00INFO26540---[dqm][io-12040-exec-1]c.c.d.service.Test: 数据结果: lisi

6、遇到的问题

问题1、 java.io.FileNotFoundException HADOOP_HOME and hadoop.home.dir are unset

  1. 如果是windows环境,需要配置win客户端的hadoop环境(必须要配置,否则会出错)

问题2、java.net.ConnectException: Connection refused: no further information

原因是zookeeper连接失败,可能原因有:

  1. 网络问题,ping 地址看是否能通,最好直接用telnet ip : 2181,
  2. 防火墙问题,看服务器防火请是否关闭
# 查看防火墙状态sudo firewall-cmd --state# 关闭防火墙:sudo systemctl stop firewalld
# 禁止开机启动sudo systemctl disable firewalld
  1. 我在做测试的时候使用的Hbase内置的zookeeper,一直telent不同,后面单独部署了zookeeper替换了Habse自带的zookeeper后解决。

本文转载自: https://blog.csdn.net/saonian0929/article/details/140213861
版权归原作者 街角等待 所有, 如有侵权,请联系我们删除。

“SpringBoot集成Hbase”的评论:

还没有评论