forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AIRFLOW-5912] Expose lineage API (apache#7138)
Lineage data is exposed via the experimental api endpoint per dag.
- Loading branch information
1 parent
5b44c0f
commit 3730c24
Showing
13 changed files
with
379 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# 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. | ||
""" | ||
Lineage apis | ||
""" | ||
import datetime | ||
from typing import Any, Dict, List | ||
|
||
from airflow.api.common.experimental import check_and_get_dag, check_and_get_dagrun | ||
from airflow.lineage import PIPELINE_INLETS, PIPELINE_OUTLETS | ||
from airflow.models.xcom import XCom | ||
from airflow.utils.session import provide_session | ||
|
||
|
||
@provide_session | ||
def get_lineage(dag_id: str, execution_date: datetime.datetime, session=None) -> Dict[str, Dict[str, Any]]: | ||
""" | ||
Gets the lineage information for dag specified | ||
""" | ||
dag = check_and_get_dag(dag_id) | ||
check_and_get_dagrun(dag, execution_date) | ||
|
||
inlets: List[XCom] = XCom.get_many(dag_ids=dag_id, execution_date=execution_date, | ||
key=PIPELINE_INLETS, session=session).all() | ||
outlets: List[XCom] = XCom.get_many(dag_ids=dag_id, execution_date=execution_date, | ||
key=PIPELINE_OUTLETS, session=session).all() | ||
|
||
lineage: Dict[str, Dict[str, Any]] = {} | ||
for meta in inlets: | ||
lineage[meta.task_id] = {'inlets': meta.value} | ||
|
||
for meta in outlets: | ||
lineage[meta.task_id]['outlets'] = meta.value | ||
|
||
return {'task_ids': lineage} |
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,73 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# 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. | ||
import os | ||
from datetime import timedelta | ||
|
||
import scrapbook as sb | ||
|
||
import airflow | ||
from airflow.lineage import AUTO | ||
from airflow.models import DAG | ||
from airflow.operators.papermill_operator import PapermillOperator | ||
from airflow.operators.python_operator import PythonOperator | ||
|
||
|
||
def check_notebook(inlets, execution_date): | ||
""" | ||
Verify the message in the notebook | ||
""" | ||
notebook = sb.read_notebook(inlets[0].url) | ||
message = notebook.scraps['message'] | ||
print(f"Message in notebook {message} for {execution_date}") | ||
|
||
if message.data != f"Ran from Airflow at {execution_date}!": | ||
return False | ||
|
||
return True | ||
|
||
|
||
args = { | ||
'owner': 'airflow', | ||
'start_date': airflow.utils.dates.days_ago(2) | ||
} | ||
|
||
dag = DAG( | ||
dag_id='example_papermill_operator', default_args=args, | ||
schedule_interval='0 0 * * *', | ||
dagrun_timeout=timedelta(minutes=60)) | ||
|
||
run_this = PapermillOperator( | ||
task_id="run_example_notebook", | ||
dag=dag, | ||
input_nb=os.path.join(os.path.dirname(os.path.realpath(__file__)), | ||
"input_notebook.ipynb"), | ||
output_nb="/tmp/out-{{ execution_date }}.ipynb", | ||
parameters={"msgs": "Ran from Airflow at {{ execution_date }}!"} | ||
) | ||
|
||
check_output = PythonOperator( | ||
task_id='check_out', | ||
python_callable=check_notebook, | ||
dag=dag, | ||
inlets=AUTO) | ||
|
||
check_output.set_upstream(run_this) | ||
|
||
if __name__ == "__main__": | ||
dag.cli() |
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,120 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
" Licensed to the Apache Software Foundation (ASF) under one\n", | ||
" or more contributor license agreements. See the NOTICE file\n", | ||
" distributed with this work for additional information\n", | ||
" regarding copyright ownership. The ASF licenses this file\n", | ||
" to you under the Apache License, Version 2.0 (the\n", | ||
" \"License\"); you may not use this file except in compliance\n", | ||
" with the License. You may obtain a copy of the License at\n", | ||
"\n", | ||
" http://www.apache.org/licenses/LICENSE-2.0\n", | ||
"\n", | ||
" Unless required by applicable law or agreed to in writing,\n", | ||
" software distributed under the License is distributed on an\n", | ||
" \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n", | ||
" KIND, either express or implied. See the License for the\n", | ||
" specific language governing permissions and limitations\n", | ||
" under the License." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"This is an example jupyter notebook for Apache Airflow that shows how to use\n", | ||
"papermill in combination with scrapbook" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import scrapbook as sb" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"The parameter tag for cells is used to tell papermill where it can find variables it needs to set" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": { | ||
"tags": [ | ||
"parameters" | ||
] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"msgs = \"Hello!\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Inside the notebook you can save data by calling the glue function. Then later you can read the results of that notebook by “scrap” name (see the Airflow Papermill example DAG)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"application/scrapbook.scrap.text+json": { | ||
"data": "Hello!", | ||
"encoder": "text", | ||
"name": "message", | ||
"version": 1 | ||
} | ||
}, | ||
"metadata": { | ||
"scrapbook": { | ||
"data": true, | ||
"display": false, | ||
"name": "message" | ||
} | ||
}, | ||
"output_type": "display_data" | ||
} | ||
], | ||
"source": [ | ||
"sb.glue('message', msgs)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"celltoolbar": "Tags", | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.5" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
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
Oops, something went wrong.