Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ For reference and tutorials, see the [Firebolt Python SDK reference](https://pyt

## Connection parameters
These parameters are used to connect to a Firebolt database:
- **engine_url** - url for an engine to make requests to. Can be retrieved from Web UI, or from [engine](https://github.com/firebolt-db/firebolt-sdk/tree/main/src/firebolt/model/engine.py) attribute `endpoint`
- **database** - name of the database to make queries to
- **username** - account username
- **password** - account password

Optional parameters
- **api_endpoint** - api hostname for logging in. Defaults to `api.app.firebolt.io`.
- **account_name** - name of firebolt account
- **client_id** - credentials client id
- **cliend_secret** - credentials client secret
- **database [Optional]** - name of the database to connect to
- **engine_name [Optional]** - name of the engine to connect to

## Examples
See [PEP-249](https://www.python.org/dev/peps/pep-0249) for the DB API reference and specifications. An example [jupyter notebook](https://github.com/firebolt-db/firebolt-sdk/tree/main/examples/dbapi.ipynb) is included to illustrate the use of the Firebolt API.
Expand Down
139 changes: 58 additions & 81 deletions examples/dbapi.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
"metadata": {},
"outputs": [],
"source": [
"from firebolt.db import connect\n",
"from firebolt.db import connect, OperationalError\n",
"from firebolt.client import DEFAULT_API_URL\n",
"from firebolt.client.auth import ClientCredentials\n",
"from datetime import datetime"
]
},
Expand All @@ -35,16 +36,12 @@
"metadata": {},
"outputs": [],
"source": [
"# Only one of these two parameters should be specified\n",
"engine_url = \"\"\n",
"assert bool(engine_url) != bool(\n",
" engine_name\n",
"), \"Specify only one of engine_name and engine_url\"\n",
"\n",
"database_name = \"\"\n",
"username = \"\"\n",
"password = \"\"\n",
"api_endpoint = DEFAULT_API_URL"
"client_id = \"\"\n",
"client_secret = \"\"\n",
"account_name = \"\"\n",
"engine_name = \"\" # Optional\n",
"database_name = \"\" # Optional\n",
"api_endpoint = DEFAULT_API_URL # Optional"
]
},
{
Expand All @@ -64,11 +61,10 @@
"source": [
"# create a connection based on provided credentials\n",
"connection = connect(\n",
" engine_url=engine_url,\n",
" auth=ClientCredentials(client_id, client_secret),\n",
" account_name=account_name,\n",
" engine_name=engine_name,\n",
" database=database_name,\n",
" username=username,\n",
" password=password,\n",
" api_endpoint=api_endpoint,\n",
")\n",
"\n",
Expand Down Expand Up @@ -218,7 +214,7 @@
" select * from test_table\n",
" \"\"\"\n",
" )\n",
"except:\n",
"except OperationalError:\n",
" pass\n",
"cursor.fetchall()"
]
Expand All @@ -228,7 +224,8 @@
"id": "b1cd4ff2",
"metadata": {},
"source": [
"## Async interface"
"## Async interface\n",
"**NOTE**: In order to make async examples work in jupyter, you would need to install [trio-jupyter](https://github.com/mehaase/trio-jupyter) library and select **Python 3 Trio** kernel"
]
},
{
Expand All @@ -238,7 +235,7 @@
"metadata": {},
"outputs": [],
"source": [
"from firebolt.async_db import connect"
"from firebolt.async_db import connect as async_connect"
]
},
{
Expand All @@ -256,30 +253,17 @@
"metadata": {},
"outputs": [],
"source": [
"async def async_connect():\n",
" # create a connection based on provided credentials\n",
" connection = await connect(\n",
" engine_url=engine_url,\n",
" engine_name=engine_name,\n",
" database=database_name,\n",
" username=username,\n",
" password=password,\n",
" api_endpoint=api_endpoint,\n",
" )\n",
"# create a connection based on provided credentials\n",
"async_connection = await async_connect(\n",
" auth=ClientCredentials(client_id, client_secret),\n",
" account_name=account_name,\n",
" engine_name=engine_name,\n",
" database=database_name,\n",
" api_endpoint=api_endpoint,\n",
")\n",
"\n",
" # create a cursor for connection\n",
" cursor = connection.cursor()\n",
" return cursor"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "58885c37",
"metadata": {},
"outputs": [],
"source": [
"async_cursor = await async_connect()"
"# create a cursor for connection\n",
"async_cursor = async_connection.cursor()"
]
},
{
Expand All @@ -297,26 +281,15 @@
"metadata": {},
"outputs": [],
"source": [
"async def run_queries(cursor):\n",
" await cursor.execute(\n",
" \"create fact table if not exists test_table (id int, name text, dt datetime) primary index id\"\n",
" )\n",
" await cursor.execute(\n",
" \"insert into test_table values (1, 'hello', '2021-01-01 01:01:01'),\"\n",
" \"(2, 'world', '2022-02-02 02:02:02'),\"\n",
" \"(3, '!', '2023-03-03 03:03:03')\"\n",
" )\n",
" await cursor.execute(\"select * from test_table\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "768fc91a",
"metadata": {},
"outputs": [],
"source": [
"await run_queries(async_cursor)"
"await async_cursor.execute(\n",
" \"create fact table if not exists test_table (id int, name text, dt datetime) primary index id\"\n",
")\n",
"await async_cursor.execute(\n",
" \"insert into test_table values (1, 'hello', '2021-01-01 01:01:01'),\"\n",
" \"(2, 'world', '2022-02-02 02:02:02'),\"\n",
" \"(3, '!', '2023-03-03 03:03:03')\"\n",
")\n",
"await async_cursor.execute(\"select * from test_table\")"
]
},
{
Expand Down Expand Up @@ -353,20 +326,9 @@
"metadata": {},
"outputs": [],
"source": [
"async def print_results(cursor):\n",
" print(await cursor.fetchone())\n",
" print(await cursor.fetchmany(1))\n",
" print(await cursor.fetchall())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "15f24a0a",
"metadata": {},
"outputs": [],
"source": [
"await print_results(async_cursor)"
"print(await cursor.fetchone())\n",
"print(await cursor.fetchmany(1))\n",
"print(await cursor.fetchall())"
]
},
{
Expand All @@ -386,27 +348,42 @@
"source": [
"# manually\n",
"connection.close()\n",
"print(connection.closed)\n",
"\n",
"await async_connection.aclose()\n",
"print(async_connection.closed)\n",
"\n",
"# using context manager\n",
"with connect(\n",
" engine_url=engine_url,\n",
" auth=ClientCredentials(client_id, client_secret),\n",
" account_name=account_name,\n",
" engine_name=engine_name,\n",
" database=database_name,\n",
" username=username,\n",
" password=password,\n",
" api_endpoint=api_endpoint,\n",
") as conn:\n",
" # create cursors, perform database queries\n",
" pass\n",
"conn.closed"
"print(conn.closed)\n",
"\n",
"# using context manager\n",
"async with await async_connect(\n",
" auth=ClientCredentials(client_id, client_secret),\n",
" account_name=account_name,\n",
" engine_name=engine_name,\n",
" database=database_name,\n",
" api_endpoint=api_endpoint,\n",
") as async_conn:\n",
" # create cursors, perform database queries\n",
" pass\n",
"async_conn.closed"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "python37",
"display_name": "Python 3 Trio",
"language": "python",
"name": "python37"
"name": "python3-trio"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -418,7 +395,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.12"
"version": "3.9.16"
}
},
"nbformat": 4,
Expand Down
Loading