DAGエアフローの理解

前述の最初のDAGエアフローの、すでに私たちの最初のタスクを実行します。この論文では、このタスクを豊かにします。

私たちの仕事の内容を見直して

  • 私たちは、DAGの名前を定義しHello-World、これが呼ばれて、dag_id
  • 補足説明
  • スケジューリング間隔のschedule_intervalを定義し、これはcronの式です
  • これは、bashのタスクを紹介します
  • パラメータDAGによって定義される重要なパラメータdefault_argsがあり、

別のタスクを実行する方法

。現時点で異なる動作を実行するために、異なるオペレータ、内蔵の数を導入することにより、内側空気流:

https://github.com/apache/airflow/tree/master/airflow/operators

一部のサードパーティにも貢献しました:

https://github.com/apache/airflow/tree/master/airflow/contrib/operators

あなたはまた、彼らのタスクタイプのプラグを作り、独自のプラグインを書くことができます。

あなたはの導入限り、これらのプラグインを使用する場合

from airflow.operators.bash_operator import BashOperator
from airflow.operators.dummy_operator import DummyOperator
from airflow.operators.python_operator import BranchPythonOperator
from operators.rdbms_to_redis_operator import RDBMS2RedisOperator
from operators.rdbms_to_hive_operator import RDBMS2HiveOperator
from operators.hive_to_rdbms_operator import Hive2RDBMSOperator

その後、必要なパラメータを入力します。

t1 = BashOperator(task_id="hello", 
            bash_command="echo 'Hello World, today is {{ ds }}'", 
            dag=dag)

Https://github.com/apache/airflow/tree/master/airflow/example_dagsソースならびにこれらのタスクを使用するプラグインを参照することができます。

タスク実行の日付を取得する方法

これだけでは、タスク神社日付テンプレート変数を介して取得することができ、このシンプルな表情で記事を引っ張って価値があります。

十分な基本的な以下のユーザー変数

templated_command = """
    echo "current bizdate is: {{ ds }} "
    echo "current bizdate in number:  {{ ds_nodash }} "
    echo "7days after: {{ macros.ds_add(ds, 7)}} "
    echo "5 days ago:  {{ macros.ds_add(ds, -5) }} "
    echo "bizdate iso8601 {{ ts }} "
    echo "bizdate format:  {{ execution_date.strftime("%d-%m-%Y") }} "
    echo "bizdate 5 days ago format: {{ (execution_date - macros.timedelta(days=5)).strftime("%Y-%m-%d") }} "

"""

t1 = BashOperator(
    task_id='print_date1',
    bash_command=templated_command,
    # on_success_callback=compass_utils.success_callback(dingding_conn_id='dingding_bigdata', receivers="ryanmiao"),
    dag=dag)

実行結果のログは次のとおりです。

    echo "current bizdate is: 2019-09-28 "
    echo "current bizdate in number:  20190928 "
    echo "7days after: 2019-10-05 "
    echo "5 days ago:  2019-09-23 "
    echo "bizdate iso8601 2019-09-28T01:00:00+08:00 "
    echo "bizdate format:  28-09-2019 "
    echo "bizdate 5 days ago format: 2019-09-23 "

警告

成功した場合、タスクが自分を実行し、結果を実行する方法を、私たちは通知を必要としています。私はときに障害を伝えることができ、伝えることができます。

default_args = {
    'owner': 'ryanmiao',
    'depends_on_past': False,
    'start_date': datetime(2019, 5, 1, 9),
    'email': ['[email protected]'],
    'email_on_failure': False,
    'email_on_retry': False,
    # 'on_failure_callback': compass_utils.ding_failure_callback('dingding_bigdata'),
    # 'on_success_callback': compass_utils.ding_success_callback('dingding_bigdata')
}

デフォルトでは、障害メール通知に電子メールが付属して、あなたは設定ファイルに電子メールを設定する必要があります。もちろん、我々は通常、独自の通知サービスを持っているだけでなく、自分自身の証明書などを統合します。だから、エアフローは、通知コールバックを提供します。

on_failure_callback 障害が実行するPythonの関数、

on_success_callback Pythonの関数、実行成功

例えば、私は爪の通知を追加する必要があります。

from airflow.contrib.operators.dingding_operator import DingdingOperator

def failure_callback(context):
    """
    The function that will be executed on failure.

    :param context: The context of the executed task.
    :type context: dict
    """
    message = 'AIRFLOW TASK FAILURE TIPS:\n' \
              'DAG:    {}\n' \
              'TASKS:  {}\n' \
              'Reason: {}\n' \
        .format(context['task_instance'].dag_id,
                context['task_instance'].task_id,
                context['exception'])
    return DingdingOperator(
        task_id='dingding_success_callback',
        dingding_conn_id='dingding_default',
        message_type='text',
        message=message,
        at_all=True,
    ).execute(context)


args['on_failure_callback'] = failure_callback

グループトークンの爪を設定するには、背景管理-接続し、CONNIDを挙げることができます。

同様に、私たちは、電子メール、電話か何かを送信するために使用され、私たち自身の使用のhttpリクエスト通知サービスああ、呼び出すことができ、カスタマイズすることができます。このカスタム通知を達成するために、バックカバーのカスタムプラグイン。

