Skip to content

Commit

Permalink
Add document for export_file (#633)
Browse files Browse the repository at this point in the history
* Add document for export_file
  • Loading branch information
sunank200 authored Aug 12, 2022
1 parent 5e9c9d3 commit 905f214
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
30 changes: 30 additions & 0 deletions docs/astro/sql/operators/export.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
.. _export_file:

====================
export_file operator
====================

.. _export_file_operator:

When to use the ``export_file`` operator
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The ``export_file`` function writes SQL table to csv/parquet on local/S3/GCS. The ``export_file`` function imports data from :ref:`supported_databases` or pandas dataframe.

There are two use cases of the ``export_file`` operator.


Case 1: Export file from table.
The following example saves your :ref:`table` of data to file storage using the ``export_file``, which returns a pointer to File object.

.. literalinclude:: ../../../../example_dags/example_google_bigquery_gcs_load_and_save.py
:language: python
:start-after: [START export_example_1]
:end-before: [END export_example_1]

Case 2: Export file from dataframe.
The following example saves your pandas dataframe of data to file storage using the ``aql.export_file``, which returns a pointer to File object.

.. literalinclude:: ../../../../example_dags/example_google_bigquery_gcs_load_and_save.py
:language: python
:start-after: [START export_example_2]
:end-before: [END export_example_2]
17 changes: 16 additions & 1 deletion example_dags/example_google_bigquery_gcs_load_and_save.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,28 @@ def extract_top_5_movies(input_df: pd.DataFrame):

t2 = extract_top_5_movies(input_df=t1)

# [START export_example_1]
aql.export_file(
task_id="save_to_gcs",
task_id="save_file_to_gcs",
input_data=t1,
output_file=File(
path=f"{gcs_bucket}/{{ task_instance_key_str }}/all_movies.csv",
conn_id="gcp_conn",
),
if_exists="replace",
)
# [END export_example_1]

# [START export_example_2]
aql.export_file(
task_id="save_dataframe_to_gcs",
input_data=t2,
output_file=File(
path=f"{gcs_bucket}/{{ task_instance_key_str }}/top_5_movies.csv",
conn_id="gcp_conn",
),
if_exists="replace",
)
# [END export_example_2]

aql.cleanup()

0 comments on commit 905f214

Please sign in to comment.