我正在编写一个气流任务来读取大型 csv 并将其保存到 postgresql 数据库。
我发现这个 asyncpg 包具有复制功能,其运行速度比任何其他包都要快得多。然而,它是异步的,我不知道如何将它合并到Airflow中。
这是示例代码:
from airflow import DAG
from airflow.operators.python_operator import PythonOperator
from datetime import datetime, timedelta
from pandas import DataFrame
import asyncpg
async def to_sql(dataframe, table_name, schema_name='public', timeout=None, truncate=False):
connection = await asyncpg.connect(user='postgres', host='host.docker.internal', database='quantaxis', password='123456')
result = await connection.copy_records_to_table(
table_name,
records=dataframe.values.tolist(),
columns=shared_columns,
schema_name=schema_name,
timeout=timeout)
await connection.close()
return result
default_args = {
'owner': 'Airflow',
'depends_on_past': False,
'start_date': datetime(2020, 1, 1),
'retries': 1,
'retry_delay': timedelta(minutes=1),
}
dag = DAG('pythonexp2123', default_args=default_args, schedule_interval=timedelta(days=1))
async def save_file_to_database(ds):
df = pd.read_csv("data{0}.csv".format(ds))
r = await to_sql(df, 'test')
return r
t1 = PythonOperator(
task_id='pushing_task',
provide_context=True,
python_callable=save_file_to_database,
dag=dag
)
t1
当我运行它时,它会返回错误:
Can't Pickle Object <Corountine>
我怎样才能改变这个函数来让这个 Dag 工作?我仍然想使用 asyncpg 包,因为它的速度。