随笔- 100  文章- 0  评论- 3  阅读- 30413 

一.连接方式

配置文件

复制代码
package com.jun.hbase.config;

import com.jun.hbase.template.HbaseTemplate;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HBaseConfig {

    private static final String HBASE_QUORUM = "hbase.zookeeper.quorum";

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

    @Bean
    @ConditionalOnMissingBean(HbaseTemplate.class)
    public HbaseTemplate hbaseTemplate() {
        org.apache.hadoop.conf.Configuration configuration = HBaseConfiguration.create();
        configuration.set(HBASE_QUORUM, zookeeper);
        return new HbaseTemplate(configuration);
    }

}
复制代码

 

获取连接

复制代码
public class HbaseTemplate {

    private static final int CACHING_SIZE = 5000;

    private Configuration configuration;

    private volatile Connection connection;

    public HbaseTemplate(Configuration configuration) {
        this.configuration = configuration;
    }

    public Connection getConnection() {
        if (null == this.connection) {
            synchronized (this) {
                if (null == this.connection) {
                    try {
                        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(10, 20, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
                        poolExecutor.prestartCoreThread();
                        this.connection = ConnectionFactory.createConnection(configuration, poolExecutor);
                    } catch (IOException e) {
                        log.error("hbase connection资源池创建失败");
                    }
                }
            }
        }
        return this.connection;
    }
}
复制代码

 

二:保存数据

复制代码
    public <T> void save(String tableName, final RowMapper<T> action, T t) {
        this.execute(tableName, mutator -> {
            Mutation mutation = action.toMutation(t);
            if (Objects.isNull(mutation)) {
                return;
            }
            mutator.mutate(mutation);
        });
    }

    // 保存数据到Hbase
    public void execute(String tableName, MutatorCallback action) {
        StopWatch sw = new StopWatch();
        sw.start();
        BufferedMutator mutator = null;
        try {
            BufferedMutatorParams mutatorParams = new BufferedMutatorParams(TableName.valueOf(tableName));
            mutator = this.getConnection().getBufferedMutator(mutatorParams.writeBufferSize(3 * 1024 * 1024));
            action.doInMutator(mutator);
        } catch (Throwable throwable) {
            sw.stop();
            throw new RuntimeException(throwable);
        } finally {
            if (null != mutator) {
                try {
                    mutator.flush();
                    mutator.close();
                    sw.stop();
                } catch (IOException e) {
                    log.error("hbase mutator资源释放失败");
                }
            }
        }
    }
复制代码

 

三:查询数据

复制代码
    /**
     * 根据rowKey获取数据
     */
    public <T> T getByRowKey(String tableName, String rowKey, final RowMapper<T> action) {
        return execute(tableName, table -> {
            try {
                Get get = new Get(Bytes.toBytes(rowKey));
                Result result = table.get(get);
                return action.mapRow(result, 0);
            } catch (Exception e) {
                log.error("根据rowKey获取数据异常", e);
                return null;
            }
        });
    }

  

    private <T> T execute(String tableName, TableCallback<T> action) {
        StopWatch sw = new StopWatch();
        sw.start();
        Table table = null;
        try {
            table = this.getConnection().getTable(TableName.valueOf(tableName));
            return action.doInTable(table);
        } catch (Throwable throwable) {
            throw new RuntimeException(throwable);
        } finally {
            if (Objects.nonNull(table)) {
                try {
                    table.close();
                    sw.stop();
                } catch (IOException e) {
                    log.error("hbase资源释放失败");
                }
            }
        }
    }
复制代码

 

 posted on   曹军  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示