使用python连接phoenix的几种方式
主要有以下几个python package可以用来连接phoenix,其余的待补充:
- jaydebeapi
- phoenixdb
phoenixdb
- 需要先启动queryserver(python queryserver.py start), 同时在hbase-site.xml中指定zookeeper的连接地址,否则默认连接是本地2181端口
使用pip install phoenixdb,具体示例如下:
import phoenixdb
import phoenixdb.cursor
database_url = 'http://localhost:8765/'
conn = phoenixdb.connect(database_url, autocommit=True)
cursor = conn.cursor()
cursor.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, username VARCHAR)")
cursor.execute("UPSERT INTO users VALUES (?, ?)", (1, 'admin'))
cursor.execute("SELECT * FROM users")
print cursor.fetchall()
cursor = conn.cursor(cursor_factory=phoenixdb.cursor.DictCursor)
cursor.execute("SELECT * FROM users WHERE id=1")
print cursor.fetchone()['USERNAME']
jaydebeapi
经热心网友指出,需要先开启JVM, 连接字符串也有细微调整
使用pip install JayDeBeApi,具体示例如下:
import jaydebeapi
conn = jaydebeapi.connect('org.apache.phoenix.jdbc.PhoenixDriver', \
'jdbc:phoenix:my_zk_server:2181',[ '', ''],
'/workspace/service/phoenix-client/phoenix-client.jar')
curs = conn.cursor()
curs.execute('select * from your_table limit 1')
curs.fetchall()