Hbase記錄-HBase增刪改查


HBase創建數據

本章將介紹如何在HBase表中創建的數據。要在HBase表中創建的數據,可以下面的命令和方法:

  • put 命令,
  • add() - Put類的方法
  • put() - HTable 類的方法.

作為一個例子,我們將在HBase中創建下表。

HBase Table

使用put命令,可以插入行到一個表。它的語法如下:

put ’<table name>’,’row1’,’<colfamily:colname>’,’<value>’

插入第一行

將第一行的值插入到emp表如下所示。

hbase(main):005:0> put 'emp','1','personal data:name','raju' 0 row(s) in 0.6600 seconds hbase(main):006:0> put 'emp','1','personal data:city','hyderabad' 0 row(s) in 0.0410 seconds hbase(main):007:0> put 'emp','1','professional data:designation','manager' 0 row(s) in 0.0240 seconds hbase(main):007:0> put 'emp','1','professional data:salary','50000' 0 row(s) in 0.0240 seconds 

以相同的方式使用put命令插入剩余的行。如果插入完成整個表格,會得到下面的輸出。

hbase(main):022:0> scan 'emp' ROW COLUMN+CELL 1 column=personal data:city, timestamp=1417524216501, value=hyderabad 1 column=personal data:name, timestamp=1417524185058, value=ramu 1 column=professional data:designation, timestamp=1417524232601, 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=1417524592204, 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=1417524693187, value=jr:engg 3 column=professional data:salary, timestamp=1417524702514, value=25000

使用Java API插入數據

可以使用Put 類的add()方法將數據插入到HBase。可以使用HTable類的put()方法保存數據。這些類屬於org.apache.hadoop.hbase.client包。下面給出的步驟是在一個HBase表創建數據。

第1步:實例化配置類

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"));

第5步:保存數據到表中

插入所需的行后,HTable類put實例的put()方法添加,如下所示保存更改。

hTable.put(p);

第6步:關閉HTable實例

創建在HBase的表數據之后,使用close()方法,如下所示關閉HTable實例。

hTable.close();

