气流xcom返回最后一个带逗号的值

2024-04-20 07:32:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚刚尝试了xcom拉式打印任务的返回值

基本上,我运行了一个SQL任务select 123123123,它将返回123123123。我的狗是

<<args and DAG details goes here>>

def puller(**kwargs):
    ti = kwargs['ti']
    pulled_value_1 = ti.xcom_pull(task_ids='push_result')
    print("VALUE IN PULLER : ", pulled_value_1)

def get_dag_ids(**kwargs):
    postgres_hook = PostgresHook(postgres_conn_id='cloudsqlpg')
    records = postgres_hook.get_records(sql='select 123123123')
    return records

t1 = PythonOperator(
    task_id="push_result",
    python_callable=get_dag_ids,
    provide_context=True,
    dag=dag
)

pull = PythonOperator(
    task_id='pullee',
    dag=dag,
    python_callable=puller,
    provide_context=True,
)


t1 >> pull

我从任务t1中得到如下输出

INFO - Done. Returned value was: [(123123123,)]

我只需要正确的值123123。它是一个数组吗?如何从该结果中提取正确的值


Tags: ididstaskgetvaluedeftipostgres
1条回答
网友
1楼 · 发布于 2024-04-20 07:32:10

您可能希望将其解压缩到单个列表/元组中。 [123123…,n]

list(itertools.chain(*records)) 

tuple(itertools.chain(*records)) 

这样就更容易访问每个结果,即记录[0]

相关问题 更多 >