springboot集成hbase示例(入门级)


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;
    }

}

猜你喜欢

转载自blog.csdn.net/ory001/article/details/108437370