Python database adapter library for Apache Phoenix databases implementing DB API 2.0 and partial SQLAlchemy support
Database connection establishment, configuration, and lifecycle management with support for various authentication mechanisms and connection parameters.
Establishes connections to Phoenix query server with comprehensive authentication and configuration support.
def connect(url, max_retries=None, auth=None, authentication=None,
avatica_user=None, avatica_password=None, truststore=None,
verify=None, do_as=None, user=None, password=None,
extra_headers=None, **kwargs):
"""
Connects to a Phoenix query server.
Parameters:
- url (str): URL to Phoenix query server (e.g., 'http://localhost:8765/')
- autocommit (bool): Switch connection to autocommit mode (default: False)
- readonly (bool): Switch connection to readonly mode (default: False)
- max_retries (int): Maximum number of retries for connection errors
- cursor_factory: Default cursor factory class (default: Cursor)
- auth: Authentication configuration object (requests.auth compatible)
- authentication (str): Authentication mechanism ('BASIC', 'DIGEST', 'SPNEGO', 'NONE')
- avatica_user (str): Username for BASIC/DIGEST authentication
- avatica_password (str): Password for BASIC/DIGEST authentication
- truststore (str): Path to PEM file for server certificate verification
- verify: Certificate verification configuration (passed to requests)
- do_as (str): Username to impersonate (sets Hadoop doAs URL parameter)
- user (str): Alias for avatica_user (BASIC/DIGEST) or do_as (SPNEGO/NONE)
- password (str): Alias for avatica_password
- extra_headers (dict): Additional HTTP headers as dictionary
Returns:
Connection: Database connection object
Raises:
InterfaceError: Connection interface problems
OperationalError: Connection establishment failures
"""Usage examples:
# Basic connection
conn = phoenixdb.connect('http://localhost:8765/')
# With autocommit
conn = phoenixdb.connect('http://localhost:8765/', autocommit=True)
# With authentication
conn = phoenixdb.connect('http://localhost:8765/',
authentication='BASIC',
avatica_user='username',
avatica_password='password')
# With SPNEGO/Kerberos
conn = phoenixdb.connect('https://localhost:8765/',
authentication='SPNEGO',
truststore='/path/to/truststore.pem')
# With custom headers and retry configuration
conn = phoenixdb.connect('http://localhost:8765/',
max_retries=3,
extra_headers={'User-Agent': 'MyApp/1.0'})Manages database connection state and provides access to cursors and metadata.
class Connection:
"""Database connection object."""
def __init__(self, client, cursor_factory=None, **kwargs): ...
def open(self):
"""Opens the connection."""
def close(self):
"""
Closes the connection and all associated cursors.
No further operations allowed after closing.
"""
def commit(self):
"""Commits the current transaction."""
def rollback(self):
"""Rolls back the current transaction."""
def cursor(self, cursor_factory=None):
"""
Creates a new cursor.
Parameters:
- cursor_factory: Cursor class to use (default: self.cursor_factory)
Returns:
Cursor: New cursor object
"""
def set_session(self, **props):
"""
Sets connection properties.
Parameters:
- autocommit (bool): Switch to autocommit mode
- readonly (bool): Switch to readonly mode
"""
def meta(self):
"""
Creates a metadata interface.
Returns:
Meta: Database metadata object
"""class Connection:
@property
def closed(self):
"""Read-only boolean indicating if connection is closed."""
@property
def cursor_factory(self):
"""Default cursor factory used by cursor() method."""
@cursor_factory.setter
def cursor_factory(self, value): ...
@property
def autocommit(self):
"""Read/write boolean for autocommit mode."""
@autocommit.setter
def autocommit(self, value): ...
@property
def readonly(self):
"""Read/write boolean for readonly mode."""
@readonly.setter
def readonly(self, value): ...
@property
def transactionisolation(self):
"""Read/write transaction isolation level."""
@transactionisolation.setter
def transactionisolation(self, value): ...Connections support Python context manager protocol for automatic resource cleanup.
class Connection:
def __enter__(self):
"""Context manager entry."""
return self
def __exit__(self, exc_type, exc_value, traceback):
"""Context manager exit with automatic connection closing."""Usage:
with phoenixdb.connect('http://localhost:8765/') as conn:
# Connection automatically closed when exiting block
cursor = conn.cursor()
cursor.execute("SELECT * FROM table")
results = cursor.fetchall()conn = phoenixdb.connect('http://localhost:8765/',
authentication='BASIC',
avatica_user='username',
avatica_password='password')conn = phoenixdb.connect('http://localhost:8765/',
authentication='DIGEST',
avatica_user='username',
avatica_password='password')# Requires prior kinit or system Kerberos configuration
conn = phoenixdb.connect('https://localhost:8765/',
authentication='SPNEGO')from requests.auth import HTTPBasicAuth
# Using requests.auth objects directly
auth = HTTPBasicAuth('username', 'password')
conn = phoenixdb.connect('http://localhost:8765/', auth=auth)# Disable certificate verification
conn = phoenixdb.connect('https://localhost:8765/', verify=False)
# Use custom truststore
conn = phoenixdb.connect('https://localhost:8765/',
truststore='/path/to/truststore.pem')Connection parameters can be embedded in the URL:
url = 'http://localhost:8765/?authentication=BASIC&avatica_user=user&truststore=/path/to/cert.pem'
conn = phoenixdb.connect(url)# Impersonate another user (Hadoop doAs)
conn = phoenixdb.connect('http://localhost:8765/', do_as='other_user')
# Using user parameter (maps to do_as for SPNEGO/NONE auth)
conn = phoenixdb.connect('http://localhost:8765/',
authentication='SPNEGO',
user='other_user')Install with Tessl CLI
npx tessl i tessl/pypi-phoenixdb