HBase創建表
可以使用命令創建一個表,在這里必須指定表名和列族名。在HBase shell中創建表的語法如下所示。
create ‘<table name>’,’<column family>’
示例
下面給出的是一個表名為emp的樣本模式。它有兩個列族:“personal data”和“professional data”。
Row key | personal data | professional data |
---|---|---|
在HBase shell創建該表如下所示。
hbase(main):002:0> create 'emp', 'personal data', ’professional data’
它會給下面的輸出。
0 row(s) in 1.1300 seconds => Hbase::Table - emp
驗證創建
可以驗證是否已經創建,使用 list 命令如下所示。在這里,可以看到創建的emp表。
hbase(main):002:0> list TABLE emp 2 row(s) in 0.0340 seconds
使用Java API創建一個表
可以使用HBaseAdmin類的createTable()方法創建表在HBase中。這個類屬於org.apache.hadoop.hbase.client 包。下面給出的步驟是來使用Java API創建表在HBase中。
第1步:實例化HBaseAdmin
這個類需要配置對象作為參數,因此初始實例配置類傳遞此實例給HBaseAdmin。
Configuration conf = HBaseConfiguration.create(); HBaseAdmin admin = new HBaseAdmin(conf);
第2步:創建TableDescriptor
HTableDescriptor類是屬於org.apache.hadoop.hbase。這個類就像表名和列族的容器一樣。
//creating table descriptor HTableDescriptor table = new HTableDescriptor(toBytes("Table name")); //creating column family descriptor HColumnDescriptor family = new HColumnDescriptor(toBytes("column family")); //adding coloumn family to HTable table.addFamily(family);
第3步:通過執行管理
使用HBaseAdmin類的createTable()方法,可以在管理模式執行創建的表。
admin.createTable(table);
下面給出的是完整的程序,通過管理員創建一個表。
import java.io.IOException; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.conf.Configuration; public class CreateTable { public static void main(String[] args) throws IOException { // Instantiating configuration class Configuration con = HBaseConfiguration.create(); // Instantiating HbaseAdmin class HBaseAdmin admin = new HBaseAdmin(con); // Instantiating table descriptor class HTableDescriptor tableDescriptor = new TableDescriptor(TableName.valueOf("emp")); // Adding column families to table descriptor tableDescriptor.addFamily(new HColumnDescriptor("personal")); tableDescriptor.addFamily(new HColumnDescriptor("professional")); // Execute the table through admin admin.createTable(tableDescriptor); System.out.println(" Table created "); } }
編譯和執行上述程序如下所示。
$javac CreateTable.java $java CreateTable
下面列出的是輸出:
Table created
HBase列出表
hbase(main):001:0 > list
當輸入這個命令,並在HBase提示符下執行,它會顯示HBase中的所有表的列表,如下圖所示。
hbase(main):001:0> list TABLE emp
在這里,可以看到一個名為表emp。
使用Java API列出表
按照下面給出的步驟來使用Java API從HBase獲得表的列表。
第1步
在類HBaseAdmin中有一個方法叫 listTables(),列出HBase中所有的表的列表。這個方法返回HTableDescriptor對象的數組。
//creating a configuration object Configuration conf = HBaseConfiguration.create(); //Creating HBaseAdmin object HBaseAdmin admin = new HBaseAdmin(conf); //Getting all the list of tables using HBaseAdmin object HTableDescriptor[] tableDescriptor =admin.listTables();
第1步
就可以得到使用HTableDescriptor類長度可變的HTableDescriptor[]數組的長度。從該對象使用getNameAsString()方法獲得表的名稱。運行'for'循環而獲得HBase表的列表。
下面給出的是使用Java API程序列出所有HBase中表的列表。
import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.MasterNotRunningException; import org.apache.hadoop.hbase.client.HBaseAdmin; public class ListTables { public static void main(String args[])throws MasterNotRunningException, IOException{ // Instantiating a configuration class Configuration conf = HBaseConfiguration.create(); // Instantiating HBaseAdmin class HBaseAdmin admin = new HBaseAdmin(conf); // Getting all the list of tables using HBaseAdmin object HTableDescriptor[] tableDescriptor =admin.listTables(); // printing all the table names. for (int i=0; i<tableDescriptor.length;i++ ){ System.out.println(tableDescriptor[i].getNameAsString()); } } }
編譯和執行上述程序如下所示。
$javac ListTables.java $java ListTables
下面列出的是輸出:
User emp
HBase禁用表
要刪除表或改變其設置,首先需要使用 disable 命令關閉表。使用 enable 命令,可以重新啟用它。
下面給出的語法是用來禁用一個表:
disable ‘emp’
下面給出的是一個例子,說明如何禁用表。
hbase(main):025:0> disable 'emp' 0 row(s) in 1.2760 seconds
驗證
禁用表之后,仍然可以通過 list 和exists命令查看到。無法掃描到它存在,它會給下面的錯誤。
hbase(main):028:0> scan 'emp' ROW COLUMN+CELL ERROR: emp is disabled.
is_disabled
這個命令是用來查看表是否被禁用。它的語法如下。
hbase> is_disabled 'table name'
下面的例子驗證表名為emp是否被禁用。如果禁用,它會返回true,如果沒有,它會返回false。
hbase(main):031:0> is_disabled 'emp' true 0 row(s) in 0.0440 seconds
disable_all
此命令用於禁用所有匹配給定正則表達式的表。disable_all命令的語法如下。
hbase> disable_all 'r.*'
假設有5個表在HBase,即raja, rajani, rajendra, rajesh 和 raju。下面的代碼將禁用所有以 raj 開始的表。
hbase(main):002:0> disable_all 'raj.*' raja rajani rajendra rajesh raju Disable the above 5 tables (y/n)? y 5 tables successfully disabled
禁用表使用Java API
要驗證一個表是否被禁用,使用isTableDisabled()方法和disableTable()方法禁用一個表。這些方法屬於HBaseAdmin類。按照下面給出禁用表中的步驟。
第1步
HBaseAdmin類的實例如下所示。
// Creating configuration object Configuration conf = HBaseConfiguration.create(); // Creating HBaseAdmin object HBaseAdmin admin = new HBaseAdmin(conf);
第2步
使用isTableDisabled()方法驗證表是否被禁用,如下圖所示。
Boolean b = admin.isTableDisabled("emp");
第3步
如果表未禁用,禁用它,如下圖所示。
if(!b){ admin.disableTable("emp"); System.out.println("Table disabled"); }
下面給出的是完整的程序,以驗證表是否被禁用;如果沒有,那么如何禁用它?
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 DisableTable{ public static void main(String args[]) throws MasterNotRunningException, IOException{ // Instantiating configuration class Configuration conf = HBaseConfiguration.create(); // Instantiating HBaseAdmin class HBaseAdmin admin = new HBaseAdmin(conf); // Verifying weather the table is disabled Boolean bool = admin.isTableDisabled("emp"); System.out.println(bool); // Disabling the table using HBaseAdmin object if(!bool){ admin.disableTable("emp"); System.out.println("Table disabled"); } } }
編譯和執行上述程序如下所示。
$javac DisableTable.java $java DsiableTable
下面列出的是輸出:
false Table disabled
HBase啟用表
啟用表的語法:
enable ‘emp’
給出下面是一個例子,使一個表啟用。
hbase(main):005:0> enable 'emp' 0 row(s) in 0.4580 seconds
驗證
啟用表之后,掃描。如果能看到的模式,那么證明表已成功啟用。
hbase(main):006:0> scan 'emp' ROW COLUMN+CELL 1 column=personal data:city, timestamp=1417516501, value=hyderabad 1 column=personal data:name, timestamp=1417525058, value=ramu 1 column=professional data:designation, timestamp=1417532601, value=manager 1 column=professional data:salary, timestamp=1417524244109, value=50000 2 column=personal data:city, timestamp=1417524574905, value=chennai 2 column=personal data:name, timestamp=1417524556125, value=ravi 2 column=professional data:designation, timestamp=14175292204, value=sr:engg 2 column=professional data:salary, timestamp=1417524604221, value=30000 3 column=personal data:city, timestamp=1417524681780, value=delhi 3 column=personal data:name, timestamp=1417524672067, value=rajesh 3 column=professional data:designation, timestamp=14175246987, value=jr:engg 3 column=professional data:salary, timestamp=1417524702514, value=25000 3 row(s) in 0.0400 seconds
is_enabled
此命令用於查找表是否被啟用。它的語法如下:
hbase> is_enabled 'table name'
下面的代碼驗證表emp是否啟用。如果啟用,它將返回true,如果沒有,它會返回false。
hbase(main):031:0> is_enabled 'emp' true 0 row(s) in 0.0440 seconds
使用Java API啟用表
要驗證一個表是否被啟用,使用isTableEnabled()方法;並且使用enableTable()方法使一個表啟用。這些方法屬於HBaseAdmin類。按照下面給出啟用表的步驟。
第1步
HBaseAdmin類的實例如下所示。
// Creating configuration object Configuration conf = HBaseConfiguration.create(); // Creating HBaseAdmin object HBaseAdmin admin = new HBaseAdmin(conf);
第2步
使用isTableEnabled()方法驗證表是否被啟用,如下所示。
Boolean bool=admin.isTableEnabled("emp");
第3步
如果表未禁用,那么禁用它,如下圖所示
if(!bool){ admin.enableTable("emp"); System.out.println("Table enabled"); }
下面給出的是完整的程序,以驗證表是否已啟用,如果它不是,那么啟用它。
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 EnableTable{ public static void main(String args[]) throws MasterNotRunningException, IOException{ // Instantiating configuration class Configuration conf = HBaseConfiguration.create(); // Instantiating HBaseAdmin class HBaseAdmin admin = new HBaseAdmin(conf); // Verifying weather the table is disabled Boolean bool = admin.isTableEnabled("emp"); System.out.println(bool); // Disabling the table using HBaseAdmin object if(!bool){ admin.enableTable("emp"); System.out.println("Table Enabled"); } } }
編譯和執行上述程序如下所示。
$javac EnableTable.java $java EnableTable
下面列出的是輸出:
false Table Enabled
HBase表描述和修改
描述
該命令返回表的說明。它的語法如下:
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',