Updated on 2022-09-14 GMT+08:00

Operate Namespaces Using REST

Function

Use the REST service to import the URL consisting of the host and port and the specified namespace, Use HTTPS to create, query, and delete namespaces, and obtain tables in the specified namespace.

Example Code

  • Invoking methods
    // Namespace operations.
    createNamespace(url, "testNs");
     getAllNamespace(url);
     deleteNamespace(url, "testNs");
     getAllNamespaceTables(url, "default");
  • Creating a namespace

    The following code snippets are in the createNamespace method in the HBaseRestTest class of the hbase-rest-example\src\main\java\com\huawei\hadoop\hbase\examples packet.

    private void createNamespace(String url, String namespace) {
         String endpoint = "/namespaces/" + namespace;
         Optional<ResultModel> result = sendAction(url + endpoint, MethodType.POST, null);
         if (result.orElse(new ResultModel()).getStatusCode() == HttpStatus.SC_CREATED) {
             LOG.info("Create namespace '{}' success.", namespace);
         } else {
             LOG.error("Create namespace '{}' failed.", namespace);
         }
     }
  • Querying all namespaces

    The following code snippets are in the getAllNamespace method in the HBaseRestTest class of the hbase-rest-example\src\main\java\com\huawei\hadoop\hbase\examples packet.

    private void getAllNamespace(String url) {
         String endpoint = "/namespaces";
         Optional<ResultModel> result = sendAction(url + endpoint, MethodType.GET, null);
         handleNormalResult(result);
     }
  • Deleting a specified namespace

    The following code snippets are in the deleteNamespace method in the HBaseRestTest class of the hbase-rest-example\src\main\java\com\huawei\hadoop\hbase\examples packet.

    private void deleteNamespace(String url, String namespace) {
         String endpoint = "/namespaces/" + namespace;
         Optional<ResultModel> result = sendAction(url + endpoint, MethodType.DELETE, null);
         if (result.orElse(new ResultModel()).getStatusCode() == HttpStatus.SC_OK) {
             LOG.info("Delete namespace '{}' success.", namespace);
         } else {
             LOG.error("Delete namespace '{}' failed.", namespace);
         }
     }
  • Obtain tables in a specified namespace.

    The following code snippets are in the getAllNamespaceTables method in the HBaseRestTest class of the hbase-rest-example\src\main\java\com\huawei\hadoop\hbase\examples packet.

    private void getAllNamespaceTables(String url, String namespace) {
         String endpoint = "/namespaces/" + namespace + "/tables";
         Optional<ResultModel> result = sendAction(url + endpoint, MethodType.GET, null);
         handleNormalResult(result);
     }