欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-04 12:54:04
今天看看怎样在 python 中使用 phoenixdb 来操作 hbase 安装 phoenixdb 库 pip install phoenixdb...

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

安装 phoenixdb 库

pip install phoenixdb

例子

首先启动 queryserver 服务

cd apache-phoenix-4.14.1-hbase-1.4-bin/bin
./queryserver.py

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

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']

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

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