springboot集成hbase示例(入门级)

本文详细介绍如何在Spring Boot项目中集成HBase,包括pom.xml配置、application.yml设置、自定义HBaseConfiguration、实现IHBaseService接口以及通过HBaseController进行数据操作,为开发者提供完整的HBase集成解决方案。
摘要由CSDN通过智能技术生成


Hbase分布式环境搭建

1.pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>hbase</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hbase</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-hadoop-hbase</artifactId>
            <version>2.5.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-hadoop</artifactId>
            <version>2.5.0.RELEASE</version>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.application.yml

hbase:
  zookeeper:
    quorum: node1,node2,node3
    property:
      clientPort: 2181

zookeeper:
  znode:
    parent: /hbase

3.HBaseConfiguration.java

package com.example.hbase.configuration;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.hadoop.hbase.HbaseTemplate;

/**
 * @author :ouruyi
 * @version 1.0
 * @date :Created in 2020/9/6 18:45
 * 功能描述:
 */
@Configuration
public class HBaseConfiguration {

    @Value("${hbase.zookeeper.quorum}")
    private String zookeeperQuorum;

    @Value("${hbase.zookeeper.property.clientPort}")
    private String clientPort;

    @Value("${zookeeper.znode.parent}")
    private String znodeParent;


    @Bean
    public org.apache.hadoop.conf.Configuration hBaseConfiguration(){
        org.apache.hadoop.conf.Configuration conf = org.apache.hadoop.hbase.HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", zookeeperQuorum);
        conf.set("hbase.zookeeper.property.clientPort", clientPort);
        conf.set("zookeeper.znode.parent", znodeParent);
        conf.set("zookeeper.sasl.client", "false");
        return conf;
    }


    @Bean
    public HbaseTemplate hbaseTemplate(org.apache.hadoop.conf.Configuration conf) {
        return new HbaseTemplate(conf);
    }

}

4.IHBaseService.java

package com.example.hbase.service;

import org.apache.hadoop.hbase.client.Result;

import java.util.List;

/**
 * @author :ouruyi
 * @version 1.0
 * @date :Created in 2020/9/6 19:12
 * 功能描述:
 */
public interface IHBaseService {

    /**
     * key 前缀匹配
     * @param tableName
     * @param startRowKey
     * @param stopRowKey
     * @param prefix
     * @return
     */
    List<Result> getListWithPrefix(String tableName, String startRowKey, String stopRowKey, String prefix);

    /**
     * key精确查找
     * @param tableName
     * @param rowKeys
     * @param familyColumn
     * @param column
     * @return
     */
    List<Result> getListRowKey(String tableName, List<String> rowKeys, String familyColumn, String column);

}

5.HBaseServiceImpl.java

package com.example.hbase.service.impl;

import com.example.hbase.service.IHBaseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.hadoop.hbase.HbaseTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author :ouruyi
 * @version 1.0
 * @date :Created in 2020/9/6 19:12
 * 功能描述:
 */
@Slf4j
@Service
public class HBaseServiceImpl implements IHBaseService {
    @Autowired
    private HbaseTemplate hbaseTemplate;

    @Override
    public List<Result> getListWithPrefix(String tableName, String startRowKey, String stopRowKey, String prefix) {
        FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
        filterList.addFilter(new PrefixFilter(Bytes.toBytes(prefix)));
        Scan scan = new Scan();
        if (filterList.getFilters().size() > 0) {
            scan.setFilter(filterList);
        }
        scan.setStartRow(Bytes.toBytes(startRowKey));
        scan.setStopRow(Bytes.toBytes(stopRowKey));

        return hbaseTemplate.find(tableName, scan, (rowMapper, rowNum) -> rowMapper);
    }

    @Override
    public List<Result> getListRowKey(String tableName, List<String> rowKeys, String familyColumn, String column) {
        return rowKeys.stream().map(rk -> {
            if (StringUtils.isNotBlank(familyColumn)) {
                if (StringUtils.isNotBlank(column)) {
                    return hbaseTemplate.get(tableName, rk, familyColumn, column, (rowMapper, rowNum) -> rowMapper);
                } else {
                    return hbaseTemplate.get(tableName, rk, familyColumn, (rowMapper, rowNum) -> rowMapper);
                }
            }
            return hbaseTemplate.get(tableName, rk, (rowMapper, rowNum) -> rowMapper);
        }).collect(Collectors.toList());
    }

}

6.HBaseController.java

package com.example.hbase.controller;

import com.example.hbase.service.IHBaseService;
import org.apache.hadoop.hbase.client.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

/**
 * @author :ouruyi
 * @version 1.0
 * @date :Created in 2020/9/6 19:11
 * 功能描述:
 */
@RestController
public class HBaseController {

    @Autowired
    IHBaseService hBaseService;

    @RequestMapping("/getListWithPrefix")
    List<Result> getListWithPrefix(){
        List<Result> resultList = hBaseService.getListWithPrefix("hbase", "001", "999", "12");
        // 预期执行结果male
        System.out.println(new String(resultList.get(0).value()));
        return resultList;
    }

    @RequestMapping("/getListRowKey")
    List<Result> getListRowKey(){
        List<String> rowKeys = Collections.singletonList("123");
        List<Result> resultList = hBaseService.getListRowKey("hbase", rowKeys, "info", "sex");
        // 预期执行结果male
        System.out.println(new String(resultList.get(0).value()));
        return resultList;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

搬山境KL攻城狮

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

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

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

打赏作者

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

抵扣说明:

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

余额充值