guan404ming commented on PR #48875:
URL: https://github.com/apache/airflow/pull/48875#issuecomment-2804049822
Here is my draft
```rst
.. Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
.. http://www.apache.org/licenses/LICENSE-2.0
.. Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
SQL Data Frameworks Integration
==============================
The :class:`~airflow.providers.common.sql.hooks.sql.DbApiHook` provides
built-in integration with popular data analysis frameworks, allowing you to
directly query databases and retrieve results as either Pandas or Polars
dataframes. This integration simplifies data workflows by eliminating the need
for manual conversion between SQL query results and data frames.
Pandas Integration
----------------
`Pandas <https://pandas.pydata.org/>`_ is a widely used data analysis and
manipulation library. The SQL hook allows you to directly retrieve query
results as Pandas DataFrames, which is particularly useful for further data
transformation, analysis, or visualization within your Airflow tasks.
.. code-block:: python
# Get complete DataFrame in a single operation
df = hook.get_df(
sql="SELECT * FROM my_table WHERE date_column >= %s",
parameters=["2023-01-01"],
df_type="pandas"
)
# Get DataFrame in chunks for memory-efficient processing of large
results
for chunk_df in hook.get_df_by_chunks(
sql="SELECT * FROM large_table",
chunksize=10000,
df_type="pandas"
):
process_chunk(chunk_df)
To use this feature, install the necessary dependencies:
.. code-block:: bash
pip install 'apache-airflow-providers-common-sql[pandas]'
Polars Integration
----------------
`Polars <https://pola.rs/>`_ is a modern, high-performance DataFrame library
implemented in Rust with Python bindings. It's designed for speed and
efficiency when working with large datasets. The SQL hook supports retrieving
data directly as Polars DataFrames, which can be particularly beneficial for
performance-critical data processing tasks.
.. code-block:: python
# Get complete DataFrame in a single operation
df = hook.get_df(
sql="SELECT * FROM my_table WHERE date_column >= %s",
parameters={"date_column": "2023-01-01"},
df_type="polars"
)
# Get DataFrame in chunks for memory-efficient processing of large
results
for chunk_df in hook.get_df_by_chunks(
sql="SELECT * FROM large_table",
chunksize=10000,
df_type="polars"
):
process_chunk(chunk_df)
To use this feature, install the necessary dependencies:
.. code-block:: bash
pip install 'apache-airflow-providers-common-sql[polars]'
Common Parameters
----------------
Both Pandas and Polars integrations support common parameters:
* ``sql``: SQL query to execute
* ``parameters``: Query parameters (supports various formats depending on
database driver)
* ``df_type``: "pandas" or "polars" to specify return type
* ``chunksize``: When using ``get_df_by_chunks``, specifies the number of
rows per chunk
* Additional keyword arguments are passed directly to the underlying
``pandas.read_sql`` or ``polars.read_database`` functions
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]