当前位置:   article > 正文

HBase数据访问的几种方式_hbase thriftserver

hbase thriftserver

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]

  1. import requests
  2. url = "http://localhost:8080/example/table_name/row_key"
  3. response = requests.get(url)
  4. if response.status_code == 200:
  5. data = response.json()
  6. print(data)
  7. else:
  8. print("Failed to retrieve data")

或者安装Python三方包: hbase-rest-py

pip install hbase-rest-py
  1. from hbase.rest_client import HBaseRESTClient
  2. from hbase.scan import Scan
  3. from hbase.scan_filter_helper import (
  4. build_base_scanner,
  5. build_prefix_filter,
  6. build_row_filter,
  7. build_value_filter,
  8. build_single_column_value_filter
  9. )
  10. client = HBaseRESTClient(['http://localhost:8080'])
  11. scanner_def = build_base_scanner(startRow="start", endRow="end", column=["cf:info"])
  12. flag, res = scan.scan(tbl_name="table_name", scanner_payload=scanner_def)

更多方法可查看:https://github.com/samirMoP/hbase-rest-py

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示例

  1. import happybase
  2. connection = happybase.Connection('localhost')
  3. connection.open()
  4. table = connection.table('table_name')
  5. scan_results = table.scan(row_start='start', row_stop='end')
  6. count = len([x for x in scan_results])
  7. connection.close()

更多方法可查看:https://github.com/python-happybase/happybase

HBase ThriftServer的优势

适合高性能和低延迟需求的场景,特别是需要处理大数据量和高并发查询的场景。

  • 延迟低,因为 Thrift 协议是二进制协议,序列化和反序列化效率高。
  • 吞吐量高,适合高并发和大数据量的查询。
  • 资源使用效率高,尤其是在需要处理大量数据时。

3 Java HBase-Client访问HBase

HBase-Client 配置

  1. <dependency>
  2. <groupId>org.apache.hbase</groupId>
  3. <artifactId>hbase-client</artifactId>
  4. <version>2.5.7</version>
  5. <exclusions>
  6. <exclusion>
  7. <groupId>log4j</groupId>
  8. <artifactId>log4j</artifactId>
  9. </exclusion>
  10. </exclusions>
  11. </dependency>

HBase-Client Java代码示例

  1. import org.apache.hadoop.hbase.HBaseConfiguration;
  2. import org.apache.hadoop.hbase.client.Connection;
  3. import org.apache.hadoop.hbase.client.ConnectionFactory;
  4. import org.apache.hadoop.hbase.client.Put;
  5. import org.apache.hadoop.hbase.client.Result;
  6. import org.apache.hadoop.hbase.client.ResultScanner;
  7. import org.apache.hadoop.hbase.client.Scan;
  8. import org.apache.hadoop.hbase.client.Table;
  9. import org.apache.hadoop.hbase.util.Bytes;
  10. public class HBaseClientExample {
  11. public static void main(String[] args) throws Exception {
  12. // 创建 HBase 配置
  13. org.apache.hadoop.conf.Configuration config = HBaseConfiguration.create();
  14. // 建立连接
  15. try (Connection connection = ConnectionFactory.createConnection(config)) {
  16. // 获取表
  17. Table table = connection.getTable(org.apache.hadoop.hbase.TableName.valueOf("my_table"));
  18. // 插入数据
  19. Put put = new Put(Bytes.toBytes("row1"));
  20. put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("qual1"), Bytes.toBytes("value1"));
  21. table.put(put);
  22. // 扫描表
  23. Scan scan = new Scan();
  24. try (ResultScanner scanner = table.getScanner(scan)) {
  25. for (Result result : scanner) {
  26. byte[] value = result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("qual1"));
  27. System.out.println("Found row: " + Bytes.toString(value));
  28. }
  29. }
  30. // 关闭表
  31. table.close();
  32. }
  33. }
  34. }

也可以使用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

5 参考资料

https://blog.51cto.com/u_16213427/11417321

https://blog.csdn.net/lIujunXHU/article/details/132765602

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/953085
推荐阅读
相关标签
  

闽ICP备14008679号