版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/irving512/article/details/78119241
0. 资料
1. 安装
- 进入响应环境后,安装
happybase
与thrift
pip install happybase
pip install thrift
- 错误处理:
- 出现的错误:
thriftpy.parser.exc.ThriftParserError: ThriftPy does not support generating module with path in protocol 'd'
- Windows中才会出现此问题。
- 参考此文处理
- 解决方案:修改
Lib\site-packages\thriftpy\parser\parser.py
文件中的代码:
- 出现的错误:
# 修改前
url_scheme = urlparse(path).scheme
if url_scheme == '':
with open(path) as fh:
data = fh.read()
elif url_scheme in ('http', 'https'):
data = urlopen(path).read()
else:
raise ThriftParserError('ThriftPy does not support generating module '
'with path in protocol \'{}\''.format(
url_scheme))
# 修改后
url_scheme = urlparse(path).scheme
if url_scheme == '':
with open(path) as fh:
data = fh.read()
elif url_scheme in ('c', 'd','e','f'): # 代表c盘、d盘、e盘、f盘等
with open(path) as fh:
data = fh.read()
elif url_scheme in ('http', 'https'):
data = urlopen(path).read()
else:
raise ThriftParserError('ThriftPy does not support generating module '
'with path in protocol \'{}\''.format(
url_scheme))
2. 使用
2.1. 建立连接
import happybase
connection = happybase.Connection(HOST_IP)
2.2. 显示可用表
print(connection.tables())
2.3. 创建表
# DOCS: http://happybase.readthedocs.io/en/latest/api.html#happybase.Connection.create_table
# create_table(name, families)
# name (str) – The table name
# families (dict) – The name and options for each column family
families = {
'cf1': dict(max_versions=10),
'cf2': dict(max_versions=1, block_cache_enabled=False),
'cf3': dict(), # use defaults
}
connection.create_table('mytable', families)
2.4. 获取表、行对象
# 不需要进行编码
table = connection.table('table_name')
# 需要进行编码
row = table.row(b'row_key')
2.5. 基本操作
# 获取数据,需要编码
print(row[b'cf1:col1'])
# 存储数据,需要编码
# DOCS: http://happybase.readthedocs.io/en/latest/api.html#happybase.Table.put
table.put(b'row-key', {b'cf:col1': b'value1', b'cf:col2': b'value2'}, timestamp=123456789)
table.put(b'row-key', {b'cf:col1': b'value1'})
# 删除数据,需要编码
table.delete(b'row-key')
table.delete(b'row-key', columns=[b'cf1:col1', b'cf1:col2'])
2.6. 批量操作
# DOCS: http://happybase.readthedocs.io/en/latest/api.html#batch
b = table.batch()
b.put(b'row-key-1', {b'cf:col1': b'value1', b'cf:col2': b'value2'})
b.put(b'row-key-2', {b'cf:col2': b'value2', b'cf:col3': b'value3'})
b.put(b'row-key-3', {b'cf:col3': b'value3', b'cf:col4': b'value4'})
b.delete(b'row-key-4')
b.send()
2.7 连接池
# DOCS: http://happybase.readthedocs.io/en/latest/api.html#connection-pool
pool = happybase.ConnectionPool(size=3, host='...')
# 应尽快使用connection对象,不应在with中处理数据
# 在with中获取数据,在with外处理数据
with pool.connection() as connection:
table = connection.table('table-name')
row = table.row(b'row-key')
process_data(row)