返回介绍

HBase 读取数据 - HBase 教程

发布于 2025-02-22 13:46:38 字数 3759 浏览 0 评论 0 收藏 0

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文