python 调用 HBase
时间: 2024-09-30 16:13:21 浏览: 50
在Python中,可以使用`hbase`库或者`httplib2`配合`gdata`库来与HBase交互。以下是基本步骤:
**使用`hbase`库**(推荐):
首先需要安装`hbase`库,可以通过pip进行安装:
```bash
pip install hbase
```
然后你可以创建一个连接,并执行基本操作,例如插入、读取、删除数据:
```python
from hbase import HBase
# 创建HBase连接
hbase = HBase('localhost', 'port', 'your_username', 'your_password')
# 插入数据
row_key = 'example'
column_family = 'cf'
qualifier = 'cq'
value = 'example_value'
hbase.put(row_key, {column_family: {qualifier: value}})
# 读取数据
result = hbase.get(row_key, column_family=column_family)
print(result[column_family][qualifier])
# 删除数据
hbase.delete(row_key, column_family, qualifier)
```
**使用`httplib2` + `gdata`库**(旧版本):
这种方法已经不再推荐,因为Google Data APIs已停止维护。不过如果你需要兼容旧项目,可以这么操作:
```python
import httplib2
from gdata.spreadsheet.service import SpreadsheetsService
# 创建服务实例
service = SpreadsheetsService()
service.email = 'your_email'
service.password = 'your_password'
# 设置HBase URL
base_url = "http://host:port/hbase"
# 使用REST API执行操作
# 示例:获取数据
response = service.request(base_url + '/get?table=your_table&rowkey=rowkey')
print(response.content)
```
阅读全文
相关推荐

















