返回介绍

HBase 表描述和修改 - HBase 教程

发布于 2025-02-22 13:46:37 字数 7352 浏览 0 评论 0 收藏 0

描述

该命令返回表的说明。它的语法如下:

hbase> describe 'table name'

下面给出的是对 emp 表的 describe 命令的输出。

hbase(main):006:0> describe 'emp'
   DESCRIPTION
      ENABLED

'emp', {NAME => 'READONLY', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER
=> 'ROW', REPLICATION_SCOPE => '0', COMPRESSION => 'NONE', VERSIONS =>
'1', TTL true

=> 'FOREVER', MIN_VERSIONS => '0', KEEP_DELETED_CELLS => 'false',
BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}, {NAME
=> 'personal

data', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW',
REPLICATION_SCOPE => '0', VERSIONS => '5', COMPRESSION => 'NONE',
MIN_VERSIONS => '0', TTL

=> 'FOREVER', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536',
IN_MEMORY => 'false', BLOCKCACHE => 'true'}, {NAME => 'professional
data', DATA_BLO

CK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0',
VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL =>
'FOREVER', K

EEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536', IN_MEMORY =>
'false', BLOCKCACHE => 'true'}, {NAME => 'table_att_unset',
DATA_BLOCK_ENCODING => 'NO 

NE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', COMPRESSION =>
'NONE', VERSIONS => '1', TTL => 'FOREVER', MIN_VERSIONS => '0',
KEEP_DELETED_CELLS

=> 'false', BLOCKSIZE => '6

修改

alter 用于更改现有表的命令。使用此命令可以更改列族的单元,设定最大数量和删除表范围运算符,并从表中删除列家族。

更改列族单元格的最大数目

下面给出的语法来改变列家族单元的最大数目。

hbase> alter 't1', NAME => 'f1', VERSIONS => 5

在下面的例子中,单元的最大数目设置为 5。

hbase(main):003:0> alter 'emp', NAME => 'personal data', VERSIONS => 5
Updating all regions with the new schema...
0/1 regions updated.
1/1 regions updated.
Done.
0 row(s) in 2.3050 seconds

表范围运算符

使用 alter,可以设置和删除表范围,运算符,如 MAX_FILESIZE,READONLY,MEMSTORE_FLUSHSIZE,DEFERRED_LOG_FLUSH 等。

设置只读

下面给出的是语法,是用以设置表为只读。

hbase>alter 't1', READONLY(option)

在下面的例子中,我们已经设置表 emp 为只读。

hbase(main):006:0> alter 'emp', READONLY
Updating all regions with the new schema...
0/1 regions updated.
1/1 regions updated.
Done.
0 row(s) in 2.2140 seconds

删除表范围运算符

也可以删除表范围运算。下面给出的是语法,从 emp 表中删除“MAX_FILESIZE”。

hbase> alter 't1', METHOD => 'table_att_unset', NAME => 'MAX_FILESIZE'

删除列族

使用 alter,也可以删除列族。下面给出的是使用 alter 删除列族的语法。

hbase> alter ‘ table name ’, ‘delete’ => ‘ column family ’

下面给出的是一个例子,从“emp”表中删除列族。

假设在 HBase 中有一个 employee 表。它包含以下数据:

hbase(main):006:0> scan 'employee'

         ROW                   COLUMN+CELL

row1 column=personal:city, timestamp=1418193767, value=hyderabad

row1 column=personal:name, timestamp=1418193806767, value=raju

row1 column=professional:designation, timestamp=1418193767, value=manager

row1 column=professional:salary, timestamp=1418193806767, value=50000

1 row(s) in 0.0160 seconds

现在使用 alter 命令删除指定的 professional 列族。

hbase(main):007:0> alter 'employee','delete'=>'professional'
Updating all regions with the new schema...
0/1 regions updated.
1/1 regions updated.
Done.
0 row(s) in 2.2380 seconds

现在验证该表中变更后的数据。观察列族“professional”也没有了,因为前面已经被删除了。

hbase(main):003:0> scan 'employee'
 ROW             COLUMN+CELL
row1 column=personal:city, timestamp=14181936767, value=hyderabad

row1 column=personal:name, timestamp=1418193806767, value=raju

1 row(s) in 0.0830 seconds

使用 Java API 添加一列族

可以使用 HBAseAdmin 类的 addColumn 方法添加一列家族的表。按照下面给出的步骤将一个列族添加到表中。

第 1 步

实例化 HBaseAdmin 类。

// Instantiating configuration object
Configuration conf = HBaseConfiguration.create();

// Instantiating HBaseAdmin class
HBaseAdmin admin = new HBaseAdmin(conf);

第 2 步

addColumn() 方法需要一个表名和一个 HColumnDescriptorclass 对象。因此需要实例化 HColumnDescriptor 类。 HColumnDescriptor 依次构造函数需要一个列族名称用于添加。在这里加入了一个名为“contactDetails”到“employee”表的列族。

// Instantiating columnDescriptor object

HColumnDescriptor columnDescriptor = new
HColumnDescriptor("contactDetails");

第 3 步

使用 addColumn 方法添加列族。通过表名和 HColumnDescriptor 类对象作为这个方法的参数。

// Adding column family
admin.addColumn("employee", new HColumnDescriptor("columnDescriptor"));

下面给出的是一个完整的程序,用于添加一列族到现有的表。

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class AddColoumn{

   public static void main(String args[]) throws MasterNotRunningException, IOException{

      // Instantiating configuration class.
      Configuration conf = HBaseConfiguration.create();

      // Instantiating HBaseAdmin class.
      HBaseAdmin admin = new HBaseAdmin(conf);

      // Instantiating columnDescriptor class
      HColumnDescriptor columnDescriptor = new HColumnDescriptor("contactDetails");

      // Adding column family
      admin.addColumn("employee", columnDescriptor);
      System.out.println("coloumn added");
   }
}

编译和执行上述程序,如下所示

$javac AddColumn.java
$java AddColumn

上述编译只有已经设置“.bashrc”中的类路径。如果还没有,请按照下面编译给出.java 文件的程序。

//if "/home/home/hadoop/hbase " is your Hbase home folder then.

$javac -cp /home/hadoop/hbase/lib/*: Demo.java

如果一切顺利,它会生成以下的输出:

 column added

使用 Java API 删除列族

可以使用 HBAseAdmin 类的 deleteColumn() 方法删除列族。按照下面给出的步骤添加一个列族到表中。

第 1 步

实例化 HBaseAdmin 类。

// Instantiating configuration object
Configuration conf = HBaseConfiguration.create();

// Instantiating HBaseAdmin class
HBaseAdmin admin = new HBaseAdmin(conf);

第 2 步

使用 deleteColumn() 方法添加列族。传递表名和列族名作为这个方法的参数。

// Deleting column family
admin.deleteColumn("employee", "contactDetails");

下面给出的是从现有表中删除列族的完整的程序。

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class DeleteColoumn{

   public static void main(String args[]) throws MasterNotRunningException, IOException{

      // Instantiating configuration class.
      Configuration conf = HBaseConfiguration.create();

      // Instantiating HBaseAdmin class.
      HBaseAdmin admin = new HBaseAdmin(conf);

      // Deleting a column family
      admin.deleteColumn("employee","contactDetails");
      System.out.println("coloumn deleted"); 
   }
}

编译和执行上述程序如下所示。

$javac DeleteColumn.java
$java DeleteColumn

下面列出的是输出:

column deleted

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文