HBase Java APi 之 Result

通过HBase Java APi 读取数据到 Result中,再对Result进行操作

import java.util.Map;
import java.util.Map.Entry;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class ResultTest {

    private static Configuration cfg = HBaseConfiguration.create();
    private static Connection connection = null;
    private static Table table = null;
    private static Get get = null;
    private static Result result = null;

    /**
     * 测试Result的getFamilyMap方法
     * <p>获取列族下的所有列修饰符与值的键值对
     */
    public static void getFamilyMapTest(String tableName, String rowKey, String family) throws Exception {
        initTableResult(tableName, rowKey);
        get.addFamily(Bytes.toBytes(family));
        connection.close();

        // 获取列族下的所有列修饰符与值的键值对,并保存于Map集合中
        Map<byte[], byte[]> kvPair = result.getFamilyMap(Bytes.toBytes(family));

        // 通过for循环遍历kvPair
        for (Entry<byte[], byte[]> entry : kvPair.entrySet()) {
            String key = new String(entry.getKey(), "utf8");
            String value = new String(entry.getValue(), "utf8");
            System.out.println(family + ":" + key + " = " + value);
        }

//        // 通过迭代器遍历kvPair
//        Iterator<Entry<byte[], byte[]>> itr = kvPair.entrySet().iterator();
//        while (itr.hasNext()) {
//            Entry<byte[], byte[]> entry = itr.next();
//            String key = new String(entry.getKey());
//            String value = new String(entry.getValue());
//            System.out.println(family + ":" + key + " = " + value);
//        }
    }

    /**
     * 测试Result的getValue方法
     * <p>获取指定列键的值
     */
    public static void getValueTest(String tableName, String rowKey, String family, String qualifier) throws Exception {
        initTableResult(tableName, rowKey);
        get.addFamily(Bytes.toBytes(family));
        connection.close();

        String value = new String(result.getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier)));
        System.out.println(family + ":" + qualifier + " = " + value);
    }

    /**
     * 测试Result的containsColumn方法
     * <p>检测列键是否存在
     */
    public static void containsColumnTest(String tableName, String rowKey, String family, String qualifier)
            throws Exception {
        initTableResult(tableName, rowKey);
        get.addFamily(Bytes.toBytes(family));
        connection.close();

        boolean b = result.containsColumn(family.getBytes(), qualifier.getBytes());
        System.out.println(family + ":" + qualifier + " is exist: " + b);
    }

    /**
     * 测试Result的listCells方法
     * <p>列出指定行键的所有单元格Cell
     */
    public static void listCellsTest(String tableName, String rowKey) throws Exception {
        initTableResult(tableName, rowKey);
        connection.close();
        
        System.out.println(Bytes.toString(result.getRow()) + "'s Cells : " + result.listCells());
        
        // 与上一条代码结果相同
//        System.out.println(new String(result.getRow()) + "'s Cells : " + result.listCells());
    }

    /**
     * 初始化表及其结果集
     */
    private static void initTableResult(String tableName, String rowKey) throws Exception {
        connection = ConnectionFactory.createConnection(cfg);
        table = connection.getTable(TableName.valueOf(tableName));
        get = new Get(Bytes.toBytes(rowKey));
        result = table.get(get);
    }

      public static void main(String[] args) throws Exception {
        
//        PutGet.put("ns1:scores", "tom", "grade", "", "1");
//        PutGet.put("ns1:scores", "tom", "course", "art", "80");
//        PutGet.put("ns1:scores", "tom", "course", "math", "89");
//        PutGet.put("ns1:scores", "jason", "grade", "", "2");
//        PutGet.put("ns1:scores", "jason", "course", "art", "87");
//        PutGet.put("ns1:scores", "jason", "course", "math", "57");
//        PutGet.get("ns1:scores", "jason");
//        PutGet.get("ns1:scores", "jason", "course");
//        PutGet.get("ns1:scores", "jason", "course", "math");
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值