
- HBase 教程
- HBase - 首頁
- HBase - 概述
- HBase - 架構
- HBase - 安裝
- HBase - Shell
- HBase - 常用命令
- HBase - 管理員 API
- HBase - 建立表
- HBase - 列出表
- HBase - 停用表
- HBase - 啟用表
- HBase - 描述與修改
- HBase - Exists
- HBase - 刪除表
- HBase - 關閉
- HBase - 客戶端 API
- HBase - 建立資料
- HBase - 更新資料
- HBase - 讀取資料
- HBase - 刪除資料
- HBase - 掃描
- HBase - 計數與截斷
- HBase - 安全性
- HBase 資源
- HBase - 問題與解答
- HBase - 快速指南
- HBase - 有用資源
HBase - 描述與修改
describe
此命令返回表的描述。其語法如下:
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
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 的建構函式反過來需要要新增的列族名。這裡我們向現有的“employee”表中新增一個名為“contactDetails”的列族。
// 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