Hbase的REST访问

使用hbase REST服务器需要先去服务器上启动服务

前台启动hbase rest服务
bin/hbase rest start -p <port>
后台启动hbase服务
bin/hbase-daemon.sh start rest -p <port>
停止服务
bin/hbase-daemon.sh stop rest

不加端口的情况下,端口默认为8080


利用requests模块访问rest接口的python代码如下,其中注释是利用curl命令的访问方式:

[python]  view plain  copy
  1. baseurl = "http://192.168.119.128:8080";  
  2.   
  3. #获取表table2中rowkey为liu的行  
  4. response = requests.get(baseurl+'/table2/liu', headers={"Accept" : "application/json"})  
  5. ''''' 
  6.           相当于 curl  -H "Accept:application/json"  http://192.168.119.128:8080/table2/liu 
  7. '''  
  8. print response.json()  
  9. #返回的字段名称和值为base64编码的,需要解密查看  
  10. print base64.b64decode(u'bW9ibGllOg==')  
  11.   
  12. #查看集群状态  
  13. response = requests.get(baseurl+'/status/cluster', headers={"Accept" : "application/json"})  
  14. ''''' 
  15.      相当于 curl  -H "Accept:application/json"  http://192.168.119.128:8080/status/cluster 
  16. '''  
  17.   
  18. print response.json()  
  19.   
  20. #查看集群版本  
  21. response = requests.get(baseurl+'/version/cluster', headers={"Accept" : "application/json"})  
  22. ''''' 
  23.          相当于  curl  -H "Accept:application/json"  http://192.168.119.128:8080/status/cluster 
  24. '''  
  25. print response.json()  
  26.   
  27. #获得表list  
  28. response = requests.get(baseurl+'/', headers={"Accept" : "application/json"})  
  29. ''''' 
  30.          相当于  curl  -H "Accept:application/json"  http://192.168.119.128:8080/ 
  31. '''  
  32. print response.json()  
  33.   
  34. #table2中添加一行数据rowkey为moblie,xml字段名称和数据base64编码  
  35. rdata='<?xml version="1.0" encoding="UTF-8" standalone="yes"?><CellSet><Row key="bW9ibGllOg=="><Cell column="bW9ibGllOg==">bGl1emhvdWxvbmcy</Cell></Ro    w></CellSet>'  
  36. response = requests.put(baseurl+'/table2/moblie', data=rdata,headers = {'content-type''text/xml'})  
  37. print response  
  38.   
  39. #curl -vi -X PUT  -H "Accept: text/xml"  -H "Content-Type: text/xml" -d '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><CellSet><Row key="bW9    ibGllOg=="><Cell column="bW9ibGllOg==">bGl1emhvdWxvbmcy</Cell></Row></CellSet>' "http://192.168.119.128:8080/table2/moblie"  
  40. #添加表users  
  41. rdata='<?xml version="1.0" encoding="UTF-8"?><TableSchema name="users"><ColumnSchema name="cf" /></TableSchema>'  
  42. response = requests.post(baseurl+'/users/schema', data=rdata,headers = {'content-type''text/xml'})  
  43. print response  
  44. #curl -vi -X POST -H "Accept: text/xml"  -H "Content-Type: text/xml" -d '<?xml version="1.0" encoding="UTF-8"?><TableSchema name="users"><ColumnSchema    name="cf" /></TableSchema>' "http://192.168.119.128:8080/users/schema"  
  45.   
  46. #删除表users  
  47. response = requests.delete(baseurl+'/users/schema')  
  48. print response  
  49. #curl -vi -X DELETE  -H "Accept: text/xml"  "http://192.168.119.128:8080/users/schema"  

更多信息请参考
http://hbase.apache.org/book.html#_rest


扩展curl 参数含义
-X/--request [GET|POST|PUT|DELETE|…]  使用指定的http method發出 http request
-H/--header                           設定request裡的header
-i/--include                          顯示response的header
-d/--data                             設定 http parameters 
-v/--verbose                          輸出比較多的訊息
-u/--user                             使用者帳號、密碼
-b/--cookie                           cookie  


Hbase的访问方式包括:
1、Native Java API:最常规和高效的访问方式;
2、HBase Shell:HBase的命令行工具,最简单的接口,适合HBase管理使用;
3、Thrift Gateway:利用Thrift序列化技术,支持C++,PHP,Python等多种语言,适合其他异构系统在线访问HBase表数据;
4、REST Gateway:支持REST 风格的Http API访问HBase, 解除了语言限制;
5、MapReduce:直接使用MapReduce作业处理Hbase数据;

6、使用Pig/hive处理Hbase数据。


本文转自 https://blog.csdn.net/liuzhoulong/article/details/52056497


扫描二维码关注公众号,回复: 1586619 查看本文章

猜你喜欢

转载自blog.csdn.net/weixin_40592935/article/details/80660695
今日推荐