使用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資料。