
- HBase 教程
- HBase - 首頁
- HBase - 概覽
- HBase - 架構
- HBase - 安裝
- HBase - Shell
- HBase - 常用命令
- HBase - 管理員API
- HBase - 建立表
- HBase - 列出表
- HBase - 停用表
- HBase - 啟用表
- HBase - 描述和修改
- HBase - 檢查是否存在
- HBase - 刪除表
- HBase - 關閉
- HBase - 客戶端API
- HBase - 建立資料
- HBase - 更新資料
- HBase - 讀取資料
- HBase - 刪除資料
- HBase - 掃描
- HBase - 計數和截斷
- HBase - 安全性
- HBase 資源
- HBase - 問答
- HBase - 快速指南
- HBase - 有用資源
HBase - 更新資料
使用 HBase Shell 更新資料
您可以使用 **put** 命令更新現有的單元格值。為此,只需遵循相同的語法並提及您的新值,如下所示。
put ‘table name’,’row ’,'Column family:column name',’new value’
新給定的值將替換現有值,從而更新行。
示例
假設在 HBase 中有一個名為 **emp** 的表,其中包含以下資料。
hbase(main):003:0> scan 'emp' ROW COLUMN + CELL row1 column = personal:name, timestamp = 1418051555, value = raju row1 column = personal:city, timestamp = 1418275907, value = Hyderabad row1 column = professional:designation, timestamp = 14180555,value = manager row1 column = professional:salary, timestamp = 1418035791555,value = 50000 1 row(s) in 0.0100 seconds
以下命令將把名為“Raju”的員工的城市值更新為“Delhi”。
hbase(main):002:0> put 'emp','row1','personal:city','Delhi' 0 row(s) in 0.0400 seconds
更新後的表如下所示,您可以在其中觀察到 Raju 的城市已更改為“Delhi”。
hbase(main):003:0> scan 'emp' ROW COLUMN + CELL row1 column = personal:name, timestamp = 1418035791555, value = raju row1 column = personal:city, timestamp = 1418274645907, value = Delhi row1 column = professional:designation, timestamp = 141857555,value = manager row1 column = professional:salary, timestamp = 1418039555, value = 50000 1 row(s) in 0.0100 seconds
使用 Java API 更新資料
您可以使用 **put()** 方法更新特定單元格中的資料。請按照以下步驟更新表的現有單元格值。
步驟 1:例項化 Configuration 類
**Configuration** 類將其物件新增到 HBase 配置檔案。您可以使用 **HbaseConfiguration** 類的 **create()** 方法建立配置物件,如下所示。
Configuration conf = HbaseConfiguration.create();
步驟 2:例項化 HTable 類
您有一個名為 **HTable** 的類,它是 HBase 中 Table 的實現。此類用於與單個 HBase 表通訊。在例項化此類時,它接受配置物件和表名稱作為引數。您可以如下所示例項化 HTable 類。
HTable hTable = new HTable(conf, tableName);
步驟 3:例項化 Put 類
要將資料插入 HBase 表,可以使用 **add()** 方法及其變體。此方法屬於 **Put**,因此例項化 **put** 類。此類需要您要插入資料的行名(字串格式)。您可以如下所示例項化 **Put** 類。
Put p = new Put(Bytes.toBytes("row1"));
步驟 4:更新現有單元格
**Put** 類的 **add()** 方法用於插入資料。它需要 3 個位元組陣列,分別表示列族、列限定符(列名)和要插入的值。使用 **add()** 方法將資料插入 HBase 表,如下所示。
p.add(Bytes.toBytes("coloumn family "), Bytes.toBytes("column name"),Bytes.toBytes("value")); p.add(Bytes.toBytes("personal"), Bytes.toBytes("city"),Bytes.toBytes("Delih"));
步驟 5:將資料儲存到表中
插入所需行後,透過將 put 例項新增到 HTable 類的 **put()** 方法中來儲存更改,如下所示。
hTable.put(p);
步驟 6:關閉 HTable 例項
在 HBase 表中建立資料後,使用 close() 方法關閉 **HTable** 例項,如下所示。
hTable.close();
下面是更新特定表中資料的完整程式。
import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.util.Bytes; public class UpdateData{ public static void main(String[] args) throws IOException { // Instantiating Configuration class Configuration config = HBaseConfiguration.create(); // Instantiating HTable class HTable hTable = new HTable(config, "emp"); // Instantiating Put class //accepts a row name Put p = new Put(Bytes.toBytes("row1")); // Updating a cell value p.add(Bytes.toBytes("personal"), Bytes.toBytes("city"),Bytes.toBytes("Delih")); // Saving the put Instance to the HTable. hTable.put(p); System.out.println("data Updated"); // closing HTable hTable.close(); } }
編譯並執行上述程式,如下所示。
$javac UpdateData.java $java UpdateData
輸出應如下所示
data Updated