HBase操作(Shell与Java API)

http://blog.csdn.net/u013980127/article/details/52443155

下面代码在hadoop 2.6.4 + hbase 1.2.2 + centos 6.5 + jdk 1.8上运行通过。

HBase操作

一般操作

命令 说明
status 显示集群状态. 选项:‘summary’, ‘simple’, or ‘detailed’. 默认值:‘summary’.
hbase> status
hbase> status ‘simple’
hbase> status ‘summary’
hbase> status ‘detailed’
version 显示版本。
hbase> version
whoami 显示当前用户与组。
hbase> whoami

表管理

1. alter

修改表结构必须先disable

Shell:

语法:alter 't1', {NAME => 'f1'}, {NAME => 'f2', METHOD => 'delete'}
必须指定列族。示例:
表t1的列族f1,修改或增加VERSIONS为5

hbase> alter ‘t1’, NAME => ‘f1’, VERSIONS => 5

也可以同时修改多个列族:

hbase> alter ‘t1’, ‘f1’, {NAME => ‘f2’, IN_MEMORY => true}, {NAME => ‘f3’, VERSIONS => 5}

删除表t1的f1列族:

hbase> alter ‘t1’, NAME => ‘f1’, METHOD => ‘delete’
或
hbase> alter ‘t1’, ‘delete’ => ‘f1’

也可以修改table-scope属性,例如MAX_FILESIZE, READONLY,
MEMSTORE_FLUSHSIZE, DEFERRED_LOG_FLUSH等。
例如,修改region的最大大小为128MB:

hbase> alter ‘t1’, MAX_FILESIZE => ‘134217728’

也可以设置表的coprocessor属性:

hbase> alter ‘t1’,
‘coprocessor’=>’hdfs:///foo.jar|com.foo.FooRegionObserver|1001|arg1=1,arg2=2’

可以设置复数个coprocessor,这时会自动添加序列以唯一标示coprocessor。

coprocessor属性设置语法:
[coprocessor jar file location] | class name | [priority] | [arguments]

也可以设置configuration给表或列族:

hbase> alter ‘t1’, CONFIGURATION => {‘hbase.hregion.scan.loadColumnFamiliesOnDemand’ => ‘true’}
hbase> alter ‘t1’, {NAME => ‘f2’, CONFIGURATION => {‘hbase.hstore.blockingStoreFiles’ => ’10’}}

也可以移除table-scope属性:

hbase> alter ‘t1’, METHOD => ‘table_att_unset’, NAME => ‘MAX_FILESIZE’

hbase> alter ‘t1’, METHOD => ‘table_att_unset’, NAME => ‘coprocessor$1’

可以通过一个命令进行多项修改:

hbase> alter ‘t1’, { NAME => ‘f1’, VERSIONS => 3 },
{ MAX_FILESIZE => ‘134217728’ }, { METHOD => ‘delete’, NAME => ‘f2’ },
OWNER => ‘johndoe’, METADATA => { ‘mykey’ => ‘myvalue’ }

Java实现:

/**
 * 修改表结构,增加列族
 *
 * @param tableName 表名
 * @param family    列族
 *
 * @throws IOException
 */
public static void putFamily(String tableName, String family) throws IOException {
    try (Connection connection = ConnectionFactory.createConnection(configuration);
         Admin admin = connection.getAdmin()
    ) {
        TableName tblName = TableName.valueOf(tableName);
        if (admin.tableExists(tblName)) {
            admin.disableTable(tblName);
            HColumnDescriptor cf = new HColumnDescriptor(family);
            admin.addColumn(TableName.valueOf(tableName), cf);
            admin.enableTable(tblName);
        } else {
            log.warn(tableName + " not exist.");
        }
    }
}

# 调用示例
putFamily("blog", "note");

2. create

创建表。

Shell:

语法:
create 'table', { NAME => 'family', VERSIONS => VERSIONS } [, { NAME => 'family', VERSIONS => VERSIONS }]

示例:

hbase> create ‘t1’, {NAME => ‘f1’, VERSIONS => 5}
hbase> create ‘t1’, {NAME => ‘f1’}, {NAME => ‘f2’}, {NAME => ‘f3’}
hbase> # The above in shorthand would be the following:
hbase> create ‘t1’, ‘f1’, ‘f2’, ‘f3’
hbase> create ‘t1’, {NAME => ‘f1’, VERSIONS => 1, TTL => 2592000, BLOCKCACHE => true}
hbase> create ‘t1’, {NAME => ‘f1’, CONFIGURATION => {‘hbase.hstore.blockingStoreFiles’ => ’10’}}

Java示例:

/**
 * 创建表
 *
 * @param tableName   表名
 * @param familyNames 列族
 *
 * @throws IOException
 */
public static void createTable(String tableName, String[] familyNames) throws IOException {

    try (Connection connection = ConnectionFactory.createConnection(configuration);
         Admin admin = connection.getAdmin()
    ) {
        TableName table = TableName.valueOf(tableName);
        if (admin.tableExists(table)) {
            log.info(tableName + " already exists");
        } else {
            HTableDescriptor hTableDescriptor = new HTableDescriptor(table);
            for (String family : familyNames) {
                hTableDescriptor.addFamily(new HColumnDescriptor(family));
            }
            admin.createTable(hTableDescriptor);
        }
    }
}

# 调用例
createTable("blog", new String[]{
  "author", "contents"});

3. describe

查询表结构

hbase> describe ‘t1’

4. disable

无效化指定表

hbase> disable ‘t1’

5. disable_all

无效化(正则)匹配的表

hbase> disable_all ‘t.*’

6. is_disabled

验证指定的表是否是无效的

hbase> is_disabled ‘t1’

7. drop

删除表。表必须是无效的。

Shell:

hbase> drop ‘t1’

Java实现:

/**
 * 删除表
 *
 * @param tableName 表名
 *
 * @throws IOException
 */
public static void dropTable(String tableName) throws IOException {

    try (Connection connection = ConnectionFactory.createConnection(configuration);
         Admin admin = connection.getAdmin()
    ) {

        TableName table = TableName.val
  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值