文档首页 > > 开发指南> HBase应用开发> 开发程序> 创建表

创建表

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

功能简介

HBase通过org.apache.hadoop.hbase.client.Admin对象的createTable方法来创建表,并指定表名、列族名。创建表有两种方式,建议采用预分Region建表方式:

  • 快速建表,即创建表后整张表只有一个Region,随着数据量的增加会自动分裂成多个Region。
  • 预分Region建表,即创建表时预先分配多个Region,此种方法建表可以提高写入大量数据初期的数据写入速度。

表的列名以及列族名不能包含特殊字符,可以由字母、数字以及下划线组成。

代码样例

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

MRS 2.0.0及以后版本请使用以下代码创建表:
public static void testCreateTable() {
    LOG.info("Entering testCreateTable.");
    // Set the column family name.
    byte [] fam = Bytes.toBytes("info");
    ColumnFamilyDescriptor familyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(fam)
        // Set data encoding methods. HBase provides DIFF,FAST_DIFF,PREFIX        
        // HBase 2.0 removed `PREFIX_TREE` Data Block Encoding from column families.
        .setDataBlockEncoding(DataBlockEncoding.FAST_DIFF)
        // Set compression methods, HBase provides two default compression        
        // methods:GZ and SNAPPY        
        // GZ has the highest compression rate,but low compression and        
        // decompression effeciency,fit for cold data        
        // SNAPPY has low compression rate, but high compression and        
        // decompression effeciency,fit for hot data.        
        // it is advised to use SANPPY
        .setCompressionType(Compression.Algorithm.SNAPPY)
        .build();
    TableDescriptor htd =
          TableDescriptorBuilder.newBuilder(tableName).setColumnFamily(familyDescriptor).build();

    Admin admin = null;
    try {
        // Instantiate an Admin object.
        admin = conn.getAdmin();
        if (!admin.tableExists(tableName)) {
            LOG.info("Creating table...");
            // create table
            admin.createTable(htd);
            LOG.info(admin.getClusterMetrics());
            LOG.info(admin.listNamespaceDescriptors());
            LOG.info("Table created successfully.");
        } else {
            LOG.warn("table already exists");
        }
    } catch (IOException e) {
        LOG.error("Create table failed.", e);
    } finally {
        if (admin != null) {
            try {
                // Close the Admin object.
                admin.close();
            } catch (IOException e) {
                LOG.error("Failed to close admin ", e);
            }
        }
    }
    LOG.info("Exiting testCreateTable.");
}
MRS 2.0.0之前版本请使用以下代码创建表:
public static void testCreateTable() {
    LOG.info("Entering testCreateTable.");
    // Specify the table descriptor.
    HTableDescriptor htd = new HTableDescriptor(tableName);

    // Set the column family name to info.
    HColumnDescriptor hcd = new HColumnDescriptor("info");

    // Set data encoding methods.HBase provides DIFF,FAST_DIFF,PREFIX
    // and PREFIX_TREE
    hcd.setDataBlockEncoding(DataBlockEncoding.FAST_DIFF);

    // Set compression methods,HBase provides two default compression
    // methods:GZ and SNAPPY
    // GZ has the highest compression rate,but low compression and
    // decompression effeciency,fit for cold data
    // SNAPPY has low compression rate, but high compression and
    // decompression effeciency,fit for hot data.
    // it is advised to use SANPPY
    hcd.setCompressionType(Compression.Algorithm.SNAPPY);

    htd.addFamily(hcd);

    Admin admin = null;
    try {
        // Instantiate an Admin object.
        admin = conn.getAdmin();
        if (!admin.tableExists(tableName)) {
            LOG.info("Creating table...");
            // create table
            admin.createTable(htd);
            LOG.info(admin.getClusterStatus());
            LOG.info(admin.listNamespaceDescriptors());
            LOG.info("Table created successfully.");
        } else {
            LOG.warn("table already exists");
        }
    } catch (IOException e) {
        LOG.error("Create table failed.", e);
    } finally {
        if (admin != null) {
            try {
                // Close the Admin object.
                admin.close();
            } catch (IOException e) {
                LOG.error("Failed to close admin ", e);
            }
        }
    }
    LOG.info("Exiting testCreateTable.");
}

代码解释

(1)创建表描述符

(2)创建列族描述符

(3)添加列族描述符到表描述符中

分享:

    相关文档

    相关产品

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

提交成功!

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

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

*必选

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

字符长度不能超过200

提交反馈 取消

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

智能客服提问云社区提问