Updated on 2024-04-02 GMT+08:00

Operate Tables Using REST

Function

Use the REST service to transfer the URL consisting of the host and port as well as the specified tableName and jsonHTD to query, modify, create, and delete table information through HTTP.

Example Code

  • Invoking methods
    // Add a table with specified info.
    createTable(url, "testRest",
         "{\"name\":\"default:testRest\",\"ColumnSchema\":[{\"name\":\"cf1\"}," + "{\"name\":\"cf2\"}]}");
    
    // Add column family 'testCF1' if not exist, else update the 'VERSIONS' to 3.
     // Notes: The unspecified property of this column family will be updated to default value.
    modifyTable(url, "testRest",
         "{\"name\":\"testRest\",\"ColumnSchema\":[{\"name\":\"testCF1\"," + "\"VERSIONS\":\"3" + "\"}]}");
    
    // Describe specific Table.
    descTable(url, "default:testRest");
    
    // delete a table with specified info.
    deleteTable(url, "default:testRest",
         "{\"name\":\"default:testRest\",\"ColumnSchema\":[{\"name\":\"testCF\"," + "\"VERSIONS\":\"3\"}]}");
  • Querying table information

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

    private void descTable(String url, String tableName) {
         String endpoint = "/" + tableName + "/schema";
         Optional<ResultModel> result = sendAction(url + endpoint, MethodType.GET, null);
         handleNormalResult((Optional<ResultModel>) result);
     }
  • Modifying table information

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

    private void modifyTable(String url, String tableName, String jsonHTD) {
         LOG.info("Start modify table.");
         String endpoint = "/" + tableName + "/schema";
         JsonElement tableDesc = new JsonParser().parse(jsonHTD);
    
         // Add a new column family or modify it.
         handleNormalResult(sendAction(url + endpoint, MethodType.POST, tableDesc));
     }
  • Creating a table

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

    private void createTable(String url, String tableName, String jsonHTD) {
         LOG.info("Start create table.");
         String endpoint = "/" + tableName + "/schema";
         JsonElement tableDesc = new JsonParser().parse(jsonHTD);
    
         // Add a table.
         handleCreateTableResult(sendAction(url + endpoint, MethodType.PUT, tableDesc));
     }
  • Deleting a table

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

    private void deleteTable(String url, String tableName, String jsonHTD) {
         LOG.info("Start delete table.");
         String endpoint = "/" + tableName + "/schema";
         JsonElement tableDesc = new JsonParser().parse(jsonHTD);
    
         // delete a table.
         handleNormalResult(sendAction(url + endpoint, MethodType.DELETE, tableDesc));
     }