{"id":927,"date":"2021-04-21T18:51:48","date_gmt":"2021-04-21T10:51:48","guid":{"rendered":"https:\/\/blog.humh.cn\/?p=927"},"modified":"2021-08-05T17:35:01","modified_gmt":"2021-08-05T09:35:01","slug":"%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96airflow%e5%86%85%e9%83%a8%e5%8f%98%e9%87%8f%e5%b9%b6%e7%bb%8f%e8%bf%87%e9%80%bb%e8%be%91%e5%a4%84%e7%90%86%e5%90%8e%e4%bc%a0%e5%85%a5bashoperator%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/blog.humh.cn\/?p=927","title":{"rendered":"\u5982\u4f55\u83b7\u53d6Airflow\u5185\u90e8\u53d8\u91cf\u5e76\u7ecf\u8fc7\u903b\u8f91\u5904\u7406\u540e\u4f20\u5165BashOperator\uff1f"},"content":{"rendered":"\n<h3>\u4e00\u3001\u95ee\u9898<\/h3>\n\n\n\n<p>\u4e00\u4e2aDAG\u91cc\u7684bash task\u4efb\u52a1\uff0c\u6bcf\u5929\u4f1a\u591a\u6b21\u6267\u884c\uff0c\u5e76\u6bcf\u6b21\u6267\u884c\u4f20\u5165shell\u811a\u672c\u7684\u53c2\u6570\u76f8\u540c\uff0c\u5373\u6bcf\u5929\u91cd\u590d\u6267\u884c\u3002\u4f20\u5165\u7684\u53c2\u6570\u4e3a\u65f6\u95f4\u53c2\u6570\uff0c\u5177\u4f53\u662f\u524d\u4e00\u5929\u7684\u65f6\u95f4datetime\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4ec5\u4fdd\u8bc1\u4efb\u52a1\u6bcf\u6b21\u987a\u5229\u6267\u884c\u7684\u8bdd\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u53d6\u201c\u5f53\u524d\u65f6\u95f4\u201d\uff0c\u7136\u540e\u7ecf\u8fc7\u5904\u7406\u4f20\u7ed9BashOperator\uff0c\u4f46\u8fd9\u6837\u7684\u5b9e\u73b0\u5bf9\u4e8e\u91cd\u8dd1\u9694\u65e5\u7684\u4efb\u52a1\u6216\u4ee5\u5f80\u7684\u4efb\u52a1\u65f6\u5c31\u4e0d\u884c\u4e86\uff0c\u56e0\u4e3a\u65f6\u95f4\u8981\u6c42\u662f\u4efb\u52a1\u6267\u884c\u5468\u671f\u7684\u524d\u4e00\u65e5\uff0c\u59824\u670810\u91cd\u8dd14\u67085\u65e5\u7684\u4efb\u52a1\uff0c\u4f20\u5165\u7684\u65f6\u95f4\u53c2\u6570\u5e94\u8be5\u4e3a4\u67084\u65e5\uff0c\u4f46\u5982\u679c\u4ee3\u7801\u53d6\u5f53\u524d\u65f6\u95f4\uff0c\u5219\u6700\u7ec8\u4f20\u51654\u67089\uff0c\u663e\u7136\u4e0d\u7b26\u5408\u8981\u6c42\u3002\u6240\u4ee5\u9700\u8981\u901a\u8fc7airflow context\u7684\u5185\u7f6e\u53d8\u91cf\u6765\u5b9e\u73b0\uff0c\u5982<code>execution_date<\/code>\u3002\u6240\u4ee5\u6709\u4e86\u4e0b\u9762\u6700\u5148\u5199\u7684\uff08\u90e8\u5206\uff09\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">'''\n\u4efb\u52a1\u540c\u4e00\u5929\u9700\u8981\u68c0\u6d4b\u4e24\u6b21\uff0c\u4f46\u4e24\u6b21\u4efb\u52a1\u4f20\u5165\u7684\u65f6\u95f4\u53c2\u6570\u90fd\u5e94\u662f\u6628\u65e5\uff08\u540c\u4e00\uff09\u65f6\u95f4\uff0c\u4efb\u52a1\u5185\u5bb9\u5c31\u662f\u5bf9\u6628\u65e5\u6570\u636e\u8fdb\u884c\u68c0\u67e5\uff0c\u76f8\u5f53\u4e8e\u4e24\u6b21\u8fdb\u884c\u7684\u4efb\u52a1\u5185\u5bb9\u4e00\u6837\u3002\n\u5982\u679c\u5f53\u524d\u4e3a06:00\u4efb\u52a1\uff08execution_date=01:00 UTC \u524d\u4e00\u65e517:00\uff09\uff0c\u5219\u811a\u672c\u4f20\u53c2\u4e0a\u4e00\u6b21\u4efb\u52a1\u6267\u884c\u4f20\u5165\u7684execution_date,\u5373previous_execution_date_success\n'''\nutc_cron = \"0 17,22 * * *\"\n\nsh_date = \"{{ execution_date }}\"\nindex = sh_date.find('T') if sh_date.find(' ') == -1 else sh_date.find(' ')\nif sh_date[index + 1 : index + 6] == '17:00':\n    sh_date = \"{{ previous_execution_date_success }}\"\n\ncheck_cos_upload_logs = BashOperator(\n    task_id=\"check_cos_upload_logs\",\nbash_command='\/check_cos_upload_logs.sh ' + sh_date,\n    dag=dag\n)<\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u4ee3\u7801\u5927\u81f4\u542b\u4e49\uff1aUTC\u65f6\u95f4\uff0c\u6bcf\u592917:00\u548c22:00\u6267\u884c\u4efb\u52a1\u3002\u56e0\u4e3a\u6700\u7ec8\u65f6\u95f4\u53c2\u6570\u5e94\u8be5\u4e3a\u6628\u65e5\uff0c\u9996\u5148\u660e\u767d<code>execution_date<\/code>\u4e3a\u4e0a\u4e00\u6b21\u4efb\u52a1\u6267\u884c\u65f6\u95f4\uff01\uff01\u5982\u679c\u5f53\u65f6\u4efb\u52a1\u65f6\u95f417:00\uff0c\u90a3\u4e48<code>execution_date<\/code>\u4e3a\u6628\u65e522:00\uff0c\u6ee1\u8db3\u3002\u5982\u679c\u5f53\u65f6\u4efb\u52a1\u65f6\u95f422:00\uff0c\u90a3\u4e48<code>execution_date<\/code>\u4e3a\u5f53\u65e517:00\uff0c\u4e0d\u6ee1\u8db3\u6628\u65e5\u65f6\u95f4\u3002\u6240\u4ee5\u9700\u8981\u6839\u636e<code>execution_date<\/code>\u53bb\u5224\u65ad\uff0c\u5982\u679c\u662f22:00\uff0c\u5219\u4e0d\u5904\u7406\uff0c\u5982\u679c17:00\uff0c\u5219\u8bf4\u660e\u5f53\u524d\u4efb\u52a1\u662f22:00\u6267\u884c\u7684\u4efb\u52a1\uff0c\u6b64\u65f6\u9700\u8981\u53bb\u4e0a\u4e0a\u6b21\u4efb\u52a1\u6267\u884c\u65f6\u95f4\uff0c\u5373\u6628\u65e522:00\uff0c\u8fd9\u91cc\u53c2\u6570<code>previous_execution_date_success<\/code>\u3002<\/p>\n\n\n\n<p>\u6839\u636e\u4e0a\u9762\u7684\u9700\u6c42\uff0c\u6240\u4ee5\u60f3\u7740\u5728BashOperator\u524d\uff0c\u901a\u8fc7python\u903b\u8f91\u5904\u7406\u4e0b\uff0c\u4e8e\u662f\u6709\u4e86\u4e0a\u9762\u7684\u4ee3\u7801\u3002\u4f46\u662f\u53d1\u73b0\u4e0a\u9762\u4ee3\u7801\u7684\u7ed3\u679c\uff0c\u6700\u7ec8airflow\u6267\u884c\u6c38\u8fdc\u6267\u884c<code>\/check_cos_upload_logs.sh {{ execution_date }}<\/code>\uff0c\u6211\u539f\u672c\u4ee5\u4e3a\u5e94\u8be5\uff1a<br>\u5982\u679c\u5f53\u524d\u65f6\u95f4\u4e3a\u201c2021-04-19T22:00:00+00:00\u201d\uff0c\u5219<code>execution_date=2021-04-19T17:00:00+00:00<\/code>\uff0c\u90a3\u4e48<code>\/check_cos_upload_logs.sh 2021-04-18T22:00:00+00:00<\/code>\uff1b<br>\u5982\u679c\u5f53\u524d\u65f6\u95f4\u4e3a\u201c2021-04-19T17:00:00+00:00\u201d\uff0c\u5219<code>execution_date=2021-04-18T22:00:00+00:00<\/code>\uff0c\u90a3\u4e48<code>\/check_cos_upload_logs.sh 2021-04-18T22:00:00+00:00<\/code>\u3002<br>\u4f46\u5b9e\u9645\u4ee3\u7801\u5e76\u4e0d\u662f\u8fd9\u6837\u7684\u5904\u7406\uff01<\/p>\n\n\n\n<h3>\u4e8c\u3001\u539f\u56e0\u5206\u6790<\/h3>\n\n\n\n<p>\u5176\u5b9e\uff0c\u4e00\u5f00\u59cb\u5c31\u51fa\u73b0\u95ee\u9898\uff0c<code>sh_date = \"{{ execution_date }}\"<\/code>\uff0c\u6211\u5e0c\u671b\u5c06\u5185\u7f6e\u53d8\u91cfexecution_date\u53d6\u51fa\u8d4b\u503c\u7ed9sh_date\uff0c\u4f46\u5b9e\u9645\u5e76\u4e0d\u4f1a\uff0c\u662f\u5c06{{ execution_date }}\u8fd9\u6837\u7684\u5b57\u7b26\u4e32\u8d4b\u7ed9\u4e86sh_date\uff0c\u8fd9\u79cd\u53cc\u82b1\u62ec\u53f7\u7684\u8bed\u6cd5\u4e3a<strong>Jinja Template<\/strong>\uff0c\u6682\u53d1\u73b0airflow\u53ea\u5728operator\u7684command\u53c2\u6570\u4e2d\u4f1a\u89e3\u6790\u8be5\u6a21\u677f\u8bed\u8a00\uff0c\u5728\u5176\u4ed6\u5730\u65b9\uff08operator\u7684param\u4ee5\u53caon_failure_callback\u53c2\u6570\u4e5f\u4e0d\u4f1a\u89e3\u6790\uff09\u5e76\u4e0d\u4f1a\u89e3\u6790\uff0c\u6240\u4ee5\u4e0a\u9762\u4ee3\u7801\u4e2d\u7d27\u63a5\u7740\u7684\u903b\u8f91\u5224\u65ad\u5904\u7406\uff0c\u90fd\u662f\u5bf9{{ execution_date }}\u8fd9\u4e2a\u5b57\u7b26\u4e32\u7684\u5904\u7406\uff01\u5bfc\u81f4bash_command\u4e00\u76f4\u4e3a<code>\/check_cos_upload_logs.sh {{ execution_date }}<\/code>\u3002<\/p>\n\n\n\n<h3>\u4e09\u3001\u89e3\u51b3\u65b9\u6848<\/h3>\n\n\n\n<p>\u4eceairflow\u652f\u6301\u7684\u7b56\u7565\u4e2d\u4e86\u89e3\u5206\u6790<\/p>\n\n\n\n<p>\u53ef\u4ee5\u5f97\u5230\u5982\u4e0b\u4e24\u4e2a\u65b9\u5411<\/p>\n\n\n\n<h4>1\u3001\u5728\u4ee3\u7801\u89e3\u91ca\u6267\u884c\u4e2d\u8fdb\u884c\u65f6\u95f4\u903b\u8f91\u7684\u5904\u7406\uff0c\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u80fd\u591f\u53d6\u5230airflow context \u5185\u90e8\u53d8\u91cf\u3002<\/h4>\n\n\n\n<p>\u76ee\u524d\u4ec5PythonOperator\u53ef\u4ee5\u64cd\u4f5ccontext\u53d8\u91cf\uff0c\u4ee3\u7801\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u4e00\u4e2a\u51fd\u6570\u5904\u7406\u65f6\u95f4\uff0cPythonOperator\u83b7\u53d6execution_date\uff0c\u6267\u884c\u8be5\u51fd\u6570\uff0c\u5c06\u51fd\u6570\u8fd4\u56de\u503c\u901a\u8fc7xcom\u4f20\u9012\u7ed9\u540e\u7eedbash task\ndef get_sh_date_method(**context):\n    \"\"\"\n    \u4efb\u52a1\u540c\u4e00\u5929\u9700\u8981\u68c0\u6d4b\u4e24\u6b21\uff0c\u4f46\u4e24\u6b21\u4efb\u52a1\u4f20\u5165\u7684\u65f6\u95f4\u53c2\u6570\u90fd\u5e94\u662f\u6628\u65e5\uff08\u540c\u4e00\uff09\u65f6\u95f4\uff0c\u4efb\u52a1\u5185\u5bb9\u5c31\u662f\u5bf9\u6628\u65e5\u6570\u636e\u8fdb\u884c\u68c0\u67e5\uff0c\u76f8\u5f53\u4e8e\u4e24\u6b21\u8fdb\u884c\u7684\u4efb\u52a1\u5185\u5bb9\u4e00\u6837\u3002\n    \u5982\u679c\u5f53\u524d\u4e3a06:00\u4efb\u52a1\uff08execution_date=01:00 UTC \u524d\u4e00\u65e517:00\uff09\uff0c\u5219\u811a\u672c\u4f20\u53c2\u4e0a\u4e00\u6b21\u4efb\u52a1\u6267\u884c\u4f20\u5165\u7684execution_date,\u5373previous_execution_date_success\n    \"\"\"\n    sh_date = context['execution_date']\n    if sh_date.hour == 17:\n        # datetime.datetime\n        return str(context['prev_execution_date_success'])\n    return sh_date.to_datetime_string()\n\n\nget_sh_date = PythonOperator(\n    task_id='get_sh_date',\n    python_callable=get_sh_date_method,\n    provide_context=True,\n    dag=dag\n)<\/pre>\n\n\n\n<p>PythonOperator\u5728\u8c03\u7528<code>python_callable<\/code>\u7684\u65f6\u5019\uff0c\u4f1a\u5c06context\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728callable\u5bf9\u5e94\u51fd\u6570\u4e2d\uff0c\u4f7f\u7528context\u83b7\u53d6\u5185\u90e8\u53d8\u91cf\u3002\u524d\u63d0\u662f\uff0c\u5fc5\u987b<code>provide_context=True<\/code>\u3002<\/p>\n\n\n\n<p>\u51fd\u6570\u5185\u90e8\u53ef\u4ee5\u76f4\u63a5\u64cd\u4f5c\u5165\u53c2context\uff0c\u4ee5\u5b57\u5178\u65b9\u5f0f\u83b7\u53d6\u6307\u5b9akey\u7684\u53d8\u91cf\u503c\u3002\u5177\u4f53key\u53c2\u8003airflow package\u7684\u201cairflow\/models\/taskinstance.py\u201d<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">        return {\n            'conf': conf,\n            'dag': task.dag,\n            'ds': ds,\n            'next_ds': next_ds,\n            'next_ds_nodash': next_ds_nodash,\n            'prev_ds': prev_ds,\n            'prev_ds_nodash': prev_ds_nodash,\n            'ds_nodash': ds_nodash,\n            'ts': ts,\n            'ts_nodash': ts_nodash,\n            'ts_nodash_with_tz': ts_nodash_with_tz,\n            'yesterday_ds': yesterday_ds,\n            'yesterday_ds_nodash': yesterday_ds_nodash,\n            'tomorrow_ds': tomorrow_ds,\n            'tomorrow_ds_nodash': tomorrow_ds_nodash,\n            'END_DATE': ds,\n            'end_date': ds,\n            'dag_run': dag_run,\n            'run_id': run_id,\n            'execution_date': pendulum.instance(self.execution_date),\n            'prev_execution_date': prev_execution_date,\n            'prev_execution_date_success': lazy_object_proxy.Proxy(\n                lambda: self.previous_execution_date_success),\n            'prev_start_date_success': lazy_object_proxy.Proxy(lambda: self.previous_start_date_success),\n            'next_execution_date': next_execution_date,\n            'latest_date': ds,\n            'macros': macros,\n            'params': params,\n            'tables': tables,\n            'task': task,\n            'task_instance': self,\n            'ti': self,\n            'task_instance_key_str': ti_key_str,\n            'test_mode': self.test_mode,\n            'var': {\n                'value': VariableAccessor(),\n                'json': VariableJsonAccessor()\n            },\n            'inlets': task.inlets,\n            'outlets': task.outlets,\n        }\n<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7f57\u5217\u7684\u5c31\u662f\u5f53\u524dcontext\u652f\u6301\u5185\u90e8\u53d8\u91cfkey\u3002<code>context['execution_date']<\/code>\u5373\u53ef\u83b7\u53d6execution_date\uff0c\u548cJinja\u6a21\u677f<code>{{ execution_date }}<\/code>\u7b49\u540c\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff1a<\/p>\n\n\n\n<ul><li>context[&#8216;execution_date&#8217;]\u7684\u7ed3\u679c\u662fpendulum.datetime\u7684\u7c7b\u578b\uff0c\u800ccontext[&#8216;prev_execution_date_success&#8217;]\u7684\u7ed3\u679c\u662fdatetime.datetime\u7684\u7c7b\u578b\uff01<\/li><li>context[&#8216;prev_execution_date_success&#8217;]\u5bf9\u5e94TaskInstance\u4e2d\u7684previous_execution_date_success <\/li><\/ul>\n\n\n\n<p>\u6700\u540e\u5c31\u53ef\u4ee5\u5728\u8be5\u51fd\u6570\u5185\u8fdb\u884c\u6211\u7684\u65f6\u95f4\u5904\u7406\u903b\u8f91\u4e86\u3002<\/p>\n\n\n\n<p>\u63a5\u4e0b\u6765\u4f1a\u6709\u4e00\u4e2a\u65b0\u7684\u95ee\u9898\uff0c\u5982\u679c\u5c06PythonOperator\u5904\u7406\u540e\u7684\u7ed3\u679c\u7ed9\u5230BashOperator\u4f7f\u7528\u5462\uff1f\u8fd9\u91cc\u5c31\u8981\u901a\u8fc7<strong>xcom<\/strong>\u7684\u673a\u5236\u4e86\u3002<\/p>\n\n\n\n<p>xom\u5c31\u662f\u4e3a\u4e86\u89e3\u51b3task\u4e4b\u95f4\u4f20\u9012\u53c2\u6570\u51fa\u73b0\u7684\u3002xcom\u7684\u539f\u7406\u5176\u5b9e\u5c31\u662f\u901a\u8fc7\u4e00\u4e2aDB\u6765\u5b9e\u73b0\u5199\u5165K-V\u548c\u8bfb\u53d6K-V\u3002\u8be5K-V\u5173\u8054\u4e86DAG\u548ctask\u3002\u5f53PythonOperator\u6267\u884c\u4e86\u4e00\u4e2a\u6709\u8fd4\u56de\u503c\u7684call_back\u51fd\u6570\u65f6\uff0c\u5c31\u4f1axcom\u5199\u5165\uff08push\uff09\u4e00\u4e2aK-V\uff0c\u5982\u4e0b<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/file.blog.humh.cn\/2021\/04\/d2b5ca33bd970f64a6301fa75ae2eb22.png\"><img width=\"1884\" height=\"531\" class=\"wp-image-929 lazyload\" title=\"\u5982\u4f55\u83b7\u53d6Airflow\u5185\u90e8\u53d8\u91cf\u5e76\u7ecf\u8fc7\u903b\u8f91\u5904\u7406\u540e\u4f20\u5165BashOperator\uff1f\" src=\"data:image\/gif;base64,R0lGODlhAgABAIAAALGxsQAAACH5BAAAAAAALAAAAAACAAEAAAICBAoAOw==\" data-src=\"https:\/\/file.blog.humh.cn\/2021\/04\/d2b5ca33bd970f64a6301fa75ae2eb22.png\" alt=\"https:\/\/file.blog.humh.cn\/2021\/04\/d2b5ca33bd970f64a6301fa75ae2eb22.png\" data-srcset=\"https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2021\/04\/d2b5ca33bd970f64a6301fa75ae2eb22.png 1884w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2021\/04\/d2b5ca33bd970f64a6301fa75ae2eb22-300x85.png 300w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2021\/04\/d2b5ca33bd970f64a6301fa75ae2eb22-1024x289.png 1024w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2021\/04\/d2b5ca33bd970f64a6301fa75ae2eb22-768x216.png 768w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2021\/04\/d2b5ca33bd970f64a6301fa75ae2eb22-1536x433.png 1536w\" sizes=\"(max-width: 1884px) 100vw, 1884px\"><\/a><\/figure><\/div>\n\n\n\n<p>K\u5c31\u662f<code>return_value<\/code>\uff0cV\u5c31\u662f\u6211\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002<br>\u7136\u540e\u5728BashOperator\u4e2d\u5c31\u53ef\u4ee5\u901a\u8fc7Jinja\u6a21\u677f\u7684\u5f62\u5f0f\uff0c\u4ee5xom.pull\u83b7\u53d6K-V\u3002\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">check_kodo_upload_logs = BashOperator(task_id=\"check_kodo_upload_logs\", bash_command=\"\/check_kodo_upload_logs.sh \" + '{{ ti.xcom_pull(task_ids=\"get_sh_date\") }}', dag=dag)<\/pre>\n\n\n\n<p>\u5f53\u7136\u4e0a\u9762PythonOperator\u4e2d\u7684call_back\u5bf9\u5e94\u51fd\u6570\uff0c\u53ef\u4ee5\u65e0\u8fd4\u56de\u503c\uff0c\u5728\u51fd\u6570\u4f53\u4e2d\u901a\u8fc7<code>xcom.push<\/code>\u663e\u5f0f\u63d2\u5165\u4e00\u4e2aK-V\u4e5f\u662f\u53ef\u4ee5\u7684\u3002<\/p>\n\n\n\n<p>\u8fd8\u6709\u4f60\u53ef\u80fd\u6ce8\u610f\u5230\u4e86\uff0cxcom\u4e2d\u7684\u6570\u636e\u5728\u968f\u7740\u4efb\u52a1\u4e0d\u65ad\u6267\u884c\uff0c\u5728\u7d2f\u79ef\u3002\u8fd9\u6837\u53ef\u80fd\u968f\u65f6\u95f4\u63a8\u79fb\uff0c\u5360\u7528\u7a7a\u95f4\u3002xcom\u652f\u6301\u4f60\u5728\u6bcf\u6b21DAG\u8fd0\u884c\u6210\u529f\u540e\uff0c\u6e05\u9664\u5bf9\u5e94xom\u7684\u6570\u636e\uff01<\/p>\n\n\n\n<p>\u5177\u4f53\u5173\u4e8excom\u4ee5\u53capush\u548cpull\u5185\u5bb9\uff0c\u53ef\u4ee5\u53c2\u8003\u8fd9\u91cc<a href=\"https:\/\/kiwijia.work\/2020\/04\/06\/xcom\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"\uff08\u5728\u65b0\u7a97\u53e3\u6253\u5f00\uff09\">https:\/\/kiwijia.work\/2020\/04\/06\/xcom\/<\/a><\/p>\n\n\n\n<h4>2\u3001\u628a\u65f6\u95f4\u903b\u8f91\u5904\u7406\u8fc7\u7a0b\u653e\u5728Operator\u6267\u884c\u4e2d\u5904\u7406\uff0c\u5e76\u4e0d\u5728\u4ee3\u7801\u89e3\u91ca\u4e2d\u6267\u884c\u3002<\/h4>\n\n\n\n<p>\u56e0\u4e3aOperator\u672c\u8eab\u5bf9Jinja\u7684\u652f\u6301\uff0c\u800cJinja\u8bed\u8a00\u652f\u6301\u4e00\u5b9a\u7684\u903b\u8f91\u6027\u7f16\u5199\u548c\u65f6\u95f4\u51fd\u6570\u3002\u56e0\u4e3a\u6211\u89c9\u5f97\u6a21\u677f\u8bed\u8a00\u5199\u8d77\u6765\u9ebb\u70e6\uff0c\u6ca1\u5b9e\u8df5\uff0c\u5177\u4f53\u53ef\u4ee5\u81ea\u884c\u5ea6\u5a18Jinja\u7684\u5185\u5bb9\u3002\u53ef\u53c2\u8003\uff1a<a href=\"http:\/\/docs.jinkan.org\/docs\/jinja2\/templates.html\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"\uff08\u5728\u65b0\u7a97\u53e3\u6253\u5f00\uff09\">http:\/\/docs.jinkan.org\/docs\/jinja2\/templates.html<\/a><\/p>\n\n\n\n<h3>\u56db\u3001\u8865\u5145<\/h3>\n\n\n\n<h4>1\u3001airflow operator env\u53c2\u6570\uff0cbash_command\u53ef\u4ee5$\u53d6\u53c2\u3002<\/h4>\n\n\n\n<p><a href=\"https:\/\/airflow.apache.org\/docs\/apache-airflow\/stable\/_api\/airflow\/operators\/bash\/index.html?highlight=airflow%20operators%20bash#module-airflow.operators.bash\">https:\/\/airflow.apache.org\/docs\/apache-airflow\/stable\/_api\/airflow\/operators\/bash\/index.html?highlight=airflow%20operators%20bash#module-airflow.operators.bash<\/a><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">bash_task = BashOperator(\n    task_id=\"bash_task\",\n    bash_command='echo \"here is the message: '$message'\"',\n    env={'message': '{{ dag_run.conf[\"message\"] if dag_run else \"\" }}'},\n)<\/pre>\n\n\n\n<h4>2\u3001airflow UI\u4e0a\u6dfb\u52a0Variables\uff0c\u4ee3\u7801\u4e2d\u53ef\u4ee5Variables.get() api\u83b7\u53d6\u3002<\/h4>\n\n\n\n<p><a href=\"https:\/\/airflow.apache.org\/docs\/apache-airflow\/stable\/howto\/variable.html\">https:\/\/airflow.apache.org\/docs\/apache-airflow\/stable\/howto\/variable.html<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<p><strong>\u672c\u6587\u53c2\u8003<\/strong><\/p>\n\n\n\n<ul><li>\u201c\u6c14\u6d41\u4e2d\u7684execution_date\uff1a\u9700\u8981\u4ee5\u53d8\u91cf\u5f62\u5f0f\u8bbf\u95ee\u201d\uff1a<a rel=\"noreferrer noopener\" aria-label=\"https:\/\/www.itranslater.com\/qa\/details\/2583867886343291904\uff08\u5728\u65b0\u7a97\u53e3\u6253\u5f00\uff09\" href=\"https:\/\/www.itranslater.com\/qa\/details\/2583867886343291904\" target=\"_blank\">https:\/\/www.itranslater.com\/qa\/details\/2583867886343291904<\/a><\/li><li>\u201cAirflow\u4e2dPythonOperator\u53c2\u6570\u4f20\u9012\u65b9\u6cd5\u201d\uff1a<a rel=\"noreferrer noopener\" aria-label=\"\uff08\u5728\u65b0\u7a97\u53e3\u6253\u5f00\uff09\" href=\"http:\/\/bigbigben.com\/2019\/09\/20\/airflow-python-operator-variable\/\" target=\"_blank\">http:\/\/bigbigben.com\/2019\/09\/20\/airflow-python-operator-variable\/<\/a><\/li><li>\u201cAirflow\u4e2d\u5229\u7528xcom\u5b9e\u73b0task\u95f4\u4fe1\u606f\u4f20\u9012\u201d\uff1a<a rel=\"noreferrer noopener\" aria-label=\"\uff08\u5728\u65b0\u7a97\u53e3\u6253\u5f00\uff09\" href=\"https:\/\/kiwijia.work\/2020\/04\/06\/xcom\/\" target=\"_blank\">https:\/\/kiwijia.work\/2020\/04\/06\/xcom\/<\/a><\/li><li>\u201cAirflow\u6e90\u7801\u5206\u6790(2)-xcom\u90e8\u5206\u201d\uff1a<a rel=\"noreferrer noopener\" aria-label=\"\uff08\u5728\u65b0\u7a97\u53e3\u6253\u5f00\uff09\" href=\"http:\/\/chace.in\/2019\/09\/01\/Airflow%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90(2)-xcom%E9%83%A8%E5%88%86\/\" target=\"_blank\">http:\/\/chace.in\/2019\/09\/01\/Airflow%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90(2)-xcom%E9%83%A8%E5%88%86\/<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u95ee\u9898 \u4e00\u4e2aDAG\u91cc\u7684bash task\u4efb\u52a1\uff0c\u6bcf\u5929\u4f1a\u591a\u6b21\u6267\u884c\uff0c\u5e76\u6bcf\u6b21\u6267\u884c\u4f20\u5165shell\u811a\u672c\u7684\u53c2\u6570\u76f8\u540c\uff0c\u5373\u6bcf\u5929 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":930,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[11],"tags":[117,65],"_links":{"self":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts\/927"}],"collection":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=927"}],"version-history":[{"count":4,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts\/927\/revisions"}],"predecessor-version":[{"id":1021,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts\/927\/revisions\/1021"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/media\/930"}],"wp:attachment":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}