文档首页 > > 开发指南> HBase应用开发> 开发程序> 修改表

修改表

分享
更新时间: 2020/01/11 GMT+08:00

功能简介

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

代码样例

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

MRS 2.0.0及以后的版本使用以下代码修改表:
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);
            // 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.");
}
MRS 2.0.0之前的版本使用以下代码修改表:
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.
        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);
         htd.addFamily(hcd);
            // Disable the table to get the table offline before modifying
            // the table.
            admin.disableTable(tableName);//注[1]
            // Submit a modifyTable request.
            admin.modifyTable(tableName, htd);
            // 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时,才能生效。

分享:

    相关文档

    相关产品

文档是否有解决您的问题?

提交成功!

非常感谢您的反馈,我们会继续努力做到更好!

反馈提交失败,请稍后再试!

*必选

请至少选择或填写一项反馈信息

字符长度不能超过200

提交反馈 取消

如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨

智能客服提问云社区提问