Hbase 的restful api 的demo例子

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

[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));
			}
		}
}
      

标签: Google 代码

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:把android activity修改为悬浮窗口

下一篇:使用SwipeRefreshLayout,实现下拉刷新