-
Notifications
You must be signed in to change notification settings - Fork 14.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AIP-47 Migrate ElasticSearch into new system tests #22811
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# 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. | ||
|
||
""" | ||
Example Airflow DAG for Elasticsearch Query. | ||
""" | ||
import os | ||
from datetime import datetime | ||
|
||
from airflow import models | ||
from airflow.decorators import task | ||
from airflow.providers.elasticsearch.hooks.elasticsearch import ElasticsearchHook | ||
|
||
ENV_ID = os.environ.get("SYSTEM_TESTS_ENV_ID") | ||
DAG_ID = 'elasticsearch_dag' | ||
CONN_ID = 'elasticsearch_default' | ||
|
||
|
||
@task(task_id='es_print_tables') | ||
def show_tables(): | ||
""" | ||
show_tables queries elasticsearch to list available tables | ||
""" | ||
# [START howto_elasticsearch_query] | ||
es = ElasticsearchHook(elasticsearch_conn_id=CONN_ID) | ||
|
||
# Handle ES conn with context manager | ||
with es.get_conn() as es_conn: | ||
tables = es_conn.execute('SHOW TABLES') | ||
for table, *_ in tables: | ||
print(f"table: {table}") | ||
return True | ||
# [END howto_elasticsearch_query] | ||
|
||
|
||
with models.DAG( | ||
DAG_ID, | ||
schedule_interval="@once", | ||
start_date=datetime(2021, 1, 1), | ||
catchup=False, | ||
tags=["example", "elasticsearch"], | ||
) as dag: | ||
execute_query = show_tables() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that sphinx will only take content between START and END markers. So your documentation will show:
which is not telling much. Unless it's what you intendent consider moving the markers to where you define the show_tables:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. Moving them to inside of the method works even better. |
||
( | ||
# TEST BODY | ||
execute_query | ||
) | ||
|
||
from tests.system.utils import get_test_run # noqa: E402 | ||
|
||
# Needed to run the example DAG with pytest (see: tests/system/README.md#run_via_pytest) | ||
test_run = get_test_run(dag) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a particular reason you moved these globals? They aren't being pulled from ENV and are each only used once, seems perfectly reasonable to me to keep them inlined as they were in the sample DAG?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ferruzzi I followed the steps mentioned in this doc to design the system.
https://cwiki.apache.org/confluence/display/AIRFLOW/AIP-47+New+design+of+Airflow+System+Tests#AIP47NewdesignofAirflowSystemTests-design_details
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My mistake. I forgot the AIP suggested moving them to globals. Carry on 😛