-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
880 additions
and
76 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,86 @@ | ||
from datetime import datetime | ||
|
||
import pandas as pd | ||
from airflow import DAG | ||
|
||
from astro import sql as aql | ||
from astro.files import File | ||
from astro.table import Table | ||
|
||
START_DATE = datetime(2000, 1, 1) | ||
LAST_ONE_DF = pd.DataFrame(data={"title": ["Random movie"], "rating": [121]}) | ||
|
||
|
||
@aql.transform() | ||
def top_five_animations(input_table: Table): # skipcq: PYL-W0613 | ||
return """ | ||
SELECT * | ||
FROM {{input_table}} | ||
WHERE genre1='Animation' | ||
ORDER BY rating desc | ||
LIMIT 5 | ||
""" | ||
|
||
|
||
@aql.transform() | ||
def last_five_animations(input_table: Table): # skipcq: PYL-W0613 | ||
return """ | ||
SELECT * | ||
FROM {{input_table}} | ||
WHERE genre1='Animation' | ||
ORDER BY rating asc | ||
LIMIT 5 | ||
""" | ||
|
||
|
||
@aql.transform | ||
def union_top_and_last(first_table: Table, second_table: Table): # skipcq: PYL-W0613 | ||
"""Union `first_table` and `second_table` tables to create a simple dataset.""" | ||
return """ | ||
SELECT title, rating from {{first_table}} | ||
UNION | ||
SELECT title, rating from {{second_table}} | ||
""" | ||
|
||
|
||
@aql.transform | ||
def union_table_and_dataframe(input_table: Table, input_dataframe: pd.DataFrame): # skipcq: PYL-W0613 | ||
"""Union `union_table` table and `input_dataframe` dataframe to create a simple dataset.""" | ||
return """ | ||
SELECT title, rating from {{input_table}} | ||
UNION | ||
SELECT title, rating from {{input_dataframe}} | ||
""" | ||
|
||
|
||
with DAG( | ||
"example_transform_mysql", | ||
schedule_interval=None, | ||
start_date=START_DATE, | ||
catchup=False, | ||
) as dag: | ||
imdb_movies = aql.load_file( | ||
input_file=File(path="s3://astro-sdk/imdb_v2.csv"), | ||
task_id="load_csv", | ||
output_table=Table(conn_id="mysql_conn"), | ||
) | ||
|
||
top_five = top_five_animations( | ||
input_table=imdb_movies, | ||
output_table=Table( | ||
conn_id="mysql_conn", | ||
), | ||
) | ||
|
||
last_five = last_five_animations( | ||
input_table=imdb_movies, | ||
output_table=Table( | ||
conn_id="mysql_conn", | ||
), | ||
) | ||
|
||
union_table = union_top_and_last(top_five, last_five) | ||
|
||
union_table_and_dataframe(union_table, LAST_ONE_DF) | ||
|
||
aql.cleanup() |
This file contains 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 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
Oops, something went wrong.