
# 使用REST接口操作HBase表
#### 功能简介
使用REST服务，传入对应host与port组成的url以及指定的tableName和jsonHTD，通过HTTP协议，进行查询表信息，修改表，创建表以及删除表的操作。
#### 代码样例
- 方法调用
  ```
  // 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\"}]}");
  ```
  
- 查询表信息 以下代码片段在"hbase-rest-example\\src\\main\\java\\com\\huawei\\hadoop\\hbase\\examples"包的"HBaseRestTest"类的descTable方法中。
  ```
  private void descTable(String url, String tableName) {
       String endpoint = "/" + tableName + "/schema";
       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"类的modifyTable方法中。
  ```
  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));
   }
  ```
  
- 创建表
  以下代码片段在"hbase-rest-example\\src\\main\\java\\com\\huawei\\hadoop\\hbase\\examples"包的"HBaseRestTest"类的createTable方法中。
  ```
  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));
   }
  ```
  
- 删除表
  以下代码片段在"hbase-rest-example\\src\\main\\java\\com\\huawei\\hadoop\\hbase\\examples"包的"HBaseRestTest"类的deleteTable方法中。
  ```
  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));
   }
  ```
  
 