下面給出的是在HBase的表創建數據的完整程序。

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 InsertData{ 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")); // adding values using add() method // accepts column family name, qualifier/row name ,value p.add(Bytes.toBytes("personal"), Bytes.toBytes("name"),Bytes.toBytes("raju")); p.add(Bytes.toBytes("personal"), Bytes.toBytes("city"),Bytes.toBytes("hyderabad")); p.add(Bytes.toBytes("professional"),Bytes.toBytes("designation"), Bytes.toBytes("manager")); p.add(Bytes.toBytes("professional"),Bytes.toBytes("salary"), Bytes.toBytes("50000")); // Saving the put Instance to the HTable. hTable.put(p); System.out.println("data inserted"); // closing HTable hTable.close(); } }

編譯和執行上述程序如下所示。

$javac InsertData.java $java InsertData

下面列出的是輸出結果:

data inserted

HBase更新數據

可以使用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字節陣列,並要插入的值。將數據插入HBase表使用add()方法,如下圖所示。

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步:保存表數據

插入所需的行后,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

HBase讀取數據

get命令和HTable類的get()方法用於從HBase表中讀取數據。使用 get 命令,可以同時獲取一行數據。它的語法如下:

get ’<table name>’,’row1

下面的例子說明如何使用get命令。掃描emp表的第一行。

hbase(main):012:0> get 'emp', '1' COLUMN CELL personal : city timestamp=1417521848375, value=hyderabad personal : name timestamp=1417521785385, value=ramu professional: designation timestamp=1417521885277, value=manager professional: salary timestamp=1417521903862, value=50000 4 row(s) in 0.0270 seconds

讀取指定列

下面給出的是語法,使用get方法讀取指定列。

hbase>get 'table name', rowid’, {COLUMN => column family:column name ’}

下面給出的示例,是用於讀取HBase表中的特定列。

hbase(main):015:0> get 'emp', 'row1', {COLUMN=>'personal:name'} COLUMN CELL personal:name timestamp=1418035791555, value=raju 1 row(s) in 0.0080 seconds

使用Java API讀取數據

從一個HBase表中讀取數據,要使用HTable類的get()方法。這種方法需要Get類的一個實例。按照下面從HBase表中檢索數據給出的步驟。

第1步:實例化Configuration類

Configuration類增加了HBase的配置文件到它的對象。使用HbaseConfiguration類的create()方法,如下圖所示的配置對象。

Configuration conf = HbaseConfiguration.create();

第2步:實例化HTable類

有一類叫HTable,實現在HBase中的Table類。此類用於單個HBase的表進行通信。在這個類實例,它接受配置對象和表名作為參數。實例化HTable類,如下圖所示。

HTable hTable = new HTable(conf, tableName);

第3步:實例化獲得類

可以從HBase表使用HTable類的get()方法檢索數據。此方法提取從一個給定的行的單元格。它需要一個 Get 類對象作為參數。創建如下圖所示。

Get get = new Get(toBytes("row1"));

第4步:讀取數據

當檢索數據,可以通過ID得到一個單列,或得到一組行一組行ID,或者掃描整個表或行的子集。

可以使用Get類的add方法變種檢索HBase表中的數據。

從特定的列族獲取指定的列,使用下面的方法。

get.addFamily(personal)

要得到一個特定的列族的所有列,使用下面的方法。

get.addColumn(personal, name)

第5步:獲取結果

獲取結果通過Get類實例的HTable類的get方法。此方法返回Result類對象,其中保存所請求的結果。下面給出的是get()方法的使用。

Result result = table.get(g);

第6步:從Result實例讀值

Result 類提供getValue()方法從它的實例讀出值。如下圖所示,使用它從Result 實例讀出值。

byte [] value = result.getValue(Bytes.toBytes("personal"),Bytes.toBytes("name")); byte [] value1 = result.getValue(Bytes.toBytes("personal"),Bytes.toBytes("city"));

下面給出的是從一個HBase表中讀取值的完整程序

import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.util.Bytes; public class RetriveData{ public static void main(String[] args) throws IOException, Exception{ // Instantiating Configuration class Configuration config = HBaseConfiguration.create(); // Instantiating HTable class HTable table = new HTable(config, "emp"); // Instantiating Get class Get g = new Get(Bytes.toBytes("row1")); // Reading the data Result result = table.get(g); // Reading values from Result class object byte [] value = result.getValue(Bytes.toBytes("personal"),Bytes.toBytes("name")); byte [] value1 = result.getValue(Bytes.toBytes("personal"),Bytes.toBytes("city")); // Printing the values String name = Bytes.toString(value); String city = Bytes.toString(value1); System.out.println("name: " + name + " city: " + city); } }

編譯和執行上述程序如下所示。

$javac RetriveData.java $java RetriveData

下面列出的是輸出:

name: Raju city: Delhi

HBase刪除數據

從表刪除特定單元格

使用 delete 命令,可以在一個表中刪除特定單元格。 delete 命令的語法如下:

delete ‘<table name>’, ‘<row>’, ‘<column name >’, ‘<time stamp>’

下面是一個刪除特定單元格和例子。在這里,我們刪除salary

hbase(main):006:0> delete 'emp', '1', 'personal data:city', 1417521848375 0 row(s) in 0.0060 seconds

刪除表的所有單元格

使用“deleteall”命令,可以刪除一行中所有單元格。下面給出是 deleteall 命令的語法。

deleteall ‘<table name>’, ‘<row>’,

這里是使用“deleteall”命令刪去 emp 表 row1 的所有單元的一個例子。

hbase(main):007:0> deleteall 'emp','1' 0 row(s) in 0.0240 seconds

使用scan命令驗證表。表被刪除后的快照如下。

hbase(main):022:0> scan 'emp' ROW COLUMN+CELL 2 column=personal data:city, timestamp=1417524574905, value=chennai 2 column=personal data:name, timestamp=1417524556125, value=ravi 2 column=professional data:designation, timestamp=1417524204, 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=1417523187, value=jr:engg 3 column=professional data:salary, timestamp=1417524702514, value=25000

使用Java API刪除數據

可以從使用HTable類的delete()方法刪除HBase表數據。按照下面給出從表中刪除數據的步驟。

第1步:實例化Configuration類

Configuration類增加了HBase配置文件到它的對象。可以創建使用HbaseConfiguration類的create()方法,如下圖所示的Configuration 對象。

Configuration conf = HbaseConfiguration.create();

第2步:實例化HTable類

有一個類叫HTable,實現在HBase中的Table類。此類用於單個HBase的表進行通信。在這個類實例,它接受配置對象和表名作為參數。實例化HTable類,如下圖所示。

HTable hTable = new HTable(conf, tableName);

第3步:實例化Delete 類

通過傳遞將要刪除的行的行ID,在字節數組格式實例化Delete類。也可以通過構造時間戳和Rowlock。

Delete delete = new Delete(toBytes("row1"));

第4步:選擇刪除數據

可以使用Delete類的delete方法刪除數據。這個類有各種刪除方法。選擇使用這些方法來刪除列或列族。這里顯示Delete類方法的用法在下面的例子。

delete.deleteColumn(Bytes.toBytes("personal"), Bytes.toBytes("name")); delete.deleteFamily(Bytes.toBytes("professional"));

第5步:刪除數據

通過HTable類實例的delete()方法,如下所示刪除所選數據。

table.delete(delete);

第6步:關閉HTable實例

刪除數據后,關閉HTable實例。

table.close();

下面給出的是從HBase表中刪除的數據的完整程序。

import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.util.Bytes; public class DeleteData { public static void main(String[] args) throws IOException { // Instantiating Configuration class Configuration conf = HBaseConfiguration.create(); // Instantiating HTable class HTable table = new HTable(conf, "employee"); // Instantiating Delete class Delete delete = new Delete(Bytes.toBytes("row1")); delete.deleteColumn(Bytes.toBytes("personal"), Bytes.toBytes("name")); delete.deleteFamily(Bytes.toBytes("professional")); // deleting the data table.delete(delete); // closing the HTable object table.close(); System.out.println("data deleted....."); } }

編譯和執行上述程序如下所示。

$javac Deletedata.java $java DeleteData

下面列出的是輸出:

data deleted
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM