python - HBase 和集成测试
问题描述
我有一个使用 HBase 作为键/值存储的 Spark 项目。我们已经开始整体实施更好的 CI/CD 实践,我正在编写一个 python 客户端来针对自包含 AWS 环境运行集成测试。
虽然我能够轻松提交我们的 Spark 作业并将它们作为 EMR 步骤运行。我还没有找到从 python 与 HBase 交互的好方法。我的目标是能够针对示例 HDFS 数据运行我们的代码,然后在 HBase 中验证我得到了预期的结果。谁能提出一个好的方法来做到这一点?
此外,我的测试集非常小。如果我可以简单地将整个 HBase 表读入内存并以这种方式检查,我也会很高兴。非常感谢社区的意见。
解决方案
这是使用 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 连接不可序列化。所以你只能从主程序运行这种类型的代码。如果你想出办法——分享!
推荐阅读
- c++ - 将列表输入到类中
- angular - Angular 在 RTL 模式下的 UI-Grid,在水平滚动时显示缺失的列,并且列与相应单元格的水平对齐不正确
- ruby-on-rails - 为什么 I18n.locale 和 redirect_back 不起作用?
- javascript - 如何仅使用具有数字值的键返回对象?
- c++ - 是否可以替换 / 或 . 在宏中带有 __ 的字符串中?
- oracle-apex - apex_data_parser 文件 > 50MB
- python - 如何处理numpy中的nan值
- spring-cloud - 如何通过 Spring Cloud 数据流中的处理器应用程序触发组合任务运行程序?
- tabulator - 更新bottomCalc-value,基于选择(如果有的话)
- sql - INTERSECT ALL 不适用于 PostgreSQL 11