设为首页 加入收藏

TOP

Hbase的REST访问
2018-11-13 15:31:54 】 浏览:22
Tags:Hbase REST 访问
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liuzhoulong/article/details/52056497

使用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命令的访问方式:

    baseurl = "http://192.168.119.128:8080";

    #获取表table2中rowkey为liu的行
    response = requests.get(baseurl+'/table2/liu', headers={"Accept" : "application/json"})
    '''
              相当于 curl  -H "Accept:application/json"  http://192.168.119.128:8080/table2/liu
    '''
    print response.json()
    #返回的字段名称和值为base64编码的,需要解密查看
    print base64.b64decode(u'bW9ibGllOg==')

    #查看集群状态
    response = requests.get(baseurl+'/status/cluster', headers={"Accept" : "application/json"})
    '''
         相当于 curl  -H "Accept:application/json"  http://192.168.119.128:8080/status/cluster
    '''
    
    print response.json()

    #查看集群版本
    response = requests.get(baseurl+'/version/cluster', headers={"Accept" : "application/json"})
    '''
             相当于  curl  -H "Accept:application/json"  http://192.168.119.128:8080/status/cluster
    '''
    print response.json()

    #获得表list
    response = requests.get(baseurl+'/', headers={"Accept" : "application/json"})
    '''
             相当于  curl  -H "Accept:application/json"  http://192.168.119.128:8080/
    '''
    print response.json()
    
    #table2中添加一行数据rowkey为moblie,xml字段名称和数据base64编码
    rdata='<xml version="1.0" encoding="UTF-8" standalone="yes"><CellSet><Row key="bW9ibGllOg=="><Cell column="bW9ibGllOg==">bGl1emhvdWxvbmcy</Cell></Ro    w></CellSet>'
    response = requests.put(baseurl+'/table2/moblie', data=rdata,headers = {'content-type': 'text/xml'})
    print response
    
    #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"
    #添加表users
    rdata='<xml version="1.0" encoding="UTF-8"><TableSchema name="users"><ColumnSchema name="cf" /></TableSchema>'
    response = requests.post(baseurl+'/users/schema', data=rdata,headers = {'content-type': 'text/xml'})
    print response
    #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"
    
    #删除表users
    response = requests.delete(baseurl+'/users/schema')
    print response
    #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数据。



】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇启动Hbase shell 下一篇操作meta表

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目