
- 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 讀取資料
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:例項化Get類
您可以使用HTable類的get()方法從HBase表檢索資料。此方法從給定行中提取單元格。它需要一個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
廣告