hbase 提供了rest api来操作访问hbase.
1. 启动REST服务
a.启动一个非守护进程模式的REST服务器(ctrl+c 终止)
bin/hbase rest start
b.启动守护进程模式的REST服务器
bin/hbase-daemon.sh start rest
默认启动的是8080端口(可以使用参数在启动时指定端口 hbase-daemon.sh start rest -p8080),可以被访问。eg. curl http://:8080/
2.获取表list(get请求):
http://example.com:8080/
返回结果:
{"table":[{"name":"TSL_EvcRealTimeData"},{"name":"test"},{"name":"test2"}]}
java代码案例:
public static String getTableList(String acceptInfo){
String uriAPI = "http://example.com:8080/";
String result = "";
HttpGet httpRequst = new HttpGet(uriAPI);
try {
httpRequst.setHeader("accept", acceptInfo);
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequst);
// 其中HttpGet是HttpUriRequst的子类
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == 200 || statusCode == 403) {
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);// 取出应答字符串
// 一般来说都要删除多余的字符
// 去掉返回结果中的"\r"字符,否则会在结果字符串后面显示一个小方格
// result.replaceAll("\r", "");
} else {
httpRequst.abort();
result = "异常的返回码:"+statusCode;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = e.getMessage().toString();
} catch (IOException e) {
e.printStackTrace();
result = e.getMessage().toString();
}
return result;
}
3.获取表结构(get请求):
http://example.com:8080/tableName/schema
返回结果:
{"name":"TSL_EvcRealTimeData","ColumnSchema":[{"name":"info","BLOOMFILTER":"ROW","VERSIONS":"1","IN_MEMORY":"false","KEEP_DELETED_CELLS":"FALSE","DATA_BLOCK_ENCODING":"NONE","TTL":"2147483647","COMPRESSION":"NONE","MIN_VERSIONS":"0","BLOCKCACHE":"true","BLOCKSIZE":"65536","REPLICATION_SCOPE":"0"}],"IS_META":"false"}
java代码举例:
public static String getSchemaInfo(String tableName, String acceptInfo) {
String uriAPI = "http://example.com:8080/" + tableName + "/schema";
String result = "";
HttpGet httpRequst = new HttpGet(uriAPI);
try {
httpRequst.setHeader("accept", acceptInfo);
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequst);
// 其中HttpGet是HttpUriRequst的子类
int statusCode = httpResponse.getStatusLine().getStatusCode();
f (statusCode == 200 || statusCode == 403) {
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);// 取出应答字符串
// 一般来说都要删除多余的字符
// 去掉返回结果中的"\r"字符,否则会在结果字符串后面显示一个小方格
// result.replaceAll("\r", "");
} else {
httpRequst.abort();
result = "异常的返回码:"+statusCode;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = e.getMessage().toString();
} catch (IOException e) {
e.printStackTrace();
result = e.getMessage().toString();
}
return result;
}
4.创建一张表(post请求):
http://example.com:8000/newTableName/schema
返回结果:
新建成功: 201 表存在并替换成功:200
java代码举例:
public static String createHtable(String newTableName, String jsonOrXmlStr,String jsonOrXml) {
String uriAPI = "http://example.com:8080/" + newTableName + "/schema";
String result = "";
HttpPost httpRequst = new HttpPost(uriAPI);
try {
StringEntity s = new StringEntity(jsonOrXmlStr.toString());
httpRequst.setHeader("accept", jsonOrXml);
httpRequst.setHeader("Content-Type", jsonOrXml);
httpRequst.setEntity(s);
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequst);
// 其中HttpGet是HttpUriRequst的子类
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == 200||statusCode == 201) {
HttpEntity httpEntity = httpResponse.getEntity();
result = "code=>"+statusCode+":"+EntityUtils.toString(httpEntity);// 取出应答字符串
// 一般来说都要删除多余的字符
// 去掉返回结果中的"\r"字符,否则会在结果字符串后面显示一个小方格
// result.replaceAll("\r", "");
} else {
httpRequst.abort();
result = "没有返回正确的状态码,请仔细检查请求表名";
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = e.getMessage().toString();
} catch (IOException e) {
e.printStackTrace();
result = e.getMessage().toString();
}
return result;
}
当然hbase还有很多很丰富的rest api,提供给开发者,上面只是简单举例
5.更多的api接口
Endpoint | HTTP Verb | Description | Example |
---|---|---|---|
|
|
Describe the schema of the specified table. |
curl -vi -X GET \ -H "Accept: text/xml" \ "http://example.com:8000/users/schema" |
|
|
Update an existing table with the provided schema fragment |
curl -vi -X POST \ -H "Accept: text/xml" \ -H "Content-Type: text/xml" \ -d '' \ "http://example.com:8000/users/schema" |
|
|
Create a new table, or replace an existing table’s schema |
curl -vi -X PUT \ -H "Accept: text/xml" \ -H "Content-Type: text/xml" \ -d '' \ "http://example.com:8000/users/schema" |
|
|
Delete the table. You must use the |
curl -vi -X DELETE \ -H "Accept: text/xml" \ "http://example.com:8000/users/schema" |
|
|
List the table regions |
curl -vi -X GET \ -H "Accept: text/xml" \ "http://example.com:8000/users/regions |
具体参考hbase官方文档的hbase章节:http://hbase.apache.org/book.html#_rest