-
Notifications
You must be signed in to change notification settings - Fork 1
pandas: Starter and Jupyter tutorial. Guidelines for efficient ingest. #297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
amotl
wants to merge
4
commits into
airflow
Choose a base branch
from
pandas
base: airflow
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
(pandas-efficient-ingest)= | ||
# Guide to efficient data ingestion to CrateDB with pandas | ||
|
||
## Introduction | ||
Bulk insert is a technique for efficiently inserting large amounts of data into a database by submitting multiple rows of data in a single database transaction. Instead of executing multiple SQL `INSERT` statements for each individual row of data, the bulk insert allows the database to process and store a batch of data at once. This approach can significantly improve the performance of data insertion, especially when dealing with large datasets. | ||
|
||
In this tutorial, you will learn how to efficiently perform [bulk inserts](https://crate.io/docs/python/en/latest/by-example/sqlalchemy/dataframe.html) into CrateDB with [pandas](https://pandas.pydata.org/) using the `insert_bulk` method, available in the `crate` Python library. To follow along with this tutorial, you should have the following: | ||
|
||
* A working installation of CrateDB. To get started with CrateDB check [this link](https://crate.io/lp-free-trial?hsCtaTracking=c2099713-cafa-4de6-a97e-2f86d80a788f%7C3a12b78e-e605-461c-9bd8-628d0d9e2522). | ||
* Python, Pandas, SQLAlchemy, and [crate driver](https://pypi.org/project/crate/) installed on your machine | ||
* Basic familiarity with pandas and SQL | ||
|
||
## Bulk insert to CrateDB | ||
|
||
The following example illustrates how to implement batch insert with the pandas library by using the `insert_bulk` method available in the `crate` driver. | ||
|
||
```python | ||
import sqlalchemy as sa | ||
import crate | ||
import pandas as pd | ||
from sqlalchemy import create_engine | ||
from crate.client.sqlalchemy.support import insert_bulk | ||
from pandas._testing import makeTimeDataFrame | ||
|
||
INSERT_RECORDS = 5000000 | ||
CHUNK_SIZE = 50000 | ||
|
||
df = makeTimeDataFrame(nper=INSERT_RECORDS, freq="S") | ||
engine = sa.create_engine('crate://localhost:4200') | ||
|
||
df.to_sql( | ||
name="cratedb-demo", | ||
con=engine, | ||
if_exists="replace", | ||
index=False, | ||
chunksize=CHUNK_SIZE, | ||
method=insert_bulk, | ||
) | ||
``` | ||
|
||
By running this code, you will generate a DataFrame with a time-based index containing 5,000,000 rows of data. Each row represents a timestamp with a frequency of 1 second (`freq="S"`). The DataFrame is then inserted into a `cratedb-demo` table in CrateDB using the `to_sql()` method. If the table already exists, it will be replaced with the new data. The data insertion will be performed in batches, with each batch containing 50,000 records. Defining the `chunksize` parameter helps in managing memory and improving performance during the data insertion process. | ||
|
||
The above code runs in approximately 14s on a local Mac M1 machine with 16GiB RAM. However, if we insert data to CrateDB by setting the `method` parameter to `None` (one insert per row), the execution time increases to 27sec. | ||
|
||
## How to find the right chunksize | ||
|
||
Determining the right chunksize depends on several factors, such as the size of your data, the number of columns in your data set, and the available memory of your machine. | ||
|
||
The `chunksize` parameter in the `to_sql()` method controls the number of rows inserted in each batch. By default, `chunksize=None`, which means the entire DataFrame will be written to the database at once. However, when working with large datasets, it is recommended to set a smaller `chunksize` value to avoid memory issues and to improve the performance of the data insertion. | ||
|
||
To determine the right `chunksize` value, you can try different values and observe the memory usage and the time it takes to complete the data insertion. A good starting point is to set the `chunksize` value to a fraction of the total number of rows in your DataFrame. For example, you can start with a `chunksize` value of 10,000 or 50,000 rows and see how it performs. If the data insertion is slow, you can try increasing the `chunksize` value to reduce the number of batches. On the other hand, if you encounter memory issues, you can try reducing the `chunksize` value. | ||
|
||
## Conclusion | ||
|
||
Congratulations! You have learned how to implement an efficient data insert into CrateDB using Pandas and `insert_bulk` method. This method allows for efficient and fast data insertion, making it suitable for handling large datasets. | ||
|
||
If you like this tutorial and want to explore further CrateDB functionalities, please visit our [documentation](https://crate.io/docs) and join our [community](https://community.cratedb.com/). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
(pandas)= | ||
# pandas | ||
|
||
```{div} | ||
:style: "float: right" | ||
[{w=180px}](https://pandas.pydata.org/) | ||
``` | ||
```{div} .clearfix | ||
``` | ||
|
||
:::{rubric} About | ||
::: | ||
|
||
[pandas] is a fast, powerful, flexible, and easy-to-use open-source data analysis | ||
and manipulation tool, built on top of the Python programming language. | ||
|
||
Pandas (stylized as pandas) is a software library written for the Python programming | ||
language for data manipulation and analysis. In particular, it offers data structures | ||
and operations for manipulating numerical tables and time series. | ||
|
||
:::{rubric} Data Model | ||
::: | ||
- Pandas is built around data structures called Series and DataFrames. Data for these | ||
collections can be imported from various file formats such as comma-separated values, | ||
JSON, Parquet, SQL database tables or queries, and Microsoft Excel. | ||
- A Series is a 1-dimensional data structure built on top of NumPy's array. | ||
- Pandas includes support for time series, such as the ability to interpolate values | ||
and filter using a range of timestamps. | ||
- By default, a Pandas index is a series of integers ascending from 0, similar to the | ||
indices of Python arrays. However, indices can use any NumPy data type, including | ||
floating point, timestamps, or strings. | ||
- Pandas supports hierarchical indices with multiple values per data point. An index | ||
with this structure, called a "MultiIndex", allows a single DataFrame to represent | ||
multiple dimensions, similar to a pivot table in Microsoft Excel. Each level of a | ||
MultiIndex can be given a unique name. | ||
|
||
|
||
:::{rubric} Learn | ||
::: | ||
- {ref}`pandas-tutorial-start` | ||
- {ref}`pandas-tutorial-jupyter` | ||
- {ref}`arrow-import-parquet` | ||
- {ref}`pandas-efficient-ingest` | ||
- [Efficient batch/bulk INSERT operations with pandas, Dask, and SQLAlchemy] | ||
- [pandas code examples] | ||
|
||
|
||
:::{toctree} | ||
:maxdepth: 1 | ||
:hidden: | ||
Starter tutorial <tutorial-start> | ||
Jupyter tutorial <tutorial-jupyter> | ||
Efficient ingest <efficient-ingest> | ||
::: | ||
|
||
|
||
[Efficient batch/bulk INSERT operations with pandas, Dask, and SQLAlchemy]: https://cratedb.com/docs/python/en/latest/by-example/sqlalchemy/dataframe.html | ||
[pandas]: https://pandas.pydata.org/ | ||
[pandas code examples]: https://github.com/crate/cratedb-examples/tree/main/by-dataframe/pandas |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That code isn't using sqlalchemy-cratedb yet, so we should update it. Maybe you could lend a hand here to update and verify this document & code snippet, @karynzv?