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

Modifying a Table

Function

Modify table information using the modifyTable method of org.apache.hadoop.hbase.client.Admin.

Example Code

The following code snippet belongs to the testModifyTable method in the HBaseSample class of the com.huawei.bigdata.hbase.examples package.

public void testModifyTable() {
    LOG.info("Entering testModifyTable.");

    // Specify the column family name.
    byte[] familyName = Bytes.toBytes("education");
    Admin admin = null;
    try {
      // Instantiate an Admin object.
      admin = conn.getAdmin();
      // Obtain the table descriptor.
      TableDescriptor htd = admin.getTableDescriptor(tableName);

      // Check whether the column family is specified before modification.
      if (!htd.hasColumnFamily(familyName)) {
        // Create the column descriptor.
        TableDescriptor tableBuilder = TableDescriptorBuilder.newBuilder(htd) 
            .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(familyName).build()).build();


        // Disable the table to get the table offline before modifying
        // the table.
        admin.disableTable(tableName);// Note[1]
        // Submit a modifyTable request.
        admin.modifyTable(tableBuilder);
        // 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.");
  }

Precautions

modifyTable takes effect only when a table is disabled.