DAGのタスクの依存関係

タスクの依存関係を定義したDAGは簡単です:

a >> b   b依赖a
a << b   a依赖b
a >> b >> c   依赖可以串起来
[a,b] >> c   可以依赖多个

最終的には完全な依存関係を組み立て、分割してそれぞれの依存関係ステートメントをラップします。

DAGのいくつかのパラメータ

ソースコードのコメントを見てみましょう

"""
    A dag (directed acyclic graph) is a collection of tasks with directional
    dependencies. A dag also has a schedule, a start date and an end date
    (optional). For each schedule, (say daily or hourly), the DAG needs to run
    each individual tasks as their dependencies are met. Certain tasks have
    the property of depending on their own past, meaning that they can't run
    until their previous schedule (and upstream tasks) are completed.

    DAGs essentially act as namespaces for tasks. A task_id can only be
    added once to a DAG.

    :param dag_id: The id of the DAG
    :type dag_id: str
    :param description: The description for the DAG to e.g. be shown on the webserver
    :type description: str
    :param schedule_interval: Defines how often that DAG runs, this
        timedelta object gets added to your latest task instance's
        execution_date to figure out the next schedule
    :type schedule_interval: datetime.timedelta or
        dateutil.relativedelta.relativedelta or str that acts as a cron
        expression
    :param start_date: The timestamp from which the scheduler will
        attempt to backfill
    :type start_date: datetime.datetime
    :param end_date: A date beyond which your DAG won't run, leave to None
        for open ended scheduling
    :type end_date: datetime.datetime
    :param template_searchpath: This list of folders (non relative)
        defines where jinja will look for your templates. Order matters.
        Note that jinja/airflow includes the path of your DAG file by
        default
    :type template_searchpath: str or list[str]
    :param template_undefined: Template undefined type.
    :type template_undefined: jinja2.Undefined
    :param user_defined_macros: a dictionary of macros that will be exposed
        in your jinja templates. For example, passing ``dict(foo='bar')``
        to this argument allows you to ``{{ foo }}`` in all jinja
        templates related to this DAG. Note that you can pass any
        type of object here.
    :type user_defined_macros: dict
    :param user_defined_filters: a dictionary of filters that will be exposed
        in your jinja templates. For example, passing
        ``dict(hello=lambda name: 'Hello %s' % name)`` to this argument allows
        you to ``{{ 'world' | hello }}`` in all jinja templates related to
        this DAG.
    :type user_defined_filters: dict
    :param default_args: A dictionary of default parameters to be used
        as constructor keyword parameters when initialising operators.
        Note that operators have the same hook, and precede those defined
        here, meaning that if your dict contains `'depends_on_past': True`
        here and `'depends_on_past': False` in the operator's call
        `default_args`, the actual value will be `False`.
    :type default_args: dict
    :param params: a dictionary of DAG level parameters that are made
        accessible in templates, namespaced under `params`. These
        params can be overridden at the task level.
    :type params: dict
    :param concurrency: the number of task instances allowed to run
        concurrently
    :type concurrency: int
    :param max_active_runs: maximum number of active DAG runs, beyond this
        number of DAG runs in a running state, the scheduler won't create
        new active DAG runs
    :type max_active_runs: int
    :param dagrun_timeout: specify how long a DagRun should be up before
        timing out / failing, so that new DagRuns can be created. The timeout
        is only enforced for scheduled DagRuns, and only once the
        # of active DagRuns == max_active_runs.
    :type dagrun_timeout: datetime.timedelta
    :param sla_miss_callback: specify a function to call when reporting SLA
        timeouts.
    :type sla_miss_callback: types.FunctionType
    :param default_view: Specify DAG default view (tree, graph, duration,
                                                   gantt, landing_times)
    :type default_view: str
    :param orientation: Specify DAG orientation in graph view (LR, TB, RL, BT)
    :type orientation: str
    :param catchup: Perform scheduler catchup (or only run latest)? Defaults to True
    :type catchup: bool
    :param on_failure_callback: A function to be called when a DagRun of this dag fails.
        A context dictionary is passed as a single parameter to this function.
    :type on_failure_callback: callable
    :param on_success_callback: Much like the ``on_failure_callback`` except
        that it is executed when the dag succeeds.
    :type on_success_callback: callable
    :param access_control: Specify optional DAG-level permissions, e.g.,
        "{'role1': {'can_dag_read'}, 'role2': {'can_dag_read', 'can_dag_edit'}}"
    :type access_control: dict
    :param is_paused_upon_creation: Specifies if the dag is paused when created for the first time.
        If the dag exists already, this flag will be ignored. If this optional parameter
        is not specified, the global config setting will be used.
    :type is_paused_upon_creation: bool or None
    """

emmm、ここでそれを分解されていない、私は理解を使用する傾向があります。時計に対して使用する場合。

概要

ダグ組成物は、整理して理解することが容易であるという特性とYAMLの構成よりも簡単な、Pythonの構文のスタイル文です。

ダグは、パラメータを定義し、タスクタイプのオペレータを定義し、定義されたタスクは、ビンに依存します。

おすすめ

転載: www.cnblogs.com/woshimrf/p/airflow-dag.html