一.连接方式
配置文件
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
浙公网安备 33010602011771号