MAPREDUCE服务 MRS-修改HBase表:代码样例

时间:2024-11-25 19:32:58

代码样例

以下代码片段在com.huawei.bigdata.hbase.examples包的“HBaseExample”类的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 = TableDescriptorBuilder.newBuilder(tableName).build();

        // Check whether the column family is specified before modification.
        if (!htd.hasColumnFamily(familyName)) {
            // Create the column descriptor.
         ColumnFamilyDescriptor cfd = ColumnFamilyDescriptorBuilder.of(familyName);
         TableDescriptor td = TableDescriptorBuilder.newBuilder(admin.getDescriptor(tableName)).setColumnFamily(cfd).build();
            // Disable the table to get the table offline before modifying
            // the table.
            admin.disableTable(tableName);//注[1]
            // Submit a modifyTable request.
            admin.modifyTable(td);
            // 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.");
}
support.huaweicloud.com/devg-mrs/mrs_06_0020.html