springboot整合hbase-client,实现put,query,delete,exists

在这里插入图片描述

HBase是一个构建在HDFS之上的、分布式的、支持多版本的NoSql数据库。它也是Google BigTable的开源实现。HBase非常适合于对海量数据进行实时随机读写。HBase中的一张表能够支撑数十亿行和数百万列。

1.环境

本例使用hbase-client也就是hbase的原生客户端去操作hbase,所需环境有:

  1. jdk1.8
  2. springboot2.3.4
  3. 三台Hadoop机器构建的hadoop集群,也可以使用单机

项目目录如下:
在这里插入图片描述

2.添加maven依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
	<exclusions>
		<exclusion>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
		</exclusion>
	</exclusions>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<optional>true</optional>
</dependency>

<dependency>
	<groupId>org.apache.hbase</groupId>
	<artifactId>hbase-client</artifactId>
	<version>2.3.1</version>
	<exclusions>
		<exclusion>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
		</exclusion>
	</exclusions>
</dependency>

3.配置文件

server.port=18189

server.undertow.max-http-post-size=
# 设置IO线程数, 它主要执行非阻塞的任务,它们会负责多个连接, 默认设置每个CPU核心一个线程
server.undertow.threads.io=8
# 阻塞任务线程池, 当执行类似servlet请求阻塞操作, undertow会从这个线程池中取得线程,它的值设置取决于系统的负载
server.undertow.threads.worker=1000
# 每块buffer的空间大小,越小的空间被利用越充分,不要设置太大,以免影响其他应用,合适即可
server.undertow.buffer-size=1024
server.undertow.direct-buffers=true

# database - hbase
datasource.hbase.zookeeper.quorum=ip1:2181,ip2:2181,ip3:2181
datasource.hbase.table=hbase
datasource.hbase.zookeeper.znode.parent=/hbase-unsecure

4.hbase配置类

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Table;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
public class HBaseConfig {

    @Value("${datasource.hbase.zookeeper.quorum}")
    private String zookeeper;
    
    @Value("${datasource.hbase.zookeeper.znode.parent}")
    private String parent;
    
    @Value("${datasource.hbase.table}")
    private String tableName;

    public Connection getConnection() throws IOException {
        org.apache.hadoop.conf.Configuration config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.quorum", zookeeper);
        if (parent != null && !"".equals(parent)) {
            config.set("zookeeper.znode.parent", parent);
        }
        Connection connection = ConnectionFactory.createConnection(config);
        return connection;
    }

    @Bean(name = "hbaseTable")
    public Table getHbaseTable() throws IOException {
        Connection connection = getConnection();
        Table table = connection.getTable(TableName.valueOf(tableName));
        return table;
    }
}

5.代码使用

@Service
public class HBaseService {

    private Logger log = LoggerFactory.getLogger(HBaseService.class);

    @Resource(name = "hbaseTable")
    private Table table;

    public boolean delete(){
        try {
            Delete delete = new Delete(Bytes.toBytes("rowKey"));
            //1.根据rowKey删除一行数据
            table.delete(delete);

            //2.删除某一行的某一个列簇内容
            delete.addFamily(Bytes.toBytes("user"));

            //3.删除某一行某个列簇某列的值
            delete.addColumn(Bytes.toBytes("user"), Bytes.toBytes("name"));
            table.close();
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
	
	public boolean put(){
        try{
            List<Put> puts = new ArrayList<Put>();
            Put put1 = new Put(Bytes.toBytes("rowKey1"));
            put1.addColumn(Bytes.toBytes("user"), Bytes.toBytes("name"), Bytes.toBytes("高并发"));
            Put put2 = new Put(Bytes.toBytes("rowKey2"));
            put2.addColumn(Bytes.toBytes("user"), Bytes.toBytes("age"), Bytes.toBytes("25"));
            puts.add(put1);
            puts.add(put2);
            table.put(puts);
            table.close();
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public boolean queryRowKeyIsExist(String key) {
        boolean flag = false;
        try {
            flag = getExists(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
    
	//rowkey是否存在数据
    public boolean getExists(String key) {
        try {
        //构造时先get一次,获取连接
            Get get = new Get(Bytes.toBytes(key));
            get.setCheckExistenceOnly(true);
            Result result = table.get(get);
            return result.getExists();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    public Map<String, String> getRowKey(String rowKey) {
	    Map<String, String> map = new HashMap<>();
	    try {
	        Get get = new Get(Bytes.toBytes(rowKey));
	        Result result = table.get(get);
	        for (Cell cell : result.rawCells()) {
	            map.put(Bytes.toString(CellUtil.cloneQualifier(cell)), Bytes.toString(CellUtil.cloneValue(cell)));
	        }
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
    	return map;
    }
    public void saveRowKey(String rowKey, Map<String, Map<String, String>> data) {
        try {
            Put put = new Put(Bytes.toBytes(rowKey));
            data.entrySet().forEach(e -> e.getValue().entrySet().forEach(ee -> {
            put.addColumn(Bytes.toBytes(e.getKey()),
            Bytes.toBytes(ee.getKey()),Bytes.toBytes(ee.getValue()));
            }));
            table.put(put);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

@Controller
@RequestMapping("/hbase")
public class HBaseController {

    @Autowired
    private HBaseService hBaseService;

    @GetMapping("/getRowKeyIsExist")
    @ResponseBody
    public Object getRowKeyIsExist(String key) {
        if (StrUtil.isNotBlank(key)) {
            if (hBaseService.getRowKeyIsExist(key)) {
                return "1";
            }
        }
        return "0";
    }

}
  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高并发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值