【发布时间】:2021-02-10 14:37:28
【问题描述】:
我想用 Airflow 创建一个 BigQuery 表(通过 bigquery 挂钩或 bigquery 空表创建器)。不幸的是,不能使用 Range 分区创建。
Someone raised a PR,但是为了最小化气流操作界面,他们建议跳过它。他们提供了这样的解决方法。
you can use the table_resource argument to pass any table definition you want, no need to specify every single parameter.
我不清楚,在气流中如何使用 Range 分区相关的 JSON。有人可以给我一个如何使用/实现它的例子吗?
我尝试了下面的东西,但是它创建了没有分区的表。(它的时间分区,但BQ运算符有一个参数,但我想试试table_resource。
resource="""{
"TimePartitioning": {
"type": "DAY",
"field": "created_at"
}
}"""
schema="""[{"mode": "REQUIRED", "name": "code", "type": "STRING"},
{"mode": "REQUIRED", "name": "created_at", "type": "DATETIME"},
{"mode": "REQUIRED", "name": "service_name", "type": "STRING"}]"""
def bq_create(**kwargs):
table_schema = 'bhuvi'
table_name = 'sampletable'
create = BigQueryCreateEmptyTableOperator (
task_id='create_bq_{}'.format(table_name),
project_id = 'myproject',
dataset_id=table_schema,
table_id=table_name,
schema_fields=json.loads(schema),
bigquery_conn_id='bigquery_default',
table_resource =json.loads(resource)
)
create.execute(context=kwargs)
bqcreate = PythonOperator(
task_id="bqcreate",
python_callable=bq_create,
provide_context=True,
dag=dag
)
bqcreate
【问题讨论】:
标签: python google-bigquery airflow