脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python使用phoenixdb操作hbase的方法示例

python使用phoenixdb操作hbase的方法示例

2021-06-03 00:28kongxx Python

这篇文章主要介绍了python使用phoenixdb操作hbase的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

今天看看怎样在 python 中使用 phoenixdb 来操作 hbase

安装 phoenixdb 库

?
1
pip install phoenixdb

例子

首先启动 queryserver 服务

?
1
2
cd apache-phoenix-4.14.1-HBase-1.4-bin/bin
./queryserver.py

然后使用下面代码来建立连接、创建/删除并查询表。代码比较简单,和我们通常查询关系型数据库比较类似,这里就不多说了哈。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import phoenixdb
import phoenixdb.cursor
 
url = 'http://localhost:8765/'
conn = phoenixdb.connect(url, autocommit=True)
 
cursor = conn.cursor()
# cursor.execute("DROP TABLE users")
cursor.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, username VARCHAR, password VARCHAR)")
cursor.execute("UPSERT INTO users VALUES (?, ?, ?)", (1, 'admin', 'Letmein'))
cursor.execute("UPSERT INTO users VALUES (?, ?, ?)", (2, 'kongxx', 'Letmein'))
cursor.execute("SELECT * FROM users")
print cursor.fetchall()
 
cursor = conn.cursor(cursor_factory=phoenixdb.cursor.DictCursor)
cursor.execute("SELECT * FROM users WHERE id=1")
user = cursor.fetchone()
print user['USERNAME']
print user['PASSWORD']

最后运行这个程序看一下效果吧。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/kongxx/article/details/87996521

延伸 · 阅读

精彩推荐