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

  • 1.2获取表结构(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"}

    程序代码举例:

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

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

项目下载

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • apache下的httpclient工具可大大简化开发过程中的点对点通信,本人将以微信多媒体接口为例,展示http...
    划破的天空阅读 10,754评论 0 32
  • 经验:1.在网络端接收数据后写入本地时要用write(byte[],0,len),不要用write(byte[])...
    123yuan123阅读 2,994评论 0 2
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,422评论 19 139
  • 该文仅对于中间这种支付方式有参考价值哟 一、开发背景 在微信公众号中,需要进行微信支付且为微信公众号网页支付。 二...
    英文名叫夏天阅读 5,854评论 0 7
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,946评论 18 399