python使用phoenixdb操作hbase的方法示例
更新時間:2019年02月28日 11:55:24 作者:kongxx
這篇文章主要介紹了python使用phoenixdb操作hbase的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
今天看看怎樣在 python 中使用 phoenixdb 來操作 hbase
安裝 phoenixdb 庫
pip install phoenixdb
例子
首先啟動 queryserver 服務
cd apache-phoenix-4.14.1-HBase-1.4-bin/bin ./queryserver.py
然后使用下面代碼來建立連接、創(chuàng)建/刪除并查詢表。代碼比較簡單,和我們通常查詢關系型數據庫比較類似,這里就不多說了哈。
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']
最后運行這個程序看一下效果吧。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

