【问题标题】:How to use bulk_load in apache airflow如何在apache气流中使用bulk_load
【发布时间】:2021-12-13 19:07:03
【问题描述】:

我有 apache airflow 2.1.4 和 postgres 数据库。 我需要一次插入多行。所以我打算使用 PostgresHook 的 bulk_load 方法,但每次都会出错。

data = pd.read_csv(open(filepath, 'rb'))
buffer = StringIO()
buffer.write(data.to_csv(index=None, header=None, sep='\t'))
buffer.seek(0)
schema_table = 'schema.table'
with PostgresHook(postgres_conn_id='my_pg_database'):
    PostgresHook.bulk_load(table=schema_table, tmp_file=buffer)

我得到的错误:

Traceback (most recent call last):
File "/home/airflow/dags/my_python_file.py", line 76, in <module>
my_func(filepath=my_file, target_schema=schema, target_table=table)
File "/home/airflow/dags/my_python_file.py", line 39, in my_func
with PostgresHook(postgres_conn_id='my_pg_database'):
AttributeError: __enter__

我什至找不到一些使用 bulk_load 的例子。将appriciate任何线索。谢谢。

【问题讨论】:

标签: python-3.x airflow bulkinsert bulk-load


【解决方案1】:

Postgres Hook(以及任何其他真正的钩子)不是“上下文管理器”。您不能使用with: 来使用它们。

类似的东西应该可以工作:

postgres_hook = PostgresHook(postgres_conn_id='my_pg_database')
postgres_hook.bulk_load(...)

【讨论】:

  • 谢谢。但现在我得到: Traceback(最近一次调用最后一次):文件“/home/airflow/dags/my_python_file.py”,第 76 行,在 my_func(filepath=my_file, target_schema=schema, target_table=table) File 中/home/airflow/dags/my_python_file.py",第 45 行,在 kplus_to_marts PostgresHook.bulk_load(table=schema_table, tmp_file=buffer) TypeError: bulk_load() missing 1 required positional argument: 'self'
  • 请阅读示例 - 它与您的不同。将东西作为对象方法而不是类方法运行是非常基本的python事情。当一些基本的 Python 知识可以回答时,我害怕在 Stack Overflow 上提问,我认为学习 Python 的方式不好
猜你喜欢
  • 2019-11-15
  • 2021-04-01
  • 1970-01-01
  • 2021-10-07
  • 2021-12-09
  • 2021-09-20
  • 1970-01-01
  • 2020-07-04
  • 1970-01-01
相关资源
最近更新 更多