该命令返回表的说明。它的语法如下:
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 |
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) |
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 |
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 |
可以使用HBAseAdmin类的addColumn方法添加一列家族的表。按照下面给出的步骤将一个列族添加到表中。
实例化HBaseAdmin类。
// Instantiating configuration object |
Configuration conf = HBaseConfiguration.create(); |
// Instantiating HBaseAdmin class |
HBaseAdmin admin = new HBaseAdmin(conf); |
addColumn()方法需要一个表名和一个HColumnDescriptorclass对象。因此需要实例化HColumnDescriptor类。 HColumnDescriptor依次构造函数需要一个列族名称用于添加。在这里加入了一个名为“contactDetails”到“employee”表的列族。
// Instantiating columnDescriptor object |
HColumnDescriptor columnDescriptor = new HColumnDescriptor("contactDetails"); |
使用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 |
可以使用HBAseAdmin类的deleteColumn()方法删除列族。按照下面给出的步骤添加一个列族到表中。
实例化HBaseAdmin类。
// Instantiating configuration object |
Configuration conf = HBaseConfiguration.create(); |
// Instantiating HBaseAdmin class |
HBaseAdmin admin = new HBaseAdmin(conf); |
使用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 |