Hbase rest 举例

作者: Artemis迷 | 来源:发表于2016-07-26 14:33 被阅读2631次

    写在前面:

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

    项目下载

    相关文章

      网友评论

        本文标题:Hbase rest 举例

        本文链接:https://www.haomeiwen.com/subject/damnjttx.html