Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zypp-io/df_to_azure
Repository for Automatically creating pipelines with copy Activity from blob to SQL. The main functionality is for fast uploading pandas DataFrames to a SQL database, using Azure Data Factory.
https://github.com/zypp-io/df_to_azure
azure blob-storage bulk-insert dataframe incremental sql sql-table upsert
Last synced: 1 day ago
JSON representation
Repository for Automatically creating pipelines with copy Activity from blob to SQL. The main functionality is for fast uploading pandas DataFrames to a SQL database, using Azure Data Factory.
- Host: GitHub
- URL: https://github.com/zypp-io/df_to_azure
- Owner: zypp-io
- License: apache-2.0
- Created: 2020-09-03T12:12:42.000Z (over 4 years ago)
- Default Branch: development
- Last Pushed: 2024-09-06T12:12:58.000Z (4 months ago)
- Last Synced: 2024-12-08T11:07:52.026Z (about 1 month ago)
- Topics: azure, blob-storage, bulk-insert, dataframe, incremental, sql, sql-table, upsert
- Language: Python
- Homepage:
- Size: 319 KB
- Stars: 1
- Watchers: 4
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[![Downloads](https://pepy.tech/badge/df_to_azure)](https://pepy.tech/project/keyvault)
[![Open Source](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://opensource.org/)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![PyPI](https://img.shields.io/pypi/v/df_to_azure)](https://pypi.org/project/df-to-azure/)
[![Latest release](https://badgen.net/github/release/zypp-io/df_to_azure)](https://github.com/zypp-io/df_to_azure/releases)DF to Azure
===> Python module for fast upload of pandas DataFrame to Azure SQL Database using automatic created pipelines in Azure Data Factory.
## Introduction
The purpose of this project is to upload large datasets using Azure Data Factory combined with an Azure SQL Server.
In steps the following process kicks off:
1. The data will be uploaded as a .csv file to Azure Blob storage.
2. A SQL table is prepared based on [pandas DataFrame types](https://pandas.pydata.org/pandas-docs/stable/user_guide/basics.html#basics-dtypes),
which will be converted to the corresponding [SQLAlchemy types](https://docs.sqlalchemy.org/en/14/core/type_basics.html).
3. A pipeline is created in datafactory for uploading the .csv from the Blob storage into the SQL table.
4. The pipeline is triggered, so that the .csv file is bulk inserted into the SQL table.## How it works
Based on the following attributes, it is possible to bulk insert your dataframe into the SQL Database:
```python
from df_to_azure import df_to_azuredf_to_azure(df=df, tablename="table_name", schema="schema", method="create")
```1. `df`: dataframe you wish to export
2. `tablename`: desired name of the table
3. `schema`: desired sql schema
4. `method`: option for "create" "append" or "upsert"
5. `id_field`: id field of the table. Necessary if `method` is set to "upsert"**Important**: the csv's are uploaded to a container called `dftoazure`, so create this in your storage account before using this module.
##### Upsert / create or append
It is possible to upsert the SQL table with (new) records, if present in the dataframe you want to upload.
Based on the id_field, the SQL table is being checked on overlapping values.
If there are new records, the "old" records will be updated in the SQL table.
The new records will be uploaded and appended to the current SQL table.# Settings
To use this module, you need to add the `azure subscriptions settings` and `azure data factory settings` to your environment variables.
We recommend to work with `.env` files (or even better, automatically load them with [Azure Keyvault](https://pypi.org/project/keyvault/)) and load them in during runtime. But this is optional and they can be set as system variables as well.
Use the following template when using `.env`## Parquet
Since version 0.6.0, functionality for uploading dataframe to parquet is supported. simply add argument `parquet=True` to upload the dataframe to the Azure storage container parquet.
The arguments tablename and schema will be used to create a folder structure. if parquet is set to True, the dataset will not be uploaded to a SQL database.```text
# --- ADF SETTINGS ---# data factory settings
rg_name : ""
rg_location: "westeurope"
df_name : ""# blob settings
ls_blob_account_name : ""
ls_blob_container_name : ""
ls_blob_account_key : ""# SQL settings
SQL_SERVER: ""
SQL_DB: ""
SQL_USER: ""
SQL_PW: ""# --- AZURE SETTINGS ---
# azure credentials for connecting to azure subscription.
client_id : ""
secret : ""
tenant : ""
subscription_id : ""
```## Maintained by [Zypp](https://github.com/zypp-io):
- [Melvin Folkers](https://github.com/melvinfolkers)
- [Erfan Nariman](https://github.com/erfannariman)## Support:
For support on using this module, you can reach us at [[email protected]](mailto:[email protected])---
## Testing
To run the test suite, use:
```commandline
pytest df_to_azure
```To run pytest for a single test:
```commandline
pytest df_to_azure/tests/test_df_to_azure.py::test_duplicate_keys_upsert
```