寫(xiě)在前面:
啟動(dòng)rest服務(wù)挣磨,使用http請(qǐng)求操作hbase(設(shè)置端口為8080)牙肝。對(duì)于hbase rest的使用是對(duì)java內(nèi)容的屏蔽,如果你是java程序員請(qǐng)不要這么做断国!插入數(shù)據(jù)不推薦使用撕彤,對(duì)于查找get rowkey的結(jié)果較為方便鱼鸠,但是也不推薦使用。適用于對(duì)java不熟悉 或者針對(duì)項(xiàng)目演示的需求羹铅。
1.獲取版本及集群環(huán)境信息(粗略)
- 1.1獲取表list(get請(qǐng)求):
http://example.com:8080/
結(jié)果返回:{"table":[{"name":"TSL_EvcRealTimeData"},{"name":"test"},{"name":"test2"}]}
程序代碼舉例:
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的子類(lèi)
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == 200 || statusCode == 403) {
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);// 取出應(yīng)答字符串
// 一般來(lái)說(shuō)都要?jiǎng)h除多余的字符
// 去掉返回結(jié)果中的"\r"字符蚀狰,否則會(huì)在結(jié)果字符串后面顯示一個(gè)小方格
// 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;
}
-
1.2獲取表結(jié)構(gòu)(get請(qǐng)求):
"http://example.com:8080/tableName/schema"
返回結(jié)果:
{"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"}
程序代碼舉例:
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的子類(lèi)
int statusCode = httpResponse.getStatusLine().getStatusCode();
f (statusCode == 200 || statusCode == 403) {
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);// 取出應(yīng)答字符串
// 一般來(lái)說(shuō)都要?jiǎng)h除多余的字符
// 去掉返回結(jié)果中的"\r"字符,否則會(huì)在結(jié)果字符串后面顯示一個(gè)小方格
// 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;
}
-
1.3創(chuàng)建一張表(post請(qǐng)求):
http://example.com:8000/newTableName/schema
返回結(jié)果:
新建成功: 201 表存在并替換成功:200
程序代碼舉例:
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的子類(lèi)
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == 200||statusCode == 201) {
HttpEntity httpEntity = httpResponse.getEntity();
result = "code=>"+statusCode+":"+EntityUtils.toString(httpEntity);// 取出應(yīng)答字符串
// 一般來(lái)說(shuō)都要?jiǎng)h除多余的字符
// 去掉返回結(jié)果中的"\r"字符睦裳,否則會(huì)在結(jié)果字符串后面顯示一個(gè)小方格
// result.replaceAll("\r", "");
} else {
httpRequst.abort();
result = "沒(méi)有返回正確的狀態(tài)碼造锅,請(qǐng)仔細(xì)檢查請(qǐng)求表名";
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = e.getMessage().toString();
} catch (IOException e) {
e.printStackTrace();
result = e.getMessage().toString();
}
return result;
}
-
1.4刪除一張表(delete請(qǐng)求):
http://example.com:8000/deleteTableName/schema
返回結(jié)果:
成功:200
public static String deleteHtable(String deteleTableName,String jsonOrXml) {
String uriAPI = "http://121.199.28.106:8080/" + deteleTableName + "/schema";
String result = "";
HttpDelete httpRequst = new HttpDelete(uriAPI);
try {
httpRequst.setHeader("accept", jsonOrXml);
httpRequst.setHeader("Content-Type", jsonOrXml);
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequst);
// 其中HttpGet是HttpUriRequst的子類(lèi)
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
result = "code=>"+statusCode+":"+EntityUtils.toString(httpEntity);// 取出應(yīng)答字符串
// 一般來(lái)說(shuō)都要?jiǎng)h除多余的字符
// 去掉返回結(jié)果中的"\r"字符,否則會(huì)在結(jié)果字符串后面顯示一個(gè)小方格
// result.replaceAll("\r", "");
} else {
httpRequst.abort();
result = "沒(méi)有返回正確的狀態(tài)碼廉邑,請(qǐng)仔細(xì)檢查請(qǐng)求表名";
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = e.getMessage().toString();
} catch (IOException e) {
e.printStackTrace();
result = e.getMessage().toString();
}
return result;
}
2.寫(xiě)入數(shù)據(jù)
-
2.1 寫(xiě)入一條rowkey數(shù)據(jù)(put請(qǐng)求)
http://example:8080/putTableName/fakerow
返回結(jié)果:
成功:200
程序代碼舉例:
public static String writeRowInTableByJson(String tableName, String jsonStr) {
String uriAPI = "http://121.199.28.106:8080/" + tableName + "/fakerow";
StringBuilder result = new StringBuilder();
HttpPut put = new HttpPut(uriAPI);
try {
put.addHeader("Accept", "application/json");
put.addHeader("Content-Type", "application/json");
// JSONObject jsonObject = JSONObject.fromObject(jsonStr);
StringEntity input = null;
try {
input = new StringEntity(jsonStr);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
put.setEntity(input);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(put);
int status = httpResponse.getStatusLine().getStatusCode();
if ( status != 200) {
throw new RuntimeException("Failed : HTTP error code : " + httpResponse.getStatusLine().getStatusCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((httpResponse.getEntity().getContent())));
String output;
while ((output = br.readLine()) != null) {
result.append(output);
}
result.append("-code:"+status);
} catch (Exception e) {
e.printStackTrace();
}
return result.toString();
}
3.獲取數(shù)據(jù)
-
3.1 獲取一條rowkey信息(get請(qǐng)求)
http://example:8080/TableName/rowkey
返回結(jié)果:
成功:200 , 結(jié)果信息(xml/json)
程序代碼舉例:
public static String getRowKey(String tableName, String rowkey,String paramInfo, String xmlOrJson) {
String uriAPI = "http://121.199.28.106:8080/" + tableName + "/" + rowkey+"/info:"+paramInfo;
String result = "";
HttpGet getR = new HttpGet(uriAPI);
try {
getR.setHeader("accept", xmlOrJson);
HttpResponse httpResponse = new DefaultHttpClient().execute(getR);
// 其中HttpGet是HttpUriRequst的子類(lèi)
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == 200 || statusCode == 403) {
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);// 取出應(yīng)答字符串
// 一般來(lái)說(shuō)都要?jiǎng)h除多余的字符
// 去掉返回結(jié)果中的"\r"字符倒谷,否則會(huì)在結(jié)果字符串后面顯示一個(gè)小方格
// result.replaceAll("\r", "");
} else {
getR.abort();
result = "沒(méi)有返回正確的狀態(tài)碼蛛蒙,請(qǐng)仔細(xì)檢查請(qǐng)求表名及參數(shù)格式!";
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = e.getMessage().toString();
} catch (IOException e) {
e.printStackTrace();
result = e.getMessage().toString();
}
return result;
}
-
3.2 scan 掃描 (get請(qǐng)求)
http://example:8080/tableName/*?startrow=a&endrow=z
返回結(jié)果:
成功:200 渤愁, 結(jié)果信息(xml/json)
程序代碼舉例:
public static String scan(String tableName,String startRowkey,String endRowkey,String[] params,String xmlOrJson){
String uriAPI = "http://121.199.28.106:8080/" + tableName + "/*?startrow=" + startRowkey+"&endrow="+endRowkey;
String result = "";
HttpGet getR = new HttpGet(uriAPI);
try {
getR.setHeader("accept", xmlOrJson);
HttpResponse httpResponse = new DefaultHttpClient().execute(getR);
// 其中HttpGet是HttpUriRequst的子類(lèi)
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == 200 || statusCode == 403) {
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);// 取出應(yīng)答字符串
// 一般來(lái)說(shuō)都要?jiǎng)h除多余的字符
// 去掉返回結(jié)果中的"\r"字符牵祟,否則會(huì)在結(jié)果字符串后面顯示一個(gè)小方格
// result.replaceAll("\r", "");
} else {
getR.abort();
result = "沒(méi)有返回正確的狀態(tài)碼,請(qǐng)仔細(xì)檢查請(qǐng)求表名及參數(shù)格式抖格!";
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = e.getMessage().toString();
} catch (IOException e) {
e.printStackTrace();
result = e.getMessage().toString();
}
return result;
}