Kinetica provides a SQL interface through its SQLAlchemy dialect, which makes use of the DBAPI interface of Kinetica's Python API.
Installation
The Kinetica Dialect for SQLAlchemy can be installed from PyPI using pip:
pip3 install sqlalchemy_kinetica
This will also install the supporting version of the Kinetica Python API.
The package is also available in GitHub:
https://github.com/kineticadb/sqlalchemy-kinetica
Connecting
The standard SQLAlchemy create_engine call can be used to connect to a Kinetica database instance.
For HTTPS-based installations, the bypass_ssl_cert_check option can be used to bypass any server certificate validation.
|
|
|
|
Managing Connections
SQLAlchemy connections can either be automatically or manually managed. When manually managed, connections must be explicitly closed after use.
|
|
|
|
Data Types
All Kinetica data types are available to be used in SQLAlchemy through either native SQLAlchemy types or Kinetica custom dialect types.
Category | Kinetica Data Type | SQLAlchemy Type Class |
---|---|---|
Number | BOOLEAN | sqlalchemy.Boolean |
TINYINT | sqlalchemy_kinetica.kinetica_types.TINYINT | |
SMALLINT | sqlalchemy_kinetica.kinetica_types.SMALLINT | |
INTEGER | sqlalchemy.Integer | |
BIGINT | sqlalchemy.BIGINT | |
UNSIGNED BIGINT | sqlalchemy_kinetica.kinetica_types.UnsignedBigInteger | |
REAL | sqlalchemy.REAL | |
DOUBLE | sqlalchemy.DOUBLE | |
DECIMAL(P,S) | sqlalchemy_kinetica.kinetica_types.DECIMAL | |
String | VARCHAR | sqlalchemy.VARCHAR |
IPV4 | sqlalchemy_kinetica.kinetica_types.IPV4 | |
UUID | sqlalchemy.UUID | |
JSON | sqlalchemy_kinetica.kinetica_types.JSON | |
Date/Time | DATE | sqlalchemy.DATE |
DATETIME | sqlalchemy.DATETIME | |
TIME | sqlalchemy.TIME | |
TIMESTAMP | sqlalchemy.TIMESTAMP | |
Binary | BLOB | sqlalchemy_kinetica.kinetica_types.BLOB |
Geospatial | GEOMETRY | sqlalchemy_kinetica.kinetica_types.GEOMETRY |
BLOB(WKT) | sqlalchemy_kinetica.kinetica_types.BlobWKT | |
Composite | BOOLEAN[N] | sqlalchemy.ARRAY, sqlalchemy.Boolean |
INTEGER[N] | sqlalchemy.ARRAY, sqlalchemy.Integer | |
BIGINT[N] | sqlalchemy.ARRAY, sqlalchemy.BIGINT | |
VECTOR(N) | sqlalchemy_kinetica.kinetica_types.VECTOR | |
REAL[N] | sqlalchemy.ARRAY, sqlalchemy.REAL | |
DOUBLE[N] | sqlalchemy.ARRAY, sqlalchemy.DOUBLE | |
VARCHAR[N] | sqlalchemy.ARRAY, sqlalchemy.VARCHAR |
Creating Tables
The full set of Kinetica table operations, table options, and column types are available through the execution of Kinetica SQL as string literals. A large portion of the Kinetica table feature set is available via SQLAlchemy objects and the Kinetica dialect.
Column Types
The following demonstrates creating a table with various column types using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
Replicated Tables
The following demonstrates creating a replicated table using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
Sharded Tables with Options
The following demonstrates creating a sharded table with a variety of table options applied, using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
CREATE TABLE...AS
CREATE TABLE ... AS is supported in both native SQL and in the Kinetica Dialect for SQLAlchemy as the following examples show:
|
|
|
|
External Tables
CREATE EXTERNAL TABLE is supported in both native SQL and in the Kinetica Dialect for SQLAlchemy as the following examples show:
|
|
|
|
Managing Data
The full set of Kinetica data manipulation operations are available through both the execution of Kinetica SQL as string literals as well as via SQLAlchemy objects and the Kinetica dialect.
Inserting Data as Values
The following demonstrates inserting data as raw values using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
Inserting Data from a Query
The following demonstrates inserting data from a query using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
Inserting Data using a CTE
The following demonstrates inserting data with a CTE using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
Updating Data with Constants
The following demonstrates updating data with raw values using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
Updating Data with Subquery Filter
The following demonstrates updating data with a subquery filter (subquery in the WHERE clause) using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
Updating Data with Subquery Assignment
The following demonstrates updating data with a subquery assignment (subquery in the SET clause) using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
Updating Data with Join
The following demonstrates updating data with a join using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
Deleting Data with Constants
The following demonstrates deleting data with raw values using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
Deleting Data with Subquery Filter
The following demonstrates deleting data with a subquery filter (subquery in the WHERE clause) using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
Querying Data
The full Kinetica query capability is available through the execution of Kinetica SQL as string literals. Much of the Kinetica query feature set is available via SQLAlchemy objects and the Kinetica dialect.
CTE
The following demonstrates a CTE query using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
Join
The following demonstrates a join query using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
ASOF Join
The following demonstrates an ASOF join query using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
Aggregation (GROUP BY)
The following demonstrates an aggregation query using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
Aggregation (ROLLUP)
The following demonstrates an aggregated roll-up query using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
Aggregation (GROUPING SETS)
The following demonstrates an aggregated grouping sets query using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
Window Functions (Rolling Sum)
The following demonstrates a rolling sum window function query using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
Window Functions (Moving Average)
The following demonstrates a moving average window function query using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
Window Functions (Ranking)
The following demonstrates a ranking window function query using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
Pivot
The following demonstrates a pivot query using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
Unpivot
The following demonstrates an unpivot query using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
Set Union (Deduplicate)
The following demonstrates a set union query that keeps duplicate records using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
Set Union (Keep Duplicates)
The following demonstrates a set union query that discards duplicate records using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
Set Intersection
The following demonstrates a set intersection query that discards duplicate records using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
Set Exception
The following demonstrates a set exception query that discards duplicate records using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|
Full Text Search
The following demonstrates using the full text search feature using a SQL literal as compared to using SQLAlchemy objects:
|
|
|
|