HBase数据访问的几种方式
1 Python访问HBase RestServer
HBase RestServer
HBase RESTServer是Apache HBase提供的一个RESTful接口,用于通过HTTP协议与HBase进行交互。通过RESTServer,用户可以方便地通过发送HTTP请求来进行数据的读取、写入和查询操作,无需直接使用HBase的Java API。
首先,我们需要启动HBase RESTServer。在HBase的安装目录下,执行以下命令来启动RESTServer:
./bin/hbase-daemon.sh start rest
HBase RestServer的示例
启动后,我们可以通过访问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)
HBase RestServer的优势
易于使用:通过HTTP协议进行交互,无需了解复杂的Java API。
跨平台支持:RESTful接口可以被任何支持HTTP协议的平台和语言所访问。
灵活性:可以方便地与其他系统集成,实现数据的共享和交换。
2 Python访问HBase ThriftServer
HBase ThriftServer
首先,我们需要启动HBase ThriftServer。在HBase的安装目录下,执行以下命令来启动ThriftServer:
./bin/hbase-daemon.sh start thrift
HBase ThriftServer的示例
安装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()
HBase ThriftServer的优势
适合高性能和低延迟需求的场景,特别是需要处理大数据量和高并发查询的场景。
- 延迟低,因为 Thrift 协议是二进制协议,序列化和反序列化效率高。
- 吞吐量高,适合高并发和大数据量的查询。
- 资源使用效率高,尤其是在需要处理大量数据时。
3 Java HBase-Client访问HBase
HBase-Client 配置
<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>
HBase-Client Java代码示例
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();
}
}
}
也可以使用spring-boot-starter-hbase进行查询,简化了集成与配置管理,由于增加了一层抽象,性能可能略低于直接使用
hbase-client
。
Java HBase 客户端查询优势
- 直接通信:Java 客户端直接与 HBase 的 RegionServers 和 ZooKeeper 进行通信,没有中间层,减少了延迟。
- 高效协议:使用 HBase 的原生 RPC 协议(基于 Protobuf),具有高效的二进制序列化和反序列化。
- 本地优化:Java 客户端是 HBase 的原生客户端,经过高度优化,能够充分利用 HBase 提供的所有特性和功能。
- 缓存机制:客户端缓存元数据(如 Region 位置信息),减少了与 ZooKeeper 和RegionServers 的通信频率,提高了性能。
4 验证版本
HBase 2.3.7