开发者

how to list all row keys in an hbase table?

开发者 https://www.devze.com 2023-02-15 01:20 出处:网络
Can anybody开发者_如何学运维 tell me, how to list all row keys in an hbase table?The HBase shell could be used to list all the row keys:

Can anybody开发者_如何学运维 tell me, how to list all row keys in an hbase table?


The HBase shell could be used to list all the row keys:

count 'table_name', { INTERVAL => 1 }


This should be considerably faster (the FirstKeyOnlyFilter is run on the server and strips all the column data before sending the result to the client):

Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, tableName.getBytes());
System.out.println("scanning full table:");
Scan scan = new Scan();
scan.setFilter(new FirstKeyOnlyFilter());
ResultScanner scanner = table.getScanner(scan);
for (Result rr : scanner) {
  byte[] key == rr.getRow();
  ...
}


Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, tableName.getBytes());

System.out.println("scanning full table:");
ResultScanner scanner = table.getScanner(new Scan());
for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
  byte[] key == rr.getRow();
  ...
}


When performing a table scan where only the row keys are needed (no families, qualifiers, values or timestamps), add a FilterList with a MUST_PASS_ALL operator to the scanner using setFilter. The filter list should include both a FirstKeyOnlyFilter and a KeyOnlyFilter. Using this filter combination will result in a worst case scenario of a RegionServer reading a single value from disk and minimal network traffic to the client for a single row.


Use the getRow method of Result class. Its description says:

Method for retrieving the row key that corresponds to the row from which this Result was created.

Assuming table is your hbase table and you are connected to your HBase instance, all you need to do is:

Scan scan = new Scan();
ResultScanner rscanner = table.getScanner(scan);
for(Result r : rscanner){
   //r is the result object that contains the row
   //do something
   System.out.println(Bytes.toString(r.getRow())); //doing something
}

I understand that this has already been answered from Java API point of view but a little more detail never hurt anyone.


It seems that you want to use HBase thrift client in PHP. Here is a sample code and you can get all data in HBase and get their row keys.

<? $_SERVER['PHP_ROOT'] = realpath(dirname(__FILE__).'/..');
   require_once $_SERVER['PHP_ROOT'].'/flib/__flib.php';
   flib_init(FLIB_CONTEXT_SCRIPT);
   require_module('storage/hbase');
   $hbase = new HBase('<server_name_running_thrift_server>', <port on which thrift server is running>);
   $hbase->open();
   $client = $hbase->getClient();
   $result = $client->scannerOpenWithFilterString('table_name', "(PrefixFilter ('row2') AND (QualifierFilter (>=, 'binary:xyz'))) AND (TimestampsFilter ( 123, 456))");
   $to_print = $client->scannerGetList($result,1);
   while ($to_print) {
      print_r($to_print);
      $to_print = $client->scannerGetList($result,1);
    }
   $client->scannerClose($result);
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号