how to list all row keys in an hbase table?

2019-02-03 01:12发布

Can anybody tell me, how to list all row keys in an hbase table?

标签: hbase
6条回答
萌系小妹纸
2楼-- · 2019-02-03 01:49

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);
?>
查看更多
聊天终结者
3楼-- · 2019-02-03 01:55

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

count 'table_name', { INTERVAL => 1 }
查看更多
劫难
4楼-- · 2019-02-03 02:05

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.

查看更多
三岁会撩人
5楼-- · 2019-02-03 02:06

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.

查看更多
够拽才男人
6楼-- · 2019-02-03 02:07

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();
  ...
}
查看更多
Anthone
7楼-- · 2019-02-03 02:09
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();
  ...
}
查看更多
登录 后发表回答