更新时间:2024-04-16 GMT+08:00
分享

修改表

功能介绍

HBase通过org.apache.hadoop.hbase.client.Admin的modifyTable方法修改表信息。

代码样例

  • 取消冷热时间线。
    public void testModifyTable() {
      LOG.info("Entering testModifyTable.");
    
      // Specify the column family name.
      byte[] familyName = Bytes.toBytes("info");
      Admin admin = null;
      try {
        // Instantiate an Admin object.
        admin = conn.getAdmin();
        // Obtain the table descriptor.
        HTableDescriptor htd = admin.getTableDescriptor(tableName);
        // Check whether the column family is specified before modification.
        if (!htd.hasFamily(familyName)) {
          // Create the column descriptor.
          HColumnDescriptor hcd = new HColumnDescriptor(familyName);
    
          //Disable hot and cold separation.
          hcd .setValue(HColumnDescriptor.COLD_BOUNDARY, null);
    
          htd.addFamily(hcd);
          // Disable the table to get the table offline before modifying
          // the table.
          admin.disableTable(tableName);
          // Submit a modifyTable request.
          admin.modifyTable(tableName, htd);  //注[1]
          // Enable the table to get the table online after modifying the
          // table.
          admin.enableTable(tableName);
        }
        LOG.info("Modify table successfully.");
      } catch (IOException e) {
        LOG.error("Modify table failed " ,e);
      } finally {
        if (admin != null) {
          try {
            // Close the Admin object.
            admin.close();
          } catch (IOException e) {
            LOG.error("Close admin failed " ,e);
          }
        }
      }
      LOG.info("Exiting testModifyTable.");
    }
    • 注意事项。

      注[1] 只有在调用disableTable接口后, 再调用modifyTable接口才能将表修改成功。之后,请调用enableTable接口重新启用表。

      注[1] 指的是代码样例中的“admin.modifyTable(tableName, htd); //注[1]”。

  • 设置已有表的冷热分离功能。
    public void testModifyTable() {
      LOG.info("Entering testModifyTable.");
    
      // Specify the column family name.
      byte[] familyName = Bytes.toBytes("info");
      Admin admin = null;
      try {
        // Instantiate an Admin object.
        admin = conn.getAdmin();
        // Obtain the table descriptor.
        HTableDescriptor htd = admin.getTableDescriptor(tableName);
        // Check whether the column family is specified before modification.
        if (!htd.hasFamily(familyName)) {
          // Create the column descriptor.
          HColumnDescriptor hcd = new HColumnDescriptor(familyName);
    
          //Set the hot and cold separation function for an existing table.
          hcd .setValue(HColumnDescriptor.COLD_BOUNDARY, "86400"); 
    
          htd.addFamily(hcd);
          // Disable the table to get the table offline before modifying
          // the table.
          admin.disableTable(tableName);
          // Submit a modifyTable request.
          admin.modifyTable(tableName, htd);  //注[1]
          // Enable the table to get the table online after modifying the
          // table.
          admin.enableTable(tableName);
        }
        LOG.info("Modify table successfully.");
      } catch (IOException e) {
        LOG.error("Modify table failed " ,e);
      } finally {
        if (admin != null) {
          try {
            // Close the Admin object.
            admin.close();
          } catch (IOException e) {
            LOG.error("Close admin failed " ,e);
          }
        }
      }
      LOG.info("Exiting testModifyTable.");
    }

注意事项

注[1] 只有在调用disableTable接口后, 再调用modifyTable接口才能将表修改成功。之后,请调用enableTable接口重新启用表。

注[1]指的是代码样例中的“admin.modifyTable(tableName, htd); //注[1]”。

相关文档