一.连接方式
配置文件
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资源释放失败"); } } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)