This repository contains YQL dialect for SqlAlchemy 2.0.
Documentation: https://ydb-platform.github.io/ydb-sqlalchemy
Note: Dialect also works with SqlAlchemy 1.4, but it is not fully tested.
To install ydb-sqlalchemy from PyPI:
$ pip install ydb-sqlalchemyTo work with current ydb-sqlalchemy version clone this repo and run from source root:
$ pip install -U .Connect to local YDB using SqlAlchemy:
import sqlalchemy as sa
engine = sa.create_engine("yql+ydb://localhost:2136/local")
with engine.connect() as conn:
rs = conn.execute(sa.text("SELECT 1 AS value"))
print(rs.fetchone())To specify credentials, you should pass credentials object to connect_args argument of create_engine method.
To use static credentials you should specify username and password as follows:
engine = sa.create_engine(
"yql+ydb://localhost:2136/local",
connect_args = {
"credentials": {
"username": "...",
"password": "..."
}
}
)To use access token credentials you should specify token as follows:
engine = sa.create_engine(
"yql+ydb://localhost:2136/local",
connect_args = {
"credentials": {
"token": "..."
}
}
)To use service account credentials you should specify service_account_json as follows:
engine = sa.create_engine(
"yql+ydb://localhost:2136/local",
connect_args = {
"credentials": {
"service_account_json": {
"id": "...",
"service_account_id": "...",
"created_at": "...",
"key_algorithm": "...",
"public_key": "...",
"private_key": "..."
}
}
}
)To use any credentials that comes with ydb package, just pass credentials object as follows:
import ydb.iam
engine = sa.create_engine(
"yql+ydb://localhost:2136/local",
connect_args = {
"credentials": ydb.iam.MetadataUrlCredentials()
}
)To setup alembic to work with YDB please check this example.
Run the command from the root directory of the repository to start YDB in a local docker container.
$ docker-compose upTo run all tests execute the command from the root directory of the repository:
$ tox -e test-allRun specific test:
$ tox -e test -- test/test_core.pyCheck code style:
$ tox -e styleReformat code:
$ tox -e isort
$ tox -e black-formatRun example (needs running local YDB):
$ python -m pip install virtualenv
$ virtualenv venv
$ source venv/bin/activate
$ pip install -r requirements.txt
$ python examples/example.pyIt is possible to use YDB SA engine with pandas fuctions to_sql() and read_sql. However, there are some limitations:
-
to_sqlmethod can not be used with column tables, since it is impossible to specifyNOT NULLcolumns with currentto_sqlarguments. YDB requires column tables to haveNOT NULLattribute onPKcolumns. -
to_sqlis not fully optimized to load huge datasets. It is recommended to usemethod="multi"and avoid setting a very largechunksize. -
read_sqlis not fully optimized to load huge datasets and could lead to significant memory consumptions.