hbase 1.1.3 java使用

hbase 1.1.3 java使用方式

  • 1.pom引用
<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-client</artifactId>
    <version>1.1.3</version>
</dependency>
  • 2.客户端host配置
    由于hbase 客户端通过zookeeper查询Hmaste、Hregionserver的地址信息,而通过zookeeper中返回的是HMaster、HRegionserver中host信息。所以需要在客户端配置对应的host。

    192.168.162.1 host1
    192.168.162.2 host2
    192.168.162.3 host3

  • 3.java 客户端

/**
 * hbase client 单例连接
 * @author chun
 *
 */
public class HbaseClient {
    private static final Logger LOGGER = LoggerFactory.getLogger(HbaseClient.class);
    private Configuration conf = null;
    private Connection conn = null;
    private static HbaseClient instance = null;

    private HbaseClient(){
        init();
    }
    public void init(){
        try{
            conf =  HBaseConfiguration.create();  
            conf.set("hbase.zookeeper.quorum", PropertiesUtil.getStringValue("hbase.zookeeper.quorum"));  
            conf.set("zookeeper.znode.parent", PropertiesUtil.getStringValue("zookeeper.znode.parent"));
            conf.set("hbase.zookeeper.property.clientPort", PropertiesUtil.getStringValue("hbase.zookeeper.property.clientPort")); 
            conn = ConnectionFactory.createConnection(conf);
        }catch(Exception e){
            LOGGER.error("初始化hbase连接失败"+e.getMessage(),e);
        }
    }

    public static HbaseClient getInstance(){
        if(instance == null){
            synchronized (HbaseClient.class) {
                if(instance == null){
                    instance = new HbaseClient();
                }
            }
        }
        return instance;
    }

    /**
     * 获取htable操作类
     * @param tableName
     * @return
     * @throws IOException
     */
    public Table getHtable(String tableName) throws IOException{
        return conn.getTable(TableName.valueOf(tableName));
    }

    /**
     * 
     * @param hTableInterface
     */
    public void relaseHtable(Table table){
        if(table == null){
            return;
        }
        try {
            table.close();
        } catch (IOException e) {
            LOGGER.error(e.getMessage(),e);
        }
    }

    /**
     * 关闭hbase连接
     */
    public void destory(){
        try {
            conn.close();
            instance = null;
        } catch (IOException e) {
            LOGGER.error(e.getMessage(),e);
        }
    }

}
  • 4.简单示例
    /**
     * 测试get使用
     */
    @Test
    public void testGet(){
        HTableInterface hTableInterface  = null;

        try{

            Get get = new Get(Bytes.toBytes("a1"));
            Get get2 = new Get(Bytes.toBytes("a2"));
            List<Get> gets = new ArrayList<Get>();
            gets.add(get);
            gets.add(get2);

            hTableInterface = HbaseClient.getInstance().getHtable("test");
            Result[] results = hTableInterface.get(gets);
            for(Result result : results){
                if(!result.isEmpty()){
                    System.out.println("the row is "+ new String(result.getRow()));
                    NavigableMap<byte[], byte[]> qualiers = result.getFamilyMap(Bytes.toBytes("cf"));
                    for(Entry<byte[], byte[]> entry: qualiers.entrySet()){
                        System.out.println("the qualier is "+Bytes.toString(entry.getKey())+" value is "+ Bytes.toString(entry.getValue()));
                    }
                }else{
                    System.out.println("the row is empty");
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            HbaseClient.getInstance().relaseHtable(hTableInterface);
        }

    }

    /**
     * 测试scan使用
     */
    @Test
    public void testScan(){

        Table table  = null;

        try{

            Scan scan = new Scan();
            scan.setStartRow(Bytes.toBytes("a"));

            table = HbaseClient.getInstance().getHtable("t1");
            ResultScanner scanner = table.getScanner(scan);
            while(true){
                Result result = scanner.next();
                if(result == null){
                    break;
                }
                if(!result.isEmpty()){
                    System.out.println("the row is "+ new String(result.getRow()));
                    NavigableMap<byte[], byte[]> qualiers = result.getFamilyMap(Bytes.toBytes("cf"));
                    for(Entry<byte[], byte[]> entry: qualiers.entrySet()){
                        System.out.println("the qualier is "+Bytes.toString(entry.getKey())+" value is "+ Bytes.toString(entry.getValue()));
                    }
                }else{
                    System.out.println("the row is empty");
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            HbaseClient.getInstance().relaseHtable(table);
        }

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值