当前位置: 代码网 > it编程>编程语言>Java > HBase数据访问的几种方式

HBase数据访问的几种方式

2024年08月01日 Java 我要评论
HBase RESTServer是Apache HBase提供的一个RESTful接口,用于通过HTTP协议与HBase进行交互。通过RESTServer,用户可以方便地通过发送HTTP请求来进行数据的读取、写入和查询操作,无需直接使用HBase的Java API。启动后,我们可以通过访问http://localhost:8080来访问RESTServer的Web UI,查看API文档和进行交互。易于使用:通过HTTP协议进行交互,无需了解复杂的Java API。

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中的数据:

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();
        }
    }
}

java hbase 客户端查询优势

  • 直接通信:java 客户端直接与 hbase 的 regionservers 和 zookeeper 进行通信,没有中间层,减少了延迟。
  • 高效协议:使用 hbase 的原生 rpc 协议(基于 protobuf),具有高效的二进制序列化和反序列化。
  • 本地优化:java 客户端是 hbase 的原生客户端,经过高度优化,能够充分利用 hbase 提供的所有特性和功能。
  • 缓存机制:客户端缓存元数据(如 region 位置信息),减少了与 zookeeper 和regionservers 的通信频率,提高了性能。

4 验证版本

5 参考资料

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

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

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com