更新时间:2023-11-10 GMT+08:00
分享

使用REST查询集群信息

功能简介

使用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 = "100.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]

    [1]修改restHostName为待访问的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);
     }
分享:

    相关文档

    相关产品