首页 > 解决方案 > HBase 和集成测试

问题描述

我有一个使用 HBase 作为键/值存储的 Spark 项目。我们已经开始整体实施更好的 CI/CD 实践,我正在编写一个 python 客户端来针对自包含 AWS 环境运行集成测试。
虽然我能够轻松提交我们的 Spark 作业并将它们作为 EMR 步骤运行。我还没有找到从 python 与 HBase 交互的好方法。我的目标是能够针对示例 HDFS 数据运行我们的代码,然后在 HBase 中验证我得到了预期的结果。谁能提出一个好的方法来做到这一点?

此外,我的测试集非常小。如果我可以简单地将整个 HBase 表读入内存并以这种方式检查,我也会很高兴。非常感谢社区的意见。

标签: pythonhbaseintegration-testing

解决方案


这是使用 Happybase API 和 Thrift Server 从 Python 读取 HBase 数据的简单方法。

在 Hbase 服务器上启动 thrift 服务器:

/YOUR_HBASE_BIN_DIR/hbase-daemon.sh start thrift

然后从Python:

import happybase

HOST = 'Hbase server host name here'
TABLE_NAME = 'MyTable'
ROW_PREFIX = 'MyPrefix'
COL_TXT = 'CI:BO'.encode('utf-8') # column family CI, column name BO (Text)
COL_LONG = 'CI:BT'.encode('utf-8') # column family CI, column name C (Long)

conn = happybase.Connection(HOST) # uses default port 9095, but provide second arg if non-default port
myTable = conn.table(TABLE_NAME)

for rowID, row in myTable.scan(row_prefix=ROW_PREFIX.encode('utf-8')): # or leave empty if want full table scan
    colValTxt = row[COL_TXT].decode('utf-8')
    colValLong = int.from_bytes(row[COL_LONG], byteorder='big')
    print('Row ID: {}\tColumn Value: {}'.format(rowID, colValTxt))
print('All Done')

正如评论中所讨论的,如果您尝试将内容传递给 Spark 工作人员,这将不起作用,因为上述 HBase 连接不可序列化。所以你只能从主程序运行这种类型的代码。如果你想出办法——分享!


推荐阅读