hbase rest 访问_Hbase rest 举例

写在前面:

启动rest服务,使用http请求操作hbase(设置端口为8080)。对于hbase rest的使用是对java内容的屏蔽,如果你是java程序员请不要这么做!插入数据不推荐使用,对于查找get rowkey的结果较为方便,但是也不推荐使用。适用于对java不熟悉 或者针对项目演示的需求。

1.获取版本及集群环境信息(粗略)

1.1获取表list(get请求):

http://example.com:8080/

结果返回:

{"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的子类

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;

}

返回结果:

{"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的子类

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;

}

返回结果:

新建成功: 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的子类

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;

}

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的子类

int statusCode = httpResponse.getStatusLine().getStatusCode();

if (statusCode == 200) {

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;

}

2.写入数据

返回结果:

成功: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.获取数据

返回结果:

成功:200 , 结果信息(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的子类

int statusCode = httpResponse.getStatusLine().getStatusCode();

if (statusCode == 200 || statusCode == 403) {

HttpEntity httpEntity = httpResponse.getEntity();

result = EntityUtils.toString(httpEntity);// 取出应答字符串

// 一般来说都要删除多余的字符

// 去掉返回结果中的"\r"字符,否则会在结果字符串后面显示一个小方格

// result.replaceAll("\r", "");

} else {

getR.abort();

result = "没有返回正确的状态码,请仔细检查请求表名及参数格式!";

}

} catch (ClientProtocolException e) {

e.printStackTrace();

result = e.getMessage().toString();

} catch (IOException e) {

e.printStackTrace();

result = e.getMessage().toString();

}

return result;

}

返回结果:

成功:200 , 结果信息(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的子类

int statusCode = httpResponse.getStatusLine().getStatusCode();

if (statusCode == 200 || statusCode == 403) {

HttpEntity httpEntity = httpResponse.getEntity();

result = EntityUtils.toString(httpEntity);// 取出应答字符串

// 一般来说都要删除多余的字符

// 去掉返回结果中的"\r"字符,否则会在结果字符串后面显示一个小方格

// result.replaceAll("\r", "");

} else {

getR.abort();

result = "没有返回正确的状态码,请仔细检查请求表名及参数格式!";

}

} catch (ClientProtocolException e) {

e.printStackTrace();

result = e.getMessage().toString();

} catch (IOException e) {

e.printStackTrace();

result = e.getMessage().toString();

}

return result;

}

项目下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值