-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from SergeyBychkov/MG-4228
MG-4228: Bump Airflow to 2.6
- Loading branch information
Showing
20 changed files
with
2,029 additions
and
1,368 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,27 @@ | ||
import yaml | ||
from pandas import DataFrame | ||
from airflow.utils.context import Context | ||
from pandas import DataFrame, concat | ||
|
||
|
||
def val_translate(context, in_df: DataFrame, file_name: str) -> DataFrame: | ||
task = context.get('task') | ||
def val_translate(context: Context, in_df: DataFrame, file_name: str) -> DataFrame: | ||
"""Translate values | ||
:param context: Airflow context | ||
:param in_df: incoming df | ||
:param file_name: translate file | ||
:return: | ||
""" | ||
task = context.get("task") | ||
out_df = in_df.copy() | ||
with open(f'{task.dag.etc_dir}/{file_name}', 'r', encoding='utf-8') as cfg: | ||
with open(f"{task.dag.etc_dir}/{file_name}", "r", encoding="utf-8") as cfg: # type: ignore | ||
config = yaml.safe_load(cfg) | ||
out_df['test'] = out_df.apply( | ||
lambda row: config[row['test']], | ||
axis=1 | ||
) | ||
out_df["test"] = out_df.apply(lambda row: config[row["test"]], axis=1) | ||
return out_df | ||
|
||
|
||
def append_dfs(_context: Context, *sources: DataFrame) -> DataFrame: | ||
"""Append several dfs in one | ||
:param _context: Airflow context | ||
:param sources: dfs | ||
:return: | ||
""" | ||
return concat([*sources]) |
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
42 changes: 20 additions & 22 deletions
42
data-detective-airflow/dags/dags/dummy_python/code/code.py
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 |
---|---|---|
@@ -1,45 +1,43 @@ | ||
from data_detective_airflow.constants import PG_CONN_ID | ||
|
||
from data_detective_airflow.dag_generator.dags import TDag | ||
from data_detective_airflow.operators.extractors import DBDump | ||
from data_detective_airflow.operators.sinks import PgSCD1 | ||
from data_detective_airflow.operators.transformers import PyTransform | ||
from data_detective_airflow.dag_generator.dags import TDag | ||
from data_detective_airflow.operators.transformers import Append, PyTransform | ||
|
||
|
||
def fill_dag(tdag: TDag): | ||
DBDump( | ||
task_id='test1', | ||
task_id="test1", | ||
conn_id=PG_CONN_ID, | ||
sql='/code/test1.sql', | ||
dag=tdag | ||
sql="/code/test1.sql", | ||
dag=tdag, | ||
) | ||
|
||
DBDump( | ||
task_id='test2', | ||
task_id="test2", | ||
conn_id=PG_CONN_ID, | ||
sql='/code/test1.sql', | ||
dag=tdag | ||
sql="/code/test1.sql", | ||
dag=tdag, | ||
) | ||
|
||
PyTransform( | ||
task_id='transform', | ||
source=['test2'], | ||
task_id="transform", | ||
source=["test2"], | ||
transformer_callable=lambda _context, df: df, | ||
dag=tdag | ||
dag=tdag, | ||
) | ||
|
||
PyTransform( | ||
task_id='append_all', | ||
source=['transform', 'test1'], | ||
transformer_callable=lambda _context, transform, test1: transform.append(test1, sort=False), | ||
dag=tdag | ||
Append( | ||
task_id="append_all", | ||
source=["transform", "test1"], | ||
dag=tdag, | ||
) | ||
|
||
PgSCD1( | ||
task_id='sink', | ||
source=['append_all'], | ||
task_id="sink", | ||
source=["append_all"], | ||
conn_id=PG_CONN_ID, | ||
table_name='test2', | ||
key=['test'], | ||
dag=tdag | ||
table_name="test2", | ||
key=["test"], | ||
dag=tdag, | ||
) |
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,11 @@ | ||
from airflow.utils.context import Context | ||
from pandas import DataFrame, concat | ||
|
||
|
||
def append_dfs(_context: Context, *sources: DataFrame) -> DataFrame: | ||
"""Append several dfs in one | ||
:param _context: Airflow context | ||
:param sources: dfs | ||
:return: | ||
""" | ||
return concat([*sources]) |
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
31 changes: 13 additions & 18 deletions
31
data-detective-airflow/data_detective_airflow/operators/transformers/append.py
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 |
---|---|---|
@@ -1,35 +1,30 @@ | ||
from airflow import AirflowException | ||
from typing import Any, Optional | ||
|
||
from pandas import concat | ||
|
||
from data_detective_airflow.operators.tbaseoperator import TBaseOperator | ||
|
||
|
||
class Append(TBaseOperator): | ||
"""Merge multiple objects into one | ||
The object must have an append method | ||
"""Merge multiple DataFrames into one | ||
:param source: Source | ||
:param append_options: Additional options for pandas.concat() method | ||
:param kwargs: Additional params for the TBaseOperator | ||
""" | ||
|
||
ui_color = '#8f75d1' | ||
ui_color = "#8f75d1" | ||
|
||
def __init__(self, source: list[str], **kwargs): | ||
def __init__(self, source: list[str], append_options: Optional[dict[str, Any]] = None, **kwargs): | ||
super().__init__(**kwargs) | ||
self.source = source | ||
self.append_options = append_options or {} | ||
for src in self.source: | ||
self.dag.task_dict[src] >> self # pylint: disable=pointless-statement | ||
|
||
def execute(self, context): | ||
result = None | ||
self.log.info('Start appending') | ||
for src in self.source: | ||
read_result = self.dag.task_dict[src].result.read(context) | ||
if result is None: | ||
result = read_result | ||
if 'append' not in dir(result): | ||
raise AirflowException('Object should have "append" method.') | ||
continue | ||
result = result.append(read_result, sort=False) | ||
self.log.info('Writing pickle result') | ||
self.result.write(result, context) | ||
self.log.info('Finish') | ||
self.log.info("Start appending...") | ||
result = [self.dag.task_dict[src].result.read(context) for src in self.source] | ||
self.log.info("Writing pickle result") | ||
self.result.write(concat(result, **self.append_options), context) | ||
self.log.info("Finish") |
Large diffs are not rendered by default.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
data-detective-airflow/tests_data/dags/dummy_petl/append_all.md
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
data-detective-airflow/tests_data/dags/dummy_petl/transform.md
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
data-detective-airflow/tests_data/dags/dummy_processed/dump_rows.md
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
data-detective-airflow/tests_data/dags/dummy_processed/sink.md
This file was deleted.
Oops, something went wrong.