上云无忧 > 文档中心 > 天翼云云HBASE数据库-使用Get读取数据
云HBASE数据库
天翼云云HBASE数据库-使用Get读取数据

文档简介:
使用Get读取数据 功能简介 要从表中读取一条数据,首先需要实例化该表对应的Table实例,然后创建一个Get对 象。也可以为Get对象设定参数值,如列族的名称和列的名称。
*产品来源:中国电信天翼云。免费试用 咨询热线:400-826-7010,为您提供专业的售前咨询,让您快速了解云产品,助您轻松上云! 微信咨询
  免费试用、价格特惠

使用Get读取数据


功能简介
要从表中读取一条数据,首先需要实例化该表对应的Table实例,然后创建一个Get对
象。也可以为Get对象设定参数值,如列族的名称和列的名称。查询到的行数据存储在
Result对象中,Result中可以存储多个Cell。

代码样例
以下代码片段在cn.chinatelecom.hbase.sample包的“HBaseSample”类的sampleGet方
法中。
public static List<String> sampleGet(Connection conn) {
        List<String> list = new ArrayList<>();
        TableName tableName = TableName.valueOf("hbase-sample");
        byte[] familyName = Bytes.toBytes("user");
        byte[][] qualifier = {Bytes.toBytes("name"), Bytes.toBytes("age")};
        byte[] rowKey = Bytes.toBytes("001");
        Get get = new Get(rowKey);
        get.addColumn(familyName, qualifier[0]);
        get.addColumn(familyName, qualifier[1]);
        try (Table table = conn.getTable(tableName)) {
            Result result = table.get(get);
            showData(list, result);
            log.info("Get data successfully.");
        } catch (IOException e) {
            log.error("Get data failed ", e);
        }
        return list;
}

private static void showData(List<String> list, Result result) {
        for (Cell cell : result.rawCells()) {
            String temp = Bytes.toString(CellUtil.cloneRow(cell)) + ":"
                    + Bytes.toString(CellUtil.cloneFamily(cell)) + ","
                    + Bytes.toString(CellUtil.cloneQualifier(cell)) + ","
                    + Bytes.toString(CellUtil.cloneValue(cell));
            log.info(temp);
            list.add(temp);
        }
    }


相似文档
  • 使用Scan读取数据 功能简介 要从表中读取数据,首先需要实例化该表对应的Table实例,然后创建一个Scan对象, 并针对查询条件设置Scan对象的参数值,为了提高查询效率,最好指定StartRow和 StopRow。
  • 使用过滤器Filter 功能简介 HBase Filter主要在Scan和Get过程中进行数据过滤,通过设置一些过滤条件来实现,如 设置RowKey、列名或者列值的过滤条件。
  • 创建Phoenix表 功能简介 Phoenix依赖HBase作为其后备存储,支持标准SQL和JDBC API的强大功能,使得SQL 用户可以访问HBase集群。 代码样例:
  • 写Phoenix表 功能简介 使用Phoenix实现写数据。 示例代码 以下代码片段在 cn.chinatelecom.hbase.sample.PhoenixSample.sampleUpsertData方法中。 /** * Upsert data */
  • 读Phoenix表 功能简介 使用Phoenix实现读数据。 示例代码 以下代码片段在cn.chinatelecom.hbase.sample.PhoenixSample.sampleSelectData方法中。 /** * Select Data */
官方微信
联系客服
400-826-7010
7x24小时客服热线
分享
  • QQ好友
  • QQ空间
  • 微信
  • 微博
返回顶部