Skip to main content

Azure Data Explorer (Kusto) dialect for SQLAlchemy

Project description

Azure Data Explorer (Kusto) dialect for SQLAlchemy

pypi

sqlalchemy-kusto implements a DBAPI (PEP-249) and SQLAlchemy dialect that enables SQL query execution via SQLAlchemy.

Current project includes support for two dialects: SQL dialect and KQL dialect.

SQL dialect

Current implementation has full support for SQL queries. But pay your attention that Kusto implementation of T-SQL has not full coverage; check the list of known issues.

KQL dialect

KQL dialect still in progress. Please, use it on your own risk for now.

Notice that implemented Kusto dialects don't support DDL statements and inserts, deletes, updates.

Installation

pip install sqlalchemy-kusto

Library usage

Using DBAPI

from sqlalchemy_kusto import connect

connection = connect(
        cluster=kusto_url,
        database=database_name,
        msi=False,
        user_msi=None,
        azure_ad_client_id=kusto_client_id,
        azure_ad_client_secret=kusto_client_secret,
        azure_ad_tenant_id=kusto_tenant_id,
)

result = connection.execute(f"select 1").fetchall()

Using SQLAlchemy raw sql

from sqlalchemy.engine import create_engine

engine = create_engine(
    f"kustosql+{kusto_url}/{database_name}?"
    f"msi=False&azure_ad_client_id={kusto_client_id}&"
    f"azure_ad_client_secret={kusto_client_secret}&"
    f"azure_ad_tenant_id={kusto_tenant_id}"
)
engine.connect()
cursor = engine.execute(f"select top 1")
data_rows = cursor.fetchall()

Using SQLAlchemy

from sqlalchemy import create_engine, MetaData, Table, Column, String, Integer

engine = create_engine(
    f"kustosql+{kusto_url}/{database_name}?"
    f"msi=False&azure_ad_client_id={kusto_client_id}&"
    f"azure_ad_client_secret={kusto_client_secret}&"
    f"azure_ad_tenant_id={kusto_tenant_id}"
)

my_table = Table(
        "MyTable",
        MetaData(),
        Column("Id", Integer),
        Column("Text", String),
)

query = my_table.select().limit(5)

engine.connect()
cursor = engine.execute(query)
print([row for row in cursor])

Using with Apache Superset

Apache Superset starting from version 1.5 also supports Kusto database engine spec.
When connecting to a new data source you may choose a data source type either KustoSQL or KustoKQL depending on the dialect you want to use.

There are following connection string formats:

# KustoSQL
kustosql+https://<CLUSTER_URL>/<DATABASE>?azure_ad_client_id=<CLIENT_ID>&azure_ad_client_secret=<CLIENT_SECRET>&azure_ad_tenant_id=<TENANT_ID>&msi=False

# KustoKQL
kustokql+https://<CLUSTER_URL>/<DATABASE>?azure_ad_client_id=<CLIENT_ID>&azure_ad_client_secret=<CLIENT_SECRET>&azure_ad_tenant_id=<TENANT_ID>&msi=False

Important notice on package version compatibility.
Apache Superset stable releases 1.5 and 2.0 dependent on sqlalchemy==1.3.24. If you want to use sqlalchemy-kusto with these versions you need to install version 1.* of the package.

Current master branch of the apache/superset dependent on sqlalchemy==1.4.36. If you want to use sqlalchemy-kusto with the latest unstable version of apache/superset, you need to install version 2.* of the package.

Contributing

Please see the CONTRIBUTING.md for development setup and contributing process guidelines.


Issue in Apache Superset repository that inspired current solution.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlalchemy-kusto-2.0.0.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

sqlalchemy_kusto-2.0.0-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy-kusto-2.0.0.tar.gz.

File metadata

  • Download URL: sqlalchemy-kusto-2.0.0.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.11

File hashes

Hashes for sqlalchemy-kusto-2.0.0.tar.gz
Algorithm Hash digest
SHA256 dc08d2d1f9bf4d86d2183dbe7bb50f00375af0c0e7859b44a273bba474413624
MD5 ae812b305c5d3813b3a699964381b90a
BLAKE2b-256 e9fd8d41f24b09a6930ddd711698c1f62c0be2f6c6e52010d0755516190ac453

See more details on using hashes here.

File details

Details for the file sqlalchemy_kusto-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_kusto-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 28656dceee458cd7807ac3686ed15e8f886ca954c9d816f3b2dee7568c39253e
MD5 2db778224e2daaea245c7d07c970e3fb
BLAKE2b-256 07355a6c305e6740693ebc367f9ba5bcc2f6317c564bbfa8ecbff13289b573c0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page