hbase(一)java中get某个hbase表格信息

package com.hjm.hbase.client;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

import java.io.Exception;

/**这是一个如何获取某个hbase表格信息的操作
 * 在Java的客户端连接hbase的时候只需要连接zk就可以
 * 1.配置对象 设置zk的位置
 * 2.获取操作hbase的连接对象
 * 3.设置已存在的表的名字
 * 4.通过表名进行连接获取表对象
 * 5.创建并设置新的get对象的属性.   通过表.get(get对象)获取得到的结果
 * 6.对结果进行属性克隆 转String进行print
 */
public class HbaseDemo1 {
    public static void main(String[] args) throws Exception {
        //1.获取配置对象
        Configuration conf = HBaseConfiguration.create();
        //2.配置对象设置好属性--zk的位置
        conf.set("hbase.zookeeper.quorum","linux01:2181,linux02:2181,linux03:2181");
        //3.通过配置对象创建连接对象
        Connection conn = ConnectionFactory.createConnection(conf);
        // TableName.create()创建表或者 以下 设置一个已经存在的表名
        TableName tb_a = TableName.valueOf("tb_a");
        //通过表名得到相应连接的表对象--a
        Table a = conn.getTable(tb_a);
        //获取这次想要查看的get对象,并设置get的属性-某个行键
        Get get = new Get("rk001".getBytes());
        //获取get的结果--克隆单元格里边包含的某些属性
        Result result = a.get(get);
        //result.advance()是否有下一个cell
        //result.current()获取当前cell
        while (result.advance()){
            Cell currentCell = result.current();
            byte[] row = CellUtil.cloneRow(currentCell);
            byte[] family = CellUtil.cloneFamily(currentCell);
            byte[] qualifier = CellUtil.cloneQualifier(currentCell);
            byte[] value = CellUtil.cloneValue(currentCell);
            System.out.println(new String(row)+"--"+new String(qualifier)+"--"+new String(value));
        }
        //关闭表格 关闭连接
        a.close();
        conn.close();
    }
}

如有误请不吝指正!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值