Hbase Rest API : 数据查询

hbase(hadoop)是用java编写的,有些语言(例如python)能够对它提供良好的支持,但也有很多语言使用起来并不是那么方便,比如c#只能通过thrift访问。Rest就能很好的解决这个问题。Hbase的org.apache.hadoop.hbase.rest包提供了rest接口,它内嵌了jetty作为servlet容器。

启动命令:./bin/hbase rest start -p <port>

默认端口8080.

以下介绍如何使用rest api查询hbase数据。

  • rowkey查询

1. 单值查询。只查询单个column family或column。

语法:GET /<table>/<row>/<column> ( : <qualifier> )?( / <timestamp> )?

范例:curl -H "Accept: application/json" http://localhost:8000/table/key/cf:raw

在测试中我发现加入<timestamp>参数之后就找不到数据了,不知道是不是bug

2. 多值查询。查询多个column family或column。

语法:GET /<table>/<row> ( / ( <column> ( : <qualifier> )? ( , <column> ( : <qualifier> )? )+ )?( / ( <start-timestamp> ',' )? <end-timestamp> )? )?( ?v= <num-versions> )?

范例:curl -H "Accept: application/json"  http://localhost:8000/table1/key1/cf1:q1,cf2/1436350580000,1436350590000/3

可以通过timestamp过滤查询结果,也可以通过<num-versions>设定返回的最大版本数

  • 通配符查询

在rowkey后添加*号,可以返回以该rowkey为前缀的所有记录,如果以*号为rowkey来查询,则返回该table中所有的记录。

范例:curl -H "Accept: application/json" http://localhost:8000/table/keyprefix*/cf

  • key list查询

据我所知,暂不支持该功能,只能对循环key list对每个key做查询。

  • key范围查询

1. 有状态scanner

首先在server端创建scanner:PUT /<table>/scanner

范例:curl -H "Content-Type: text/xml" -d '<Scanner batch="1"/>' http://localhost:8000/table/scanner

在response中获得scanner id,然后可以使用这个id拿到这个scanner查询到的所有cell的值,

语法:GET /<table>/scanner/<scanner-id>

范例:curl -H "Content-Type: application/json" http://localhost:8000/table/scanner/12447063229213b1937

注意每次调用只能返回一个cell的值,而且比较奇怪的是只返回值而没有cf和qualifier,所以使用起来很不方便。

使用完之后删除scanner:DELETE /<table>/scanner/<scanner-id>

2. 无状态scanner

无状态的scanner不保存任何关于查询的状态,它把所有的查询条件作为参数进行一次性的查询。

查询参数包括:

startrow - 查询起始key。

endrow - 查询终止key。

columns - 查询的column。

starttime, endtime - 通过指定起始时间选择特定的数据版本,starttime和endtime必须同时设置。

maxversions - 每个返回的cell的版本数。

limit - 返回数据条数.

范例:

查询单个column cf:q:

curl -H "Content-Type: text/xml" https://localhost:8080/table/*?columns=cf:q

查询多个column cf1:q1和cf2:q2:

curl -H "Content-Type: text/xml" https://localhost:8080/table/*?columns=cf1:q1,cf2:q2

从key1开始查询两条记录:

curl -H "Content-Type: text/xml" https://localhost:8080/table/*?startrow=key1&limit=2

查询1389900769772和1389900800000之间的版本:

curl -H "Content-Type: text/xml" https://localhost:8080/table/*?starttime=1389900769772&endtime=1389900800000

猜你喜欢

转载自kane-xie.iteye.com/blog/2226813