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
广告
© . All rights reserved.