Hbase面试 描述 Hbase 中 scan 和 get 的功能以及实现的异同.

Scan

获取全部数据 @Test

//测试性能
public void test() throws IOException {
Scan scan=new Scan();
excuteScan(scan);
}

可以设置setbatch

@Test
    //测试性能
    public void test() throws IOException {
    Scan scan=new Scan();
    //Set the maximum number of values to return for each call to next()
    scan.setBatch(4);
    excuteScan(scan);
    }

源码
控制的是每次next调用的数量,那继续看next

/**
   * Set the maximum number of values to return for each call to next()
   * @param batch the maximum number of values
   */
  public Scan setBatch(int batch) {
    if (this.hasFilter() && this.filter.hasFilterRow()) {
      throw new IncompatibleFilterException(
        "Cannot set batch on a scan using a filter" +
        " that returns true for filter.hasFilterRow");
    }
    this.batch = batch;
    return this;
  }
@Override
public Result next() throws IOException {
  // If the scanner is closed and there's nothing left in the cache, next is a no-op.
  if (cache.size() == 0 && this.closed) {
    return null;
  }

  // 缓冲中没有就 RPC 调用读取数据进缓存
  if (cache.size() == 0) {   
        loadCache();
  }
 
  // 缓冲中有直接从缓存中取
  if (cache.size() > 0) {
    return cache.poll();
  }
 
  // if we exhausted this scanner before calling close, write out the scan metrics
  writeScanMetrics();
  return null;
}

每次从缓存 cache 中读,缓存为空则 loadCache , 实际上 cache 是通过一个链表来实现的.

setBatch() 坑爹的命名,这个实际上是配置获取的列数,假如表有两个列簇 cf,info,每个列簇5个列。这样每行可能有10列了,setBatch() 可以控制每次获取的最大列数,进一步从列级别控制流量。配置建议:当列数很多,数据量大时考虑配置此参数,例如100列每次只获取50列。一般情况可以默认值(-1 不受限)。

Get

看几个例子就明白了

 /**
     * 单行获取每次RPC请求值发送一个Get对象中的数据,因为Get对象初始化时需要输入行键,因此可以理解为一个Get对象就代表一行。
     * 一行中可以包含多个列簇或者多个列等信息
     *
     * @throws IOException
     */
    @Test
    //利用get查询数据
    public void testget() throws IOException {
        Get get = new Get("rk000010".getBytes());
        get.setClosestRowBefore(true);
        System.out.println(get);
        //如何从geg对象中获取数据
        //Extracts certain cells from a given row.
        Result result = table.get(get);
        CellScanner cellScanner = result.cellScanner();
        while (cellScanner.advance()) {

            Cell cell = cellScanner.current();
            System.out.println(new String(CellUtil.cloneRow(cell)) + "\t" + new String(CellUtil.cloneValue(cell)));
        }
    }


    @Test
    //利用get查询数据
    public void testget1() throws IOException {
        Get get = new Get("rk000010".getBytes());
        //如何从geg对象中获取数据
        //Extracts certain cells from a given row.
        /**
         * (1)Result对象,在查询得到的结果,每一行数据会被作为一个Result对象,将数据存入到一个Result实例中。
         * 当我们需要获取一行数据时则需要获取该行数据所在的Result对象即可。该对象内部封装了一个KeyValue 对象数组。
         * 在0.98.4以前的本班。result类提供了 raw() 方法去获取整个result对象中的KeyValue数组。
         * 在0.98.4以后,则提供了一个新的节后: rowCells() 方法获取KeyValue对象,不过返回的是KeyValue 对象父类引用
         */
        Result result = table.get(get);
        System.out.println(get.getMaxVersions());
        //Return an cells of a Result as an array of KeyValues WARNING do not use, expensive.
        KeyValue[] kvs = result.raw();
        System.out.println(kvs.length);//4
        for (KeyValue kv : kvs) {
            System.out.println(Bytes.toString(kv.getRow()));
            System.out.println(Bytes.toString(kv.getValue()));
        }
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值