Hbase 的restful api 的demo例子
[Java]代碼
package hbase;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Decoder;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.utils.JsonUtil;
import com.thinkgem.jeesite.modules.vis.entity.MacroEconomy;
/**
*
* @Description: RESTFulClient 工具類
* @author
* @date 2015年10月19日
* @version V1.0
*/
public class URLRESTFulClientUtils {
private static Logger log = LoggerFactory.getLogger(URLRESTFulClientUtils.class);
static HttpURLConnection httpConnection=null;
static Gson gson =new Gson();
static BASE64Decoder decoder = new BASE64Decoder();
static List<Object> list=new ArrayList();
static List<MacroEconomy> listMacroEconomy=new ArrayList();
static List<String> listString=new ArrayList();
static JsonObject object=new JsonObject();
static JsonArray json=new JsonArray();
static Map<String, Object> mp = new HashMap<String, Object>();
static Map<String, Object> mpcell = new HashMap<String, Object>();
@Test
public static List<String> scanner(String hbaseURL, String method){
try {
URL restServiceURL = new URL(hbaseURL);
httpConnection = (HttpURLConnection) restServiceURL.openConnection();
if (StringUtils.isBlank(method)) {
method="GET";
}
System.out.println(method+"-------"+hbaseURL);
httpConnection.setRequestMethod(method);
// httpConnection.setRequestProperty("Accept", "application/json");
httpConnection.setRequestProperty("Accept", "application/json");
if (httpConnection.getResponseCode() != 200) {
throw new RuntimeException("HTTP GET Request Failed with Error code : " + httpConnection.getResponseCode());
}
InputStreamReader isr = new InputStreamReader(httpConnection.getInputStream(), "UTF-8");
BufferedReader responseBuffer = new BufferedReader(isr);
String output;
while ((output = responseBuffer.readLine()) != null) {
object=JsonUtil.parseJson(output);
System.out.println("--out " +output);
}
String[] bas=object.toString().split(",\"");
for (String bbb: bas) {
if (bbb.startsWith("$\":\"")) {
byte[] aaa= decoder.decodeBuffer((String) bbb.subSequence(4, 176));
String afterdecoder=new String(aaa, "utf-8");
if (afterdecoder.endsWith("?")) {
afterdecoder=afterdecoder.replace("?", "");
}
listString.add(afterdecoder);
}
}
httpConnection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}finally{
}
return listString;
}
public static void main(String[] args) {
List<String> listStr=new ArrayList();
// listStr=URLRESTFulClientUtils.scanner("http://192.168.2.100:6080/macro_economy/*", "GET"); //全表查詢
// listStr=URLRESTFulClientUtils.scanner("http://192.168.2.100:6080/macro_economy/r9", "GET");
// listStr=URLRESTFulClientUtils.scanner("http://192.168.2.100:6080/macro_economy/schema", "DELETE"); //刪除表
// listStr=URLRESTFulClientUtils.scanner("http://192.168.2.100:6080/macro_economy/r3", "DELETE");//刪除單條記錄
// listStr=URLRESTFulClientUtils.scanner("http://192.168.2.100:6080//macro_economy/scanner?timestamp=1445397011347&column= json:json&start_row=r1&stop_row=r3", "GET");//獲取單條記錄
// listStr=URLRESTFulClientUtils.scanner("http://192.168.2.100:6080//macro_economy/scanner?column= json:json&start_row=r1&stop_row=r3", "GET");//獲取單條記錄
listStr=URLRESTFulClientUtils.scanner("http://192.168.2.100:6080/macro_economy/start_row=r1&stop_row=r3", "GET");//獲取單條記錄
// listStr=URLRESTFulClientUtils.scanner("http://192.168.2.100:6080/macro_economy/enable", "");//獲取單條記錄
for (int i = 0; i < listStr.size(); i++) {
System.out.println("-listStr--"+listStr.get(i));
}
}
}
本文由用戶 hwl0420 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!