更新时间:2024-06-05 GMT+08:00
分享

修改HBase

功能简介

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

代码样例

以下代码片段在com.huawei.bigdata.hbase.examples包的“HBaseSample”类的testModifyTable方法中

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.getDescriptor(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);//注[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.");
  }

注意事项

注[1] modifyTable只有表被disable时,才能生效。

相关文档