简体   繁体   English

将 xcom 值传递给 Airflow 中的 JiraOperator

[英]Passing xcom value to JiraOperator in Airflow

def f_get_value_via_xcom(**context):
    var = context['ti'].xcom_pull(dag_id='bash_execute', key='return_value')  
    
bash_execute = BashOperator(
    task_id='bash_execute',
    bash_command='some bash command'
    xcom_push=True,
    priority_weight=100,
    start_date = datetime(2021, 1, 21))
    
def setUp(self):
    configuration.load_test_config()
    args = {
         'owner': 'airflow',
         'start_date': DEFAULT_DATE
    }
dag = DAG(dag_id='test_jira_operator',
          default_args=default_args,
          schedule_interval='15 9 * * *',
          dagrun_timeout=timedelta(seconds=120))
db.merge_conn(
    Connection(
        conn_id='jira username', conn_type='jira',
        host='jira server', port=443,
        extra='{"verify": "False", "project": "AIRFLOW"}'))          
        
        
jira_ticket_search_operator = JiraOperator(task_id='test_jira_operator',
               jira_conn_id='airflow_jira',
                                           jira_method="create_issue",
                                           python_callable=f_get_value_via_xcom,
                                           provide_context=True,
                                           jira_method_args={
                                                   'project': {'id': 10000},
                                                   'summary': 'New test issue',
                                                   'description': "{{ ti.xcom_pull('php_thelia') }}",
                                                   'issuetype': {'name': 'Bug'},
                                           },
                                           dag=dag)         
                                                  
                                                   
bash_execute >> jira_ticket_search_operator

I'm trying to make a DAG where it creates a new Jira task with the description being the console output from a bash command but i'm getting the following error我正在尝试创建一个 DAG,它在其中创建一个新的 Jira 任务,其描述是来自 bash 命令的控制台 output 但我收到以下错误

*[2021-01-22 12:57:28,109] {taskinstance.py:1152} ERROR - Object of type 'Issue' is not JSON serializable*
                 

and i don't understand why.我不明白为什么。 I have posted above my current airflow script.Could you please help me.我已经在我当前的 airflow 脚本上方发布了。请你帮帮我。

This is because from Airflow 2.0, the default value for [core] enable_xcom_pickling has been changed to False for security reasons.这是因为从 Airflow 2.0 开始,出于安全原因, [core] enable_xcom_pickling的默认值已更改为False

The pickle type for XCom messages has been replaced to JSON by default to prevent RCE attacks. XCom 消息的 pickle 类型已默认替换为 JSON 以防止 RCE 攻击。 Note that JSON serialization is stricter than pickling, so for example if you want to pass raw bytes through XCom you must encode them using an encoding like base64.请注意,JSON 序列化比酸洗更严格,因此例如,如果您想通过 XCom 传递原始字节,则必须使用 base64 之类的编码对其进行编码。 If you understand the risk and still want to use pickling, set enable_xcom_pickling = True in your Airflow config's core section.如果您了解风险并仍想使用酸洗,请在 Airflow 配置的核心部分中设置enable_xcom_pickling = True

Like it is mentioned in the docs, if you are happy with the risk and know that no one is going to infiltrate your environment, you can set enable_xcom_pickling = True to use Pickling to store XComs instead of JSON.就像文档中提到的那样,如果您对风险感到满意并且知道没有人会渗透到您的环境中,您可以设置enable_xcom_pickling = True以使用 Pickling 来存储 XComs 而不是 JSON。 And then you won't get that error.然后你不会得到那个错误。 JSON serialization does not support arbitrary objects -- which is why Object of type Issue is not supported. JSON 序列化不支持任意对象——这就是不支持Object of type Issue原因。

https://github.com/apache/airflow/blob/master/UPDATING.md#the-default-value-for-core-enable_xcom_pickling-has-been-changed-to-false https://github.com/apache/airflow/blob/master/UPDATING.md#the-default-value-for-core-enable_xcom_pickling-has-been-changed-to-false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM