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-XXX] Fix Minor issues with Azure Cosmos Operator (apache#4289)
- Fixed Documentation in integration.rst - Fixed Incorrect type in docstring of `AzureCosmosInsertDocumentOperator` - Added the Hook, Sensor and Operator in code.rst - Updated the name of example DAG and its filename to follow the convention
- Loading branch information
Showing
5 changed files
with
228 additions
and
224 deletions.
There are no files selected for viewing
128 changes: 64 additions & 64 deletions
128
...b/example_dags/example_cosmosdb_sensor.py → ...ple_dags/example_azure_cosmosdb_sensor.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,64 +1,64 @@ | ||
# -*- 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. | ||
|
||
""" | ||
This is only an example DAG to highlight usage of AzureCosmosDocumentSensor to detect | ||
if a document now exists. | ||
You can trigger this manually with `airflow trigger_dag example_cosmosdb_sensor`. | ||
*Note: Make sure that connection `azure_cosmos_default` is properly set before running | ||
this example.* | ||
""" | ||
|
||
from airflow import DAG | ||
from airflow.contrib.sensors.azure_cosmos_sensor import AzureCosmosDocumentSensor | ||
from airflow.contrib.operators.azure_cosmos_insertdocument_operator import AzureCosmosInsertDocumentOperator | ||
from airflow.utils import dates | ||
|
||
default_args = { | ||
'owner': 'airflow', | ||
'depends_on_past': False, | ||
'start_date': dates.days_ago(2), | ||
'email': ['[email protected]'], | ||
'email_on_failure': False, | ||
'email_on_retry': False | ||
} | ||
|
||
dag = DAG('example_cosmosdb_sensor', default_args=default_args) | ||
|
||
dag.doc_md = __doc__ | ||
|
||
t1 = AzureCosmosDocumentSensor( | ||
task_id='check_cosmos_file', | ||
database_name='airflow_example_db', | ||
collection_name='airflow_example_coll', | ||
document_id='airflow_checkid', | ||
azure_cosmos_conn_id='azure_cosmos_default', | ||
dag=dag) | ||
|
||
t2 = AzureCosmosInsertDocumentOperator( | ||
task_id='insert_cosmos_file', | ||
dag=dag, | ||
database_name='airflow_example_db', | ||
collection_name='new-collection', | ||
document={"id": "someuniqueid", "param1": "value1", "param2": "value2"}, | ||
azure_cosmos_conn_id='azure_cosmos_default') | ||
|
||
t2.set_upstream(t1) | ||
# -*- 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. | ||
|
||
""" | ||
This is only an example DAG to highlight usage of AzureCosmosDocumentSensor to detect | ||
if a document now exists. | ||
You can trigger this manually with `airflow trigger_dag example_cosmosdb_sensor`. | ||
*Note: Make sure that connection `azure_cosmos_default` is properly set before running | ||
this example.* | ||
""" | ||
|
||
from airflow import DAG | ||
from airflow.contrib.sensors.azure_cosmos_sensor import AzureCosmosDocumentSensor | ||
from airflow.contrib.operators.azure_cosmos_operator import AzureCosmosInsertDocumentOperator | ||
from airflow.utils import dates | ||
|
||
default_args = { | ||
'owner': 'airflow', | ||
'depends_on_past': False, | ||
'start_date': dates.days_ago(2), | ||
'email': ['[email protected]'], | ||
'email_on_failure': False, | ||
'email_on_retry': False | ||
} | ||
|
||
dag = DAG('example_azure_cosmosdb_sensor', default_args=default_args) | ||
|
||
dag.doc_md = __doc__ | ||
|
||
t1 = AzureCosmosDocumentSensor( | ||
task_id='check_cosmos_file', | ||
database_name='airflow_example_db', | ||
collection_name='airflow_example_coll', | ||
document_id='airflow_checkid', | ||
azure_cosmos_conn_id='azure_cosmos_default', | ||
dag=dag) | ||
|
||
t2 = AzureCosmosInsertDocumentOperator( | ||
task_id='insert_cosmos_file', | ||
dag=dag, | ||
database_name='airflow_example_db', | ||
collection_name='new-collection', | ||
document={"id": "someuniqueid", "param1": "value1", "param2": "value2"}, | ||
azure_cosmos_conn_id='azure_cosmos_default') | ||
|
||
t1 >> t2 |
138 changes: 69 additions & 69 deletions
138
...s/azure_cosmos_insertdocument_operator.py → ...ontrib/operators/azure_cosmos_operator.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,69 +1,69 @@ | ||
# -*- 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. | ||
|
||
from airflow.contrib.hooks.azure_cosmos_hook import AzureCosmosDBHook | ||
from airflow.models import BaseOperator | ||
from airflow.utils.decorators import apply_defaults | ||
|
||
|
||
class AzureCosmosInsertDocumentOperator(BaseOperator): | ||
""" | ||
Inserts a new document into the specified Cosmos database and collection | ||
It will create both the database and collection if they do not already exist | ||
:param database_name: The name of the database. (templated) | ||
:type database_name: str | ||
:param collection_name: The name of the collection. (templated) | ||
:type collection_name: str | ||
:param document: The document to insert | ||
:type document: json | ||
:param azure_cosmos_conn_id: reference to a CosmosDB connection. | ||
:type azure_cosmos_conn_id: str | ||
""" | ||
template_fields = ('database_name', 'collection_name') | ||
ui_color = '#e4f0e8' | ||
|
||
@apply_defaults | ||
def __init__(self, | ||
database_name, | ||
collection_name, | ||
document, | ||
azure_cosmos_conn_id='azure_cosmos_default', | ||
*args, | ||
**kwargs): | ||
super(AzureCosmosInsertDocumentOperator, self).__init__(*args, **kwargs) | ||
self.database_name = database_name | ||
self.collection_name = collection_name | ||
self.document = document | ||
self.azure_cosmos_conn_id = azure_cosmos_conn_id | ||
|
||
def execute(self, context): | ||
# Create the hook | ||
hook = AzureCosmosDBHook(azure_cosmos_conn_id=self.azure_cosmos_conn_id) | ||
|
||
# Create the DB if it doesn't already exist | ||
if (not hook.does_database_exist(self.database_name)): | ||
hook.create_database(self.database_name) | ||
|
||
# Create the collection as well | ||
if (not hook.does_collection_exist(self.collection_name, self.database_name)): | ||
hook.create_collection(self.collection_name, self.database_name) | ||
|
||
# finally insert the document | ||
hook.upsert_document(self.document, self.database_name, self.collection_name) | ||
# -*- 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. | ||
|
||
from airflow.contrib.hooks.azure_cosmos_hook import AzureCosmosDBHook | ||
from airflow.models import BaseOperator | ||
from airflow.utils.decorators import apply_defaults | ||
|
||
|
||
class AzureCosmosInsertDocumentOperator(BaseOperator): | ||
""" | ||
Inserts a new document into the specified Cosmos database and collection | ||
It will create both the database and collection if they do not already exist | ||
:param database_name: The name of the database. (templated) | ||
:type database_name: str | ||
:param collection_name: The name of the collection. (templated) | ||
:type collection_name: str | ||
:param document: The document to insert | ||
:type document: dict | ||
:param azure_cosmos_conn_id: reference to a CosmosDB connection. | ||
:type azure_cosmos_conn_id: str | ||
""" | ||
template_fields = ('database_name', 'collection_name') | ||
ui_color = '#e4f0e8' | ||
|
||
@apply_defaults | ||
def __init__(self, | ||
database_name, | ||
collection_name, | ||
document, | ||
azure_cosmos_conn_id='azure_cosmos_default', | ||
*args, | ||
**kwargs): | ||
super(AzureCosmosInsertDocumentOperator, self).__init__(*args, **kwargs) | ||
self.database_name = database_name | ||
self.collection_name = collection_name | ||
self.document = document | ||
self.azure_cosmos_conn_id = azure_cosmos_conn_id | ||
|
||
def execute(self, context): | ||
# Create the hook | ||
hook = AzureCosmosDBHook(azure_cosmos_conn_id=self.azure_cosmos_conn_id) | ||
|
||
# Create the DB if it doesn't already exist | ||
if not hook.does_database_exist(self.database_name): | ||
hook.create_database(self.database_name) | ||
|
||
# Create the collection as well | ||
if not hook.does_collection_exist(self.collection_name, self.database_name): | ||
hook.create_collection(self.collection_name, self.database_name) | ||
|
||
# finally insert the document | ||
hook.upsert_document(self.document, self.database_name, self.collection_name) |
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.