Skip to content

Commit 7b6be1b

Browse files
committed
[Python/SQLAlchemy] Improve README
1 parent a43cda7 commit 7b6be1b

File tree

1 file changed

+66
-3
lines changed

1 file changed

+66
-3
lines changed

by-language/python-sqlalchemy/README.rst

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,56 @@ About
1111

1212
Example programs demonstrating CrateDB's SQLAlchemy adapter and dialect.
1313

14+
This section and examples are mostly about `DataFrame operations with SQLAlchemy`_,
15+
specifically about how to insert data into `CrateDB`_ efficiently using `pandas`_ and
16+
`Dask`_.
17+
18+
1419

1520
*****
1621
Usage
1722
*****
1823

24+
The CrateDB Python driver provides a convenience function ``insert_bulk``. It
25+
can be used like this::
26+
27+
# CrateDB Cloud
28+
# DBURI = "crate://admin:<PASSWORD>@example.aks1.westeurope.azure.cratedb.net:4200?ssl=true"
29+
30+
# CrateDB Self-Managed
31+
# DBURI = "crate://crate@localhost:4200/"
32+
33+
import sqlalchemy as sa
34+
from crate.client.sqlalchemy.support import insert_bulk
35+
36+
# pandas
37+
engine = sa.create_engine(DBURI, **kwargs)
38+
df.to_sql(
39+
name="testdrive",
40+
con=engine,
41+
if_exists="append",
42+
index=False,
43+
chunksize=5_000,
44+
method=insert_bulk,
45+
)
46+
47+
# Dask
48+
ddf.to_sql(
49+
"testdrive",
50+
uri=DBURI,
51+
index=False,
52+
if_exists="replace",
53+
chunksize=10_000,
54+
parallel=True,
55+
method=insert_bulk,
56+
)
57+
58+
59+
60+
*****
61+
Setup
62+
*****
63+
1964
To start a CrateDB instance on your machine for evaluation purposes, invoke::
2065

2166
docker run -it --rm --publish=4200:4200 --publish=5432:5432 crate
@@ -29,6 +74,15 @@ Navigate to example program directory, and install prerequisites::
2974
source .venv/bin/activate
3075
pip install --upgrade --requirement requirements.txt
3176

77+
Invoke the software tests::
78+
79+
pytest
80+
81+
82+
********
83+
Examples
84+
********
85+
3286
Run example programs::
3387

3488
# Connect to CrateDB on localhost.
@@ -42,9 +96,18 @@ Run example programs::
4296

4397
time python insert_dask.py
4498

45-
# Connect to CrateDB Cloud.
46-
time python insert_pandas.py --dburi='crate://admin:<PASSWORD>@example.aks1.westeurope.azure.cratedb.net:4200?ssl=true'
99+
Use ``insert_pandas.py`` to connect to any other database instance::
100+
101+
export DBURI="crate://crate@localhost:4200/"
102+
export DBURI="crate://admin:<PASSWORD>@example.aks1.westeurope.azure.cratedb.net:4200?ssl=true"
103+
time python insert_pandas.py --dburi="${DBURI}"
47104

48105
.. TIP::
49106

50-
For more information, please refer to the header section of each of the provided example programs.
107+
For more information, please refer to the header sections of each of the provided example programs.
108+
109+
110+
.. _CrateDB: https://github.com/crate/crate
111+
.. _Dask: https://www.dask.org/
112+
.. _DataFrame operations with SQLAlchemy: https://cratedb.com/docs/python/en/latest/by-example/sqlalchemy/dataframe.html
113+
.. _pandas: https://pandas.pydata.org/

0 commit comments

Comments
 (0)