
# 使用REST接口查询HBase集群信息
#### 功能简介
使用REST服务，传入对应host与port组成的url，通过HTTP协议，获取集群版本与状态信息。
#### 代码样例
- 连接RestServer服务 普通模式下，用户不需要登录即可连接RestServer服务。所以请将"hbase-rest-example\\src\\main\\java\\com\\huawei\\hadoop\\hbase\\examples"包的"HBaseRestTest"类的main方法中与登录相关代码语句如下所示进行注释：
  ```
  //In Windows environment
          //String userdir = HBaseRestTest.class.getClassLoader().getResource("conf").getPath() + File.separator;
          //In Linux environment
          //String userdir = System.getProperty("user.dir") + File.separator + "conf" + File.separator;
          //userKeytabFile = userdir + "user.keytab";
          //krb5File = userdir + "krb5.conf";
          //String principal = "hbaseuser1";
          //login(principal, userKeytabFile, krb5File);
  ```
  以下代码片段在"hbase-rest-example\\src\\main\\java\\com\\huawei\\hadoop\\hbase\\examples"包的"HBaseRestTest"类的main方法中。
  ```
  // RESTServer's hostname.
          String restHostName = "10.120.16.170";[1]
          String securityModeUrl = new StringBuilder("https://").append(restHostName).append(":21309").toString();
          String nonSecurityModeUrl = new StringBuilder("http://").append(restHostName).append(":21309").toString();
          HBaseRestTest test = new HBaseRestTest();
          //If cluster is non-security mode，use nonSecurityModeUrl as  parameter.
          test.test(nonSecurityModeUrl);[2]
  ```
  MRS 3.6.0-LTS及之后版本：
  ```
  // Specify the Rest server hostnames, separated by comma if multiple are there.[1]
          String restServerHostNames = "xxx.xxx.xxx.x,xxx.xxx.xxx.xx,xxx.xxx.xxx.xxx";
          for (String hostname : restServerHostNames.split(",")) {
              try {
                  String securityModeUrl = new StringBuilder("https://").append(hostname).append(":21309").toString();
                  String nonSecurityModeUrl = new StringBuilder("http://").append(hostname).append(":21309").toString();
                  HBaseRestTest test = new HBaseRestTest();
                  //If cluster is non-security mode，use nonSecurityModeUrl as parameter.
                  test.test(nonSecurityModeUrl);[2]
                  break;
              } catch (IOException e) {
                  LOG.error("Failed to connect to the host: {}, retrying with other servers if any", hostname, e);
              }
          }
  ```
  \[1\]修改**restHostName**为待访问的RestServer实例所在节点的IP地址，建议部署多个RESTServer增加业务可靠性，多个RESTServer IP以逗号进行分隔，并将节点IP配置到运行样例代码的本机hosts文件中。
  \[2\]非安全模式采用http模式进行访问HBase REST服务，传入"nonSecurityModeUrl作为test.test()"参数。
  
- 获取集群版本信息 以下代码片段在"hbase-rest-example\\src\\main\\java\\com\\huawei\\hadoop\\hbase\\examples"包的"HBaseRestTest"类的getClusterVersion方法中。
  ```
  private void getClusterVersion(String url) {
       String endpoint = "/version/cluster";
       Optional<ResultModel> result = sendAction(url + endpoint, MethodType.GET, null);
       handleNormalResult((Optional<ResultModel>) result);
   }
  ```
  
- 获取集群状态信息 以下代码片段在"hbase-rest-example\\src\\main\\java\\com\\huawei\\hadoop\\hbase\\examples"包的"HBaseRestTest"类的getClusterStatus方法中。
  ```
  private void getClusterStatus(String url) {
       String endpoint = "/status/cluster";
       Optional<ResultModel> result = sendAction(url + endpoint, MethodType.GET, null);
       handleNormalResult(result);
   }
  ```
  
 
