df_to_azure 2.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- df_to_azure-2.0.0/LICENSE +201 -0
- df_to_azure-2.0.0/PKG-INFO +224 -0
- df_to_azure-2.0.0/README.md +197 -0
- df_to_azure-2.0.0/df_to_azure/__init__.py +13 -0
- df_to_azure-2.0.0/df_to_azure/adf.py +281 -0
- df_to_azure-2.0.0/df_to_azure/auth.py +32 -0
- df_to_azure-2.0.0/df_to_azure/db.py +290 -0
- df_to_azure-2.0.0/df_to_azure/env.py +25 -0
- df_to_azure-2.0.0/df_to_azure/exceptions.py +42 -0
- df_to_azure-2.0.0/df_to_azure/export.py +429 -0
- df_to_azure-2.0.0/df_to_azure/settings.py +34 -0
- df_to_azure-2.0.0/df_to_azure/utils.py +95 -0
- df_to_azure-2.0.0/df_to_azure.egg-info/PKG-INFO +224 -0
- df_to_azure-2.0.0/df_to_azure.egg-info/SOURCES.txt +17 -0
- df_to_azure-2.0.0/df_to_azure.egg-info/dependency_links.txt +1 -0
- df_to_azure-2.0.0/df_to_azure.egg-info/requires.txt +8 -0
- df_to_azure-2.0.0/df_to_azure.egg-info/top_level.txt +1 -0
- df_to_azure-2.0.0/pyproject.toml +49 -0
- df_to_azure-2.0.0/setup.cfg +4 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: df_to_azure
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: Automatically write pandas DataFrames to SQL by creating pipelines in Azure Data Factory with copy activity from blob to SQL
|
|
5
|
+
Author-email: Zypp <hello@zypp.io>
|
|
6
|
+
Project-URL: Bug Tracker, https://github.com/zypp-io/df_to_azure/issues
|
|
7
|
+
Project-URL: Source, https://github.com/zypp-io/df_to_azure
|
|
8
|
+
Keywords: python,azure,data-warehouse,sql,blob
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Requires-Python: <3.14,>=3.11
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Requires-Dist: azure-identity>=1.12.0
|
|
19
|
+
Requires-Dist: azure-mgmt-datafactory>=7.1.0
|
|
20
|
+
Requires-Dist: azure-mgmt-resource>=23.1.1
|
|
21
|
+
Requires-Dist: azure-storage-blob>=12.20.0
|
|
22
|
+
Requires-Dist: pandas>=2.2.2
|
|
23
|
+
Requires-Dist: pyarrow>=16.1.0
|
|
24
|
+
Requires-Dist: pyodbc>=5.1.0
|
|
25
|
+
Requires-Dist: sqlalchemy>=2.0.36
|
|
26
|
+
Dynamic: license-file
|
|
27
|
+
|
|
28
|
+
DF to Azure
|
|
29
|
+
===
|
|
30
|
+
|
|
31
|
+
[](https://pepy.tech/project/keyvault)
|
|
32
|
+
[](https://opensource.org/)
|
|
33
|
+
[](https://github.com/psf/black)
|
|
34
|
+
[](https://pypi.org/project/df-to-azure/)
|
|
35
|
+
[](https://github.com/zypp-io/df_to_azure/releases)
|
|
36
|
+
|
|
37
|
+
> Python module for fast upload of pandas DataFrame to Azure SQL Database using automatic created pipelines in Azure Data Factory.
|
|
38
|
+
|
|
39
|
+
Supported Python versions: 3.11, 3.12, and 3.13.
|
|
40
|
+
|
|
41
|
+
## Introduction
|
|
42
|
+
|
|
43
|
+
The purpose of this project is to upload large datasets using Azure Data Factory combined with an Azure SQL Server.
|
|
44
|
+
In steps the following process kicks off:<p>
|
|
45
|
+
1. The data will be uploaded as a .csv file to Azure Blob storage.<br>
|
|
46
|
+
2. A SQL table is prepared based on [pandas DataFrame types](https://pandas.pydata.org/pandas-docs/stable/user_guide/basics.html#basics-dtypes),
|
|
47
|
+
which will be converted to the corresponding [SQLAlchemy types](https://docs.sqlalchemy.org/en/14/core/type_basics.html). <br>
|
|
48
|
+
3. A pipeline is created in datafactory for uploading the .csv from the Blob storage into the SQL table.<br>
|
|
49
|
+
4. The pipeline is triggered, so that the .csv file is bulk inserted into the SQL table.<br>
|
|
50
|
+
|
|
51
|
+
## How it works
|
|
52
|
+
|
|
53
|
+
Based on the following attributes, it is possible to bulk insert your dataframe into the SQL Database:
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
from df_to_azure import df_to_azure
|
|
57
|
+
|
|
58
|
+
df_to_azure(df=df, tablename="table_name", schema="schema", method="create")
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
1. `df`: dataframe you wish to export
|
|
62
|
+
2. `tablename`: desired name of the table
|
|
63
|
+
3. `schema`: desired sql schema
|
|
64
|
+
4. `method`: option for "create" "append" or "upsert"
|
|
65
|
+
5. `id_field`: id field of the table. Necessary if `method` is set to "upsert"
|
|
66
|
+
|
|
67
|
+
**Important**: the csv's are uploaded to a container called `dftoazure`, so create this in your storage account before using this module.
|
|
68
|
+
|
|
69
|
+
##### Upsert / create or append
|
|
70
|
+
It is possible to upsert the SQL table with (new) records, if present in the dataframe you want to upload.
|
|
71
|
+
Based on the id_field, the SQL table is being checked on overlapping values.
|
|
72
|
+
If there are new records, the "old" records will be updated in the SQL table.
|
|
73
|
+
The new records will be uploaded and appended to the current SQL table.
|
|
74
|
+
|
|
75
|
+
# Settings
|
|
76
|
+
The default authentication path is passwordless:
|
|
77
|
+
|
|
78
|
+
- Python Azure SDK clients use `DefaultAzureCredential`.
|
|
79
|
+
- Local development can use `az login`.
|
|
80
|
+
- Deployed Python workloads can use a managed identity.
|
|
81
|
+
- Data Factory linked services use the Data Factory managed identity by default.
|
|
82
|
+
|
|
83
|
+
You should not need SQL passwords, storage account keys, or storage connection strings for the default setup. When
|
|
84
|
+
legacy values are present, the package uses a fixed fallback order instead of auth-mode environment variables.
|
|
85
|
+
|
|
86
|
+
## Parquet
|
|
87
|
+
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.
|
|
88
|
+
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.
|
|
89
|
+
|
|
90
|
+
```text
|
|
91
|
+
# Azure subscription and Data Factory settings
|
|
92
|
+
subscription_id=""
|
|
93
|
+
rg_name=""
|
|
94
|
+
rg_location="westeurope"
|
|
95
|
+
df_name=""
|
|
96
|
+
|
|
97
|
+
# Storage account used for temporary SQL upload parquet files and parquet=True uploads.
|
|
98
|
+
# Use this for passwordless storage auth. If you use a storage connection string instead,
|
|
99
|
+
# ls_blob_account_name is not required.
|
|
100
|
+
ls_blob_account_name=""
|
|
101
|
+
|
|
102
|
+
# Azure SQL Database
|
|
103
|
+
SQL_SERVER="<server-name>.database.windows.net"
|
|
104
|
+
SQL_DB=""
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
The lowercase names above are the original names and remain supported. Uppercase aliases are also accepted, for example
|
|
108
|
+
`SUBSCRIPTION_ID`, `RG_NAME`, `RG_LOCATION`, `DF_NAME`, `LS_BLOB_ACCOUNT_NAME`, and `LS_BLOB_ACCOUNT_KEY`.
|
|
109
|
+
|
|
110
|
+
The authentication rule is the same for direct Python connections and the Data Factory linked services:
|
|
111
|
+
**explicit credentials win, and without them the package is passwordless.** To go passwordless, simply do not set the
|
|
112
|
+
password, key, and connection-string variables.
|
|
113
|
+
|
|
114
|
+
- Storage: `AZURE_STORAGE_CONNECTION_STRING` → `LS_BLOB_ACCOUNT_NAME` + `LS_BLOB_ACCOUNT_KEY` →
|
|
115
|
+
`LS_BLOB_ACCOUNT_NAME` with `DefaultAzureCredential` (Python) / managed identity (ADF).
|
|
116
|
+
- SQL: `SQL_USER` + `SQL_PW` → passwordless: `DefaultAzureCredential` access token over ODBC (Python) /
|
|
117
|
+
managed identity (ADF).
|
|
118
|
+
The ADF SQL linked service uses the user-assigned managed identity referenced by `DF_TO_AZURE_ADF_CREDENTIAL_NAME`
|
|
119
|
+
when set, otherwise the system-assigned managed identity.
|
|
120
|
+
- Azure management clients always use `DefaultAzureCredential`.
|
|
121
|
+
|
|
122
|
+
For a user-assigned managed identity on Data Factory, first create a credential in the Data Factory itself
|
|
123
|
+
(Manage → Credentials) that references the identity, then set the *name of that credential* — not the identity's
|
|
124
|
+
client ID or resource ID:
|
|
125
|
+
|
|
126
|
+
```text
|
|
127
|
+
DF_TO_AZURE_ADF_CREDENTIAL_NAME=""
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Azure permissions for passwordless auth
|
|
131
|
+
|
|
132
|
+
There are two identities involved in a normal SQL upload:
|
|
133
|
+
|
|
134
|
+
1. The identity running Python.
|
|
135
|
+
2. The managed identity of the Azure Data Factory that runs the copy pipeline.
|
|
136
|
+
|
|
137
|
+
For local development, sign in first:
|
|
138
|
+
|
|
139
|
+
```commandline
|
|
140
|
+
az login
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Grant the local developer identity:
|
|
144
|
+
|
|
145
|
+
- Azure permissions to create or update Data Factory pipelines, datasets, and linked services. `Data Factory Contributor` on the Data Factory is usually enough when `create=False`; resource group `Contributor` is needed when using `create=True`.
|
|
146
|
+
- `Storage Blob Data Contributor` on the storage account or the relevant containers.
|
|
147
|
+
- A contained Azure SQL user and database permissions for creating schemas, tables, and upsert procedures.
|
|
148
|
+
|
|
149
|
+
Example SQL permissions for the local developer or deployed Python host identity:
|
|
150
|
+
|
|
151
|
+
```sql
|
|
152
|
+
CREATE USER [user-or-managed-identity-name] FROM EXTERNAL PROVIDER;
|
|
153
|
+
ALTER ROLE db_datareader ADD MEMBER [user-or-managed-identity-name];
|
|
154
|
+
ALTER ROLE db_datawriter ADD MEMBER [user-or-managed-identity-name];
|
|
155
|
+
ALTER ROLE db_ddladmin ADD MEMBER [user-or-managed-identity-name];
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
Grant the Data Factory managed identity:
|
|
159
|
+
|
|
160
|
+
- `Storage Blob Data Reader` on the storage account or the `dftoazure` container so ADF can read the staged parquet file.
|
|
161
|
+
- An Azure SQL contained user with permission to insert into the target and staging tables.
|
|
162
|
+
- `EXECUTE` permission when using `method="upsert"` because ADF runs the generated upsert stored procedure.
|
|
163
|
+
|
|
164
|
+
Example SQL permissions for the Data Factory system-assigned managed identity:
|
|
165
|
+
|
|
166
|
+
```sql
|
|
167
|
+
CREATE USER [your-data-factory-name] FROM EXTERNAL PROVIDER;
|
|
168
|
+
ALTER ROLE db_datareader ADD MEMBER [your-data-factory-name];
|
|
169
|
+
ALTER ROLE db_datawriter ADD MEMBER [your-data-factory-name];
|
|
170
|
+
GRANT EXECUTE TO [your-data-factory-name];
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Azure SQL must have a Microsoft Entra admin configured before `CREATE USER ... FROM EXTERNAL PROVIDER` works.
|
|
174
|
+
|
|
175
|
+
For user-assigned managed identity on Data Factory, assign the identity to the factory, create a Data Factory credential for it, and set `DF_TO_AZURE_ADF_CREDENTIAL_NAME` to that credential name.
|
|
176
|
+
|
|
177
|
+
Passwordless SQL connections require a Microsoft ODBC Driver for SQL Server version that supports access-token
|
|
178
|
+
authentication. Use the newest available ODBC Driver 18 where possible.
|
|
179
|
+
|
|
180
|
+
### Cross-tenant databases
|
|
181
|
+
|
|
182
|
+
A Data Factory in a different subscription is fine with managed identity when it is still in the same tenant as the
|
|
183
|
+
Azure SQL server. When the Data Factory and Azure SQL server are in different tenants, managed identity may not be a
|
|
184
|
+
valid SQL principal in the target tenant. In that case, set `SQL_USER` and `SQL_PW`; the package will use those
|
|
185
|
+
credentials for both direct Python SQL setup and the ADF SQL linked service. No extra auth-mode variables are required.
|
|
186
|
+
|
|
187
|
+
## Maintained by [Zypp](https://github.com/zypp-io):
|
|
188
|
+
- [Melvin Folkers](https://github.com/melvinfolkers)
|
|
189
|
+
- [Erfan Nariman](https://github.com/erfannariman)
|
|
190
|
+
|
|
191
|
+
## Support:
|
|
192
|
+
For support on using this module, you can reach us at [hello@zypp.io](mailto:hello@zypp.io)
|
|
193
|
+
|
|
194
|
+
---
|
|
195
|
+
|
|
196
|
+
## Testing
|
|
197
|
+
|
|
198
|
+
The auth tests are unit tests and run without any Azure resources or environment variables:
|
|
199
|
+
|
|
200
|
+
```commandline
|
|
201
|
+
pytest df_to_azure/tests/test_auth.py
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
The rest of the test suite are integration tests: they create real pipelines, blob containers, and SQL tables in
|
|
205
|
+
Azure. To run them, point the environment variables from the [Settings](#settings) section at an Azure environment
|
|
206
|
+
you own:
|
|
207
|
+
|
|
208
|
+
```text
|
|
209
|
+
SUBSCRIPTION_ID=""
|
|
210
|
+
RG_NAME=""
|
|
211
|
+
RG_LOCATION="westeurope"
|
|
212
|
+
DF_NAME=""
|
|
213
|
+
LS_BLOB_ACCOUNT_NAME=""
|
|
214
|
+
SQL_SERVER="<server-name>.database.windows.net"
|
|
215
|
+
SQL_DB=""
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
Authenticate (for example with `az login`, see the permissions section above), then run:
|
|
219
|
+
|
|
220
|
+
```commandline
|
|
221
|
+
pytest df_to_azure
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Tip: put the variables in a local `.env` file (git-ignored) and run `uv run --env-file .env pytest df_to_azure`.
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
DF to Azure
|
|
2
|
+
===
|
|
3
|
+
|
|
4
|
+
[](https://pepy.tech/project/keyvault)
|
|
5
|
+
[](https://opensource.org/)
|
|
6
|
+
[](https://github.com/psf/black)
|
|
7
|
+
[](https://pypi.org/project/df-to-azure/)
|
|
8
|
+
[](https://github.com/zypp-io/df_to_azure/releases)
|
|
9
|
+
|
|
10
|
+
> Python module for fast upload of pandas DataFrame to Azure SQL Database using automatic created pipelines in Azure Data Factory.
|
|
11
|
+
|
|
12
|
+
Supported Python versions: 3.11, 3.12, and 3.13.
|
|
13
|
+
|
|
14
|
+
## Introduction
|
|
15
|
+
|
|
16
|
+
The purpose of this project is to upload large datasets using Azure Data Factory combined with an Azure SQL Server.
|
|
17
|
+
In steps the following process kicks off:<p>
|
|
18
|
+
1. The data will be uploaded as a .csv file to Azure Blob storage.<br>
|
|
19
|
+
2. A SQL table is prepared based on [pandas DataFrame types](https://pandas.pydata.org/pandas-docs/stable/user_guide/basics.html#basics-dtypes),
|
|
20
|
+
which will be converted to the corresponding [SQLAlchemy types](https://docs.sqlalchemy.org/en/14/core/type_basics.html). <br>
|
|
21
|
+
3. A pipeline is created in datafactory for uploading the .csv from the Blob storage into the SQL table.<br>
|
|
22
|
+
4. The pipeline is triggered, so that the .csv file is bulk inserted into the SQL table.<br>
|
|
23
|
+
|
|
24
|
+
## How it works
|
|
25
|
+
|
|
26
|
+
Based on the following attributes, it is possible to bulk insert your dataframe into the SQL Database:
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
from df_to_azure import df_to_azure
|
|
30
|
+
|
|
31
|
+
df_to_azure(df=df, tablename="table_name", schema="schema", method="create")
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
1. `df`: dataframe you wish to export
|
|
35
|
+
2. `tablename`: desired name of the table
|
|
36
|
+
3. `schema`: desired sql schema
|
|
37
|
+
4. `method`: option for "create" "append" or "upsert"
|
|
38
|
+
5. `id_field`: id field of the table. Necessary if `method` is set to "upsert"
|
|
39
|
+
|
|
40
|
+
**Important**: the csv's are uploaded to a container called `dftoazure`, so create this in your storage account before using this module.
|
|
41
|
+
|
|
42
|
+
##### Upsert / create or append
|
|
43
|
+
It is possible to upsert the SQL table with (new) records, if present in the dataframe you want to upload.
|
|
44
|
+
Based on the id_field, the SQL table is being checked on overlapping values.
|
|
45
|
+
If there are new records, the "old" records will be updated in the SQL table.
|
|
46
|
+
The new records will be uploaded and appended to the current SQL table.
|
|
47
|
+
|
|
48
|
+
# Settings
|
|
49
|
+
The default authentication path is passwordless:
|
|
50
|
+
|
|
51
|
+
- Python Azure SDK clients use `DefaultAzureCredential`.
|
|
52
|
+
- Local development can use `az login`.
|
|
53
|
+
- Deployed Python workloads can use a managed identity.
|
|
54
|
+
- Data Factory linked services use the Data Factory managed identity by default.
|
|
55
|
+
|
|
56
|
+
You should not need SQL passwords, storage account keys, or storage connection strings for the default setup. When
|
|
57
|
+
legacy values are present, the package uses a fixed fallback order instead of auth-mode environment variables.
|
|
58
|
+
|
|
59
|
+
## Parquet
|
|
60
|
+
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.
|
|
61
|
+
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.
|
|
62
|
+
|
|
63
|
+
```text
|
|
64
|
+
# Azure subscription and Data Factory settings
|
|
65
|
+
subscription_id=""
|
|
66
|
+
rg_name=""
|
|
67
|
+
rg_location="westeurope"
|
|
68
|
+
df_name=""
|
|
69
|
+
|
|
70
|
+
# Storage account used for temporary SQL upload parquet files and parquet=True uploads.
|
|
71
|
+
# Use this for passwordless storage auth. If you use a storage connection string instead,
|
|
72
|
+
# ls_blob_account_name is not required.
|
|
73
|
+
ls_blob_account_name=""
|
|
74
|
+
|
|
75
|
+
# Azure SQL Database
|
|
76
|
+
SQL_SERVER="<server-name>.database.windows.net"
|
|
77
|
+
SQL_DB=""
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
The lowercase names above are the original names and remain supported. Uppercase aliases are also accepted, for example
|
|
81
|
+
`SUBSCRIPTION_ID`, `RG_NAME`, `RG_LOCATION`, `DF_NAME`, `LS_BLOB_ACCOUNT_NAME`, and `LS_BLOB_ACCOUNT_KEY`.
|
|
82
|
+
|
|
83
|
+
The authentication rule is the same for direct Python connections and the Data Factory linked services:
|
|
84
|
+
**explicit credentials win, and without them the package is passwordless.** To go passwordless, simply do not set the
|
|
85
|
+
password, key, and connection-string variables.
|
|
86
|
+
|
|
87
|
+
- Storage: `AZURE_STORAGE_CONNECTION_STRING` → `LS_BLOB_ACCOUNT_NAME` + `LS_BLOB_ACCOUNT_KEY` →
|
|
88
|
+
`LS_BLOB_ACCOUNT_NAME` with `DefaultAzureCredential` (Python) / managed identity (ADF).
|
|
89
|
+
- SQL: `SQL_USER` + `SQL_PW` → passwordless: `DefaultAzureCredential` access token over ODBC (Python) /
|
|
90
|
+
managed identity (ADF).
|
|
91
|
+
The ADF SQL linked service uses the user-assigned managed identity referenced by `DF_TO_AZURE_ADF_CREDENTIAL_NAME`
|
|
92
|
+
when set, otherwise the system-assigned managed identity.
|
|
93
|
+
- Azure management clients always use `DefaultAzureCredential`.
|
|
94
|
+
|
|
95
|
+
For a user-assigned managed identity on Data Factory, first create a credential in the Data Factory itself
|
|
96
|
+
(Manage → Credentials) that references the identity, then set the *name of that credential* — not the identity's
|
|
97
|
+
client ID or resource ID:
|
|
98
|
+
|
|
99
|
+
```text
|
|
100
|
+
DF_TO_AZURE_ADF_CREDENTIAL_NAME=""
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Azure permissions for passwordless auth
|
|
104
|
+
|
|
105
|
+
There are two identities involved in a normal SQL upload:
|
|
106
|
+
|
|
107
|
+
1. The identity running Python.
|
|
108
|
+
2. The managed identity of the Azure Data Factory that runs the copy pipeline.
|
|
109
|
+
|
|
110
|
+
For local development, sign in first:
|
|
111
|
+
|
|
112
|
+
```commandline
|
|
113
|
+
az login
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Grant the local developer identity:
|
|
117
|
+
|
|
118
|
+
- Azure permissions to create or update Data Factory pipelines, datasets, and linked services. `Data Factory Contributor` on the Data Factory is usually enough when `create=False`; resource group `Contributor` is needed when using `create=True`.
|
|
119
|
+
- `Storage Blob Data Contributor` on the storage account or the relevant containers.
|
|
120
|
+
- A contained Azure SQL user and database permissions for creating schemas, tables, and upsert procedures.
|
|
121
|
+
|
|
122
|
+
Example SQL permissions for the local developer or deployed Python host identity:
|
|
123
|
+
|
|
124
|
+
```sql
|
|
125
|
+
CREATE USER [user-or-managed-identity-name] FROM EXTERNAL PROVIDER;
|
|
126
|
+
ALTER ROLE db_datareader ADD MEMBER [user-or-managed-identity-name];
|
|
127
|
+
ALTER ROLE db_datawriter ADD MEMBER [user-or-managed-identity-name];
|
|
128
|
+
ALTER ROLE db_ddladmin ADD MEMBER [user-or-managed-identity-name];
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Grant the Data Factory managed identity:
|
|
132
|
+
|
|
133
|
+
- `Storage Blob Data Reader` on the storage account or the `dftoazure` container so ADF can read the staged parquet file.
|
|
134
|
+
- An Azure SQL contained user with permission to insert into the target and staging tables.
|
|
135
|
+
- `EXECUTE` permission when using `method="upsert"` because ADF runs the generated upsert stored procedure.
|
|
136
|
+
|
|
137
|
+
Example SQL permissions for the Data Factory system-assigned managed identity:
|
|
138
|
+
|
|
139
|
+
```sql
|
|
140
|
+
CREATE USER [your-data-factory-name] FROM EXTERNAL PROVIDER;
|
|
141
|
+
ALTER ROLE db_datareader ADD MEMBER [your-data-factory-name];
|
|
142
|
+
ALTER ROLE db_datawriter ADD MEMBER [your-data-factory-name];
|
|
143
|
+
GRANT EXECUTE TO [your-data-factory-name];
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Azure SQL must have a Microsoft Entra admin configured before `CREATE USER ... FROM EXTERNAL PROVIDER` works.
|
|
147
|
+
|
|
148
|
+
For user-assigned managed identity on Data Factory, assign the identity to the factory, create a Data Factory credential for it, and set `DF_TO_AZURE_ADF_CREDENTIAL_NAME` to that credential name.
|
|
149
|
+
|
|
150
|
+
Passwordless SQL connections require a Microsoft ODBC Driver for SQL Server version that supports access-token
|
|
151
|
+
authentication. Use the newest available ODBC Driver 18 where possible.
|
|
152
|
+
|
|
153
|
+
### Cross-tenant databases
|
|
154
|
+
|
|
155
|
+
A Data Factory in a different subscription is fine with managed identity when it is still in the same tenant as the
|
|
156
|
+
Azure SQL server. When the Data Factory and Azure SQL server are in different tenants, managed identity may not be a
|
|
157
|
+
valid SQL principal in the target tenant. In that case, set `SQL_USER` and `SQL_PW`; the package will use those
|
|
158
|
+
credentials for both direct Python SQL setup and the ADF SQL linked service. No extra auth-mode variables are required.
|
|
159
|
+
|
|
160
|
+
## Maintained by [Zypp](https://github.com/zypp-io):
|
|
161
|
+
- [Melvin Folkers](https://github.com/melvinfolkers)
|
|
162
|
+
- [Erfan Nariman](https://github.com/erfannariman)
|
|
163
|
+
|
|
164
|
+
## Support:
|
|
165
|
+
For support on using this module, you can reach us at [hello@zypp.io](mailto:hello@zypp.io)
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
## Testing
|
|
170
|
+
|
|
171
|
+
The auth tests are unit tests and run without any Azure resources or environment variables:
|
|
172
|
+
|
|
173
|
+
```commandline
|
|
174
|
+
pytest df_to_azure/tests/test_auth.py
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
The rest of the test suite are integration tests: they create real pipelines, blob containers, and SQL tables in
|
|
178
|
+
Azure. To run them, point the environment variables from the [Settings](#settings) section at an Azure environment
|
|
179
|
+
you own:
|
|
180
|
+
|
|
181
|
+
```text
|
|
182
|
+
SUBSCRIPTION_ID=""
|
|
183
|
+
RG_NAME=""
|
|
184
|
+
RG_LOCATION="westeurope"
|
|
185
|
+
DF_NAME=""
|
|
186
|
+
LS_BLOB_ACCOUNT_NAME=""
|
|
187
|
+
SQL_SERVER="<server-name>.database.windows.net"
|
|
188
|
+
SQL_DB=""
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
Authenticate (for example with `az login`, see the permissions section above), then run:
|
|
192
|
+
|
|
193
|
+
```commandline
|
|
194
|
+
pytest df_to_azure
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
Tip: put the variables in a local `.env` file (git-ignored) and run `uv run --env-file .env pytest df_to_azure`.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
from .export import df_to_azure as df_to_azure
|
|
4
|
+
|
|
5
|
+
__version__ = "2.0.0"
|
|
6
|
+
|
|
7
|
+
logging.basicConfig(
|
|
8
|
+
format="%(asctime)s.%(msecs)03d [%(levelname)-5s] [%(name)s] - %(message)s",
|
|
9
|
+
datefmt="%Y-%m-%d %H:%M:%S",
|
|
10
|
+
level=logging.INFO,
|
|
11
|
+
)
|
|
12
|
+
logging.getLogger("azure.core.pipeline.policies.http_logging_policy").setLevel(logging.WARNING)
|
|
13
|
+
logging.getLogger("azure.identity._internal.get_token_mixin").setLevel(logging.WARNING)
|