HBase 查询示例:put()、get() 和 scan() 命令

向HBase表写入数据:Shell
这些示例假设 HBase 是 已安装并运行HBase shell 已打开,并且 名为 guru99 的表 柱家族的教育和项目已经存在。
put 命令用于将数据存储到表中。
Syntax: put <'tablename'>,<'rowname'>,<'columnvalue'>,<'value'>
此命令用于以下用途:
- 它会将单元格“值”放置在已定义或指定的表、行或列中。
- 它还可以选择性地添加时间戳。
例如,这里我们将值放入表“guru99”的第 r1 行和第 c1 列中:
hbase> put 'guru99', 'r1', 'c1', 'value', 10
我们在表“guru99”中放入了三个值,分别为 10、15 和 30,如下截图所示。
假设表“guru99”有一个表引用,例如 g。您也可以对表引用运行命令,如下所示:
hbase> g.put 'guru99', 'r1', 'c1', 'value', 10
将值放入“guru99”后,输出结果如上图所示。
从HBase表读取数据:Shell
在本节中,我们将检查以下内容: HBase 外壳:
- 插入到 HBase 表“guru99”中的值。
- HBase 表 guru99 中存在的列名及其值。
以下扫描输出列出了插入到“guru99”中的每个值,以及它们的行名和列名:
从上面的截图中,我们可以推断出以下信息:
- 如果在 HBase shell 中运行“scan”命令,它将显示“guru99”中插入的值,如下所示。
- 在 HBase shell 中,它将显示我们的代码插入的值,以及列名和行名。
- 这里我们可以看到插入的列名是“教育”和“项目”。
- 插入的值分别是“大数据”和“HBase教程”到上述列中。
您还可以使用 get 命令从表中读取数据。
Syntax: get <'tablename'>, <'rowname'>, {< Additional parameters>}
此处的附加参数包括 TIMERANGE、TIMESTAMP、VERSIONS 和 FILTERS。使用此命令,您可以获取表格中的某一行或某一单元格的内容。您可以添加其他参数,例如 TIMESTAMP、TIMERANGE、VERSIONS 或 FILTERS,以获取特定行或单元格的内容。下表总结了这些参数:
| 参数 | 目的 |
|---|---|
| 时间范围 | 返回时间戳在指定开始时间和结束时间范围内的单元格。 |
| TIMESTAMP | 仅返回在特定时间戳存储的单元格版本。 |
| 版本 | 设置要返回的单元格版本数量(默认值为 1)。 |
| 滤波器 | 应用筛选器来限制返回的行或列。 |
| COLUMN | 将结果限定为特定的列族或限定符。 |
以下是一些使用 get 命令的示例:
hbase> get 'guru99', 'r1', {COLUMN => 'c1'}
对于表“guru99”,使用此命令将显示第 r1 行和第 c1 列的值,如下面的屏幕截图所示。
hbase> get 'guru99', 'r1'
对于表“guru99”,使用此命令将显示第 r1 行的值。
hbase> get 'guru99', 'r1', {TIMERANGE => [ts1, ts2]}
对于表“guru99”,使用此命令将显示时间范围 ts1 到 ts2 内的行 r1 值。
hbase> get 'guru99', 'r1', {COLUMN => ['c1', 'c2', 'c3']}
对于表“guru99”,使用此命令将显示行 r1 和列族 c1、c2 和 c3 的值。
将数据写入 HBase 表:JAVA API
在此步骤中,我们将向 HBase 表“guru99”写入数据。
首先,我们需要编写代码,使用 HBaseLoading.java 程序从 HBase 插入和检索值。要在表的列级别创建和插入值,您可以编写如下所示的代码。
下面的截图显示了 Java 用于构建 HBase 配置并将值插入“guru99”的代码:
从上面的截图中可以看出:
- 创建 HBase 配置时,它会指向我们在 HBase 安装期间在 hbase-site.xml 和 hbase-default.xml 文件中设置的任何配置。
- 使用 HTable 方法创建表“guru99”。
- 将第 1 行添加到表“guru99”。
- 指定列名“education”和“projects”,并在相应的行中将值插入到列名中。此处插入的值为: BigData 以及“HBase教程”。
从 HBase 表读取数据: Java API
在上述章节中,我们将把 HBase 表中存储的值提取出来并显示出来。
以下控制台输出显示了从 HBase 表“guru99”中读取的数据:
检索存储在“guru99”中的结果:
- 在这里,我们将获取存储在列族中的值,即“教育”和“项目”。
- 我们将使用“get”命令来获取HBase表中存储的值。
- 使用“scan”命令扫描结果。存储在第一行中的值将显示在控制台上。
代码编写完成后,运行它。 Java 应用 喜欢这个:
右键单击 HBaseLoading.java -> 运行方式 -> Java 应用。
运行“HBaseLoading.java”后,值会被插入到HBase中“guru99”表的每一列中,并且同一个程序也可以检索这些值。
这是完整的代码:
import java.io.IOException; 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.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.util.Bytes; public class HBaseLoading { public static void main(String[] args) throws IOException { /* When you create a HBaseConfiguration, it reads in whatever you've set into your hbase-site.xml and in hbase-default.xml, as long as these can be found on the CLASSPATH*/ org.apache.hadoop.conf.Configuration config = HBaseConfiguration.create(); /*This instantiates an HTable object that connects you to the "test" table*/ HTable table = new HTable(config, "guru99"); /* To add to a row, use Put. A Put constructor takes the name of the row you want to insert into as a byte array.*/ Put p = new Put(Bytes.toBytes("row1")); /*To set the value you'd like to update in the row 'row1', specify the column family, column qualifier, and value of the table cell you'd like to update. The column family must already exist in your table schema. The qualifier can be anything.*/ p.add(Bytes.toBytes("education"), Bytes.toBytes("col1"),Bytes.toBytes("BigData")); p.add(Bytes.toBytes("projects"),Bytes.toBytes("col2"),Bytes.toBytes("HBaseTutorials")); // Once you've adorned your Put instance with all the updates you want to make, to commit it do the following table.put(p); // Now, to retrieve the data we just wrote. Get g = new Get(Bytes.toBytes("row1")); Result r = table.get(g); byte [] value = r.getValue(Bytes.toBytes("education"),Bytes.toBytes("col1")); byte [] value1 = r.getValue(Bytes.toBytes("projects"),Bytes.toBytes("col2")); String valueStr = Bytes.toString(value); String valueStr1 = Bytes.toString(value1); System.out.println("GET: " +"education: "+ valueStr+"projects: "+valueStr1); Scan s = new Scan(); s.addColumn(Bytes.toBytes("education"), Bytes.toBytes("col1")); s.addColumn(Bytes.toBytes("projects"), Bytes.toBytes("col2")); ResultScanner scanner = table.getScanner(s); try { for (Result rr = scanner.next(); rr != null; rr = scanner.next()) { System.out.println("Found row : " + rr); } } finally { // Make sure you close your scanners when you are done! scanner.close(); } } }
关于 API 版本的说明: 上面的示例使用了 HTable 类及其 add() 方法,这反映了旧版 HBase 客户端 API。自 HBase 1.0 起,HTable 已被弃用。现代代码通过 ConnectionFactory.createConnection(…).getTable(…) 获取 Table 对象,并使用 addColumn() 方法而非 add() 方法来构建 Put 操作。Put、Get 和 Scan 操作的逻辑保持不变。





