pyphoenix is a Python library for accessing the Phoenix SQL database using the remote query server introduced in Phoenix 4.4. The library implements the standard DB API 2.0 interface, which should be familiar to most Python programmers. It include sqlachemy plugins.
The easiest way to install the library is using pip:
pip install pyPhoenix
You can also download the source code and install it manually:
cd /path/to/pyphoenix/ python setup.py install
The library implements the standard DB API 2.0 interface, so it can be used the same way you would use any other SQL database from Python, for example:
import pyphoenix database_url = 'http://localhost:8765/' conn = pyphoenix.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()
Phoenix 4.7 uses a serialization based on Protocol Buffers (proto3) by default.
This library only supports Protocol serialization.
If you want to quickly try out the included examples, you can set up a local virtualenv with all the necessary requirements:
virtualenv e source e/bin/activate pip install -r requirements.txt python setup.py develop
If you need a Phoenix server for experimenting, you can get one running quickly using Vagrant:
You can connect to the virtual machine and work with the Phoenix shell from there:
vagrant ssh /opt/phoenix/bin/sqlline.py localhost
There is a Python-based interactive shell include in the examples folder, which can be used to connect to Phoenix and execute queries:
./examples/shell.py http://localhost:8765/ db=> CREATE TABLE test (id INTEGER PRIMARY KEY, name VARCHAR); no rows affected (1.363 seconds) db=> UPSERT INTO test (id, name) VALUES (1, 'Lukas'); 1 row affected (0.004 seconds) db=> SELECT * FROM test; +------+-------+ | ID | NAME | +======+=======+ | 1 | Lukas | +------+-------+ 1 row selected (0.019 seconds)
The library comes with a test suite for testing Python DB API 2.0 compliance and various Phoenix-specific features. In order to run the test suite, you need a working Phoenix database and set the pyphoenix_TEST_DB_URL environment variable:
export pyphoenix_TEST_DB_URL='http://localhost:8765/' nosetests
import sqlalchemy db = sqlalchemy.create_engine('phoenix://localhost:8765/') conn = db.connect()