HBase 描述和更改
描述
此命令返回表的描述。其语法如下:
hbase> describe 'table name'
下面给出的是描述命令在
emp 表上的输出。
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() 方法需要一个表名和一个
HColumnDescriptor 类的对象。因此实例化
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
如果一切顺利,它将产生以下输出:
使用 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
以下应该是输出: