springboot 集成hbase

1.maven依赖:(hbase 依赖的包中会和其他包中有冲突,所以在此选择忽略)


    org.apache.hbase
    hbase-client
    1.3.0
    
        
            org.slf4j
            slf4j-log4j12
        
        
            log4j
            log4j
        
        
            javax.servlet
            servlet-api
        
        
            com.google.guava
            guava
        
        
            org.elasticsearch
            elasticsearch
        
    

2.yml文件配置

hbase:
  config:
    hbase.zookeeper.quorum: xxx.xxx.xxx.xxx
    hbase.zookeeper.port: 2181
    hbase.zookeeper.znode: /hbase
    hbase.client.keyvalue.maxsize: 1572864000

3.HbaseProperties 内容

import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.Map;

@ConfigurationProperties(prefix = "hbase")
public class HbaseProperties {
    private Map config;
    public Map getConfig() {
        return config;
    }
    public void setConfig(Map config) {
        this.config = config;
    }
}

4.HbaseConfig 配置

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.Map;
import java.util.Set;

@Configuration
@EnableConfigurationProperties(HbaseProperties.class)
public class HbaseConfig {
    private final HbaseProperties properties;
    public HbaseConfig(HbaseProperties properties) {
        this.properties = properties;
    }
    public org.apache.hadoop.conf.Configuration configuration() {
        org.apache.hadoop.conf.Configuration configuration = HBaseConfiguration.create();
        Map config = properties.getConfig();
        Set keySet = config.keySet();
        for (String key : keySet) {
            configuration.set(key, config.get(key));
        }
        return configuration;
    }
}

5.HbaseService 接口

public interface HbaseService {
    String getValue(String tableName, String rowkey, String family, String column);
}

6.HbaseServiceImpl 接口实现类

@Service
public class HbaseServiceImpl implements HbaseService{
    private static final Logger log = LoggerFactory.getLogger(HbaseServiceImpl.class);
    @Autowired
    private HbaseConfig con;
    /**
     * 根据tableName,rowkey,family coluem 查询单个数据
     */
    public String getValue(String tableName, String rowkey, String family, String column) {
        Table table = null;
        Connection connection=null;
        String res = "";
        if (StringUtils.isBlank(tableName) || StringUtils.isBlank(family)
                || StringUtils.isBlank(rowkey) || StringUtils.isBlank(column)) {
            return null;
        }
        try {
            connection = ConnectionFactory.createConnection(con.configuration());
            table = connection.getTable(TableName.valueOf(tableName));
            Get g = new Get(rowkey.getBytes());
            g.addColumn(family.getBytes(), column.getBytes());
            Result result = table.get(g);
            List ceList = result.listCells();
            if (ceList != null && ceList.size() > 0) {
                for (Cell cell : ceList) {
                    res = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                table.close();
                connection.close();
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
        return res;
    }

7.参数说明(想要了解更多hbase的数据存储结构,可自行百度了解)

tableName:hbase表名, 

rowkey:rowkey规则(i_20180910),

family:列簇,

column :列

注:此例子只是单个数据获取,有需要批量获取数据的,或则分页处理,可以给我留言,整理的不好,请见谅~~~~~~

你可能感兴趣的:(springboot 集成hbase)