赞
踩
HBase RESTServer是Apache HBase提供的一个RESTful接口,用于通过HTTP协议与HBase进行交互。通过RESTServer,用户可以方便地通过发送HTTP请求来进行数据的读取、写入和查询操作,无需直接使用HBase的Java API。
首先,我们需要启动HBase RESTServer。在HBase的安装目录下,执行以下命令来启动RESTServer:
./bin/hbase-daemon.sh start rest
启动后,我们可以通过访问http://localhost:8080来访问RESTServer的Web UI,查看API文档和进行交互。
下面是一个简单的Python代码示例,演示如何通过HBase RESTServer来读取HBase中的数据:
http://localhost:8080/table_name/row_key [GET]
- import requests
-
- url = "http://localhost:8080/example/table_name/row_key"
-
- response = requests.get(url)
-
- if response.status_code == 200:
- data = response.json()
- print(data)
- else:
- print("Failed to retrieve data")
或者安装Python三方包: hbase-rest-py
pip install hbase-rest-py
- from hbase.rest_client import HBaseRESTClient
- from hbase.scan import Scan
- from hbase.scan_filter_helper import (
- build_base_scanner,
- build_prefix_filter,
- build_row_filter,
- build_value_filter,
- build_single_column_value_filter
- )
-
- client = HBaseRESTClient(['http://localhost:8080'])
- scanner_def = build_base_scanner(startRow="start", endRow="end", column=["cf:info"])
- flag, res = scan.scan(tbl_name="table_name", scanner_payload=scanner_def)
易于使用:通过HTTP协议进行交互,无需了解复杂的Java API。
跨平台支持:RESTful接口可以被任何支持HTTP协议的平台和语言所访问。
灵活性:可以方便地与其他系统集成,实现数据的共享和交换。
首先,我们需要启动HBase ThriftServer。在HBase的安装目录下,执行以下命令来启动ThriftServer:
./bin/hbase-daemon.sh start thrift
安装Python三方包: happybase
pip install happybase
scan示例
- import happybase
-
- connection = happybase.Connection('localhost')
- connection.open()
-
- table = connection.table('table_name')
- scan_results = table.scan(row_start='start', row_stop='end')
- count = len([x for x in scan_results])
-
- connection.close()
适合高性能和低延迟需求的场景,特别是需要处理大数据量和高并发查询的场景。
- <dependency>
- <groupId>org.apache.hbase</groupId>
- <artifactId>hbase-client</artifactId>
- <version>2.5.7</version>
- <exclusions>
- <exclusion>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- import org.apache.hadoop.hbase.HBaseConfiguration;
- import org.apache.hadoop.hbase.client.Connection;
- import org.apache.hadoop.hbase.client.ConnectionFactory;
- 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.client.Table;
- import org.apache.hadoop.hbase.util.Bytes;
-
- public class HBaseClientExample {
- public static void main(String[] args) throws Exception {
- // 创建 HBase 配置
- org.apache.hadoop.conf.Configuration config = HBaseConfiguration.create();
-
- // 建立连接
- try (Connection connection = ConnectionFactory.createConnection(config)) {
- // 获取表
- Table table = connection.getTable(org.apache.hadoop.hbase.TableName.valueOf("my_table"));
-
- // 插入数据
- Put put = new Put(Bytes.toBytes("row1"));
- put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("qual1"), Bytes.toBytes("value1"));
- table.put(put);
-
- // 扫描表
- Scan scan = new Scan();
- try (ResultScanner scanner = table.getScanner(scan)) {
- for (Result result : scanner) {
- byte[] value = result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("qual1"));
- System.out.println("Found row: " + Bytes.toString(value));
- }
- }
-
- // 关闭表
- table.close();
- }
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
也可以使用spring-boot-starter-hbase进行查询,简化了集成与配置管理,由于增加了一层抽象,性能可能略低于直接使用
hbase-client
。
HBase 2.3.7
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。