Python 如何使用“创建表”;按“分类”;使用BigQueryCreateEmptyTableOperator的列?

Python 如何使用“创建表”;按“分类”;使用BigQueryCreateEmptyTableOperator的列?,python,google-bigquery,airflow,Python,Google Bigquery,Airflow,我试图在使用python脚本的GCP composer任务中,使用带有cluster_by column的BigQueryCreateEmptyTableOperator创建一个表。我正在使用“cluster_fields”创建列,但它不起作用。请告诉我正确的方法是什么 下面是我正在使用的代码 stop_op = BigQueryCreateEmptyTableOperator( task_id='BigQuery_CreateTable', dataset_id

我试图在使用python脚本的GCP composer任务中,使用带有cluster_by column的BigQueryCreateEmptyTableOperator创建一个表。我正在使用“cluster_fields”创建列,但它不起作用。请告诉我正确的方法是什么

下面是我正在使用的代码

stop_op = BigQueryCreateEmptyTableOperator(
        task_id='BigQuery_CreateTable',
        dataset_id=dataset_nm,
        table_id=table_nm,
        project_id=project_nm,
        schema_fields=[{"name": "emp_name", "type": "STRING", "mode": "REQUIRED"},
                       {"name": "salary", "type": "INTEGER", "mode": "NULLABLE"},
                       {"name": "distribution_name", "type": "STRING", "mode": "NULLABLE"},
                       {"name": "transaction_date", "type": "DATE", "mode": "NULLABLE"}],
    time_partitioning={'type': 'DAY', 'field': 'transaction_date'},
    cluster_fields='distribution_name',
    bigquery_conn_id='bigquery_default',
    google_cloud_storage_conn_id='bigquery_default',
    autodetect=True,
    dag=dag
)

目前,最新版本的Airflow(撰写此答案时为1.10.5)中没有此功能

但是,您可以使用下面的“创建新运算符”并使用该运算符


从afflow.utils.decorators导入应用默认值
从afflow.contrib.hooks.bigquery\u hook导入BigQueryHook
从airflow.contrib.hooks.gcs\u hook导入GoogleCloudStorageHook,\u parse\u gcs\u url
从afflow.contrib.operators.bigquery_操作符导入BigQueryCreateEmptyTableOperator
导入json
类BQCreateEmptyTableWithClusteredFieldsOp(BigQueryCreateEmptyTableOperator):
模板字段=('dataset\u id'、'table\u id'、'project\u id',
“gcs\u架构\u对象”、“标签”)
ui_color='#f0eee4'
#pylint:disable=参数太多
@应用默认值
定义初始化(自我,
数据集\u id,
表1-1,
项目id=无,
schema_fields=None,
gcs_模式_对象=无,
时间=无,
bigquery\u conn\u id='bigquery\u default',
google_cloud_storage_conn_id='google_cloud_default',
委托给=无,
标签=无,
加密配置=无,
集群_字段=无,
*args,**kwargs):
super(BigQueryCreateEmptyTableOperator,self)。\uuuuuuu init\uuuu(*args,**kwargs)
self.project\u id=项目\u id
self.dataset\u id=数据集\u id
self.table\u id=table\u id
self.schema\u字段=schema\u字段
self.gcs\u schema\u object=gcs\u schema\u object
self.bigquery\u conn\u id=bigquery\u conn\u id
self.google\u cloud\u storage\u conn\u id=google\u cloud\u storage\u conn\u id
self.delegate\u to=委托给
self.time\u partitioning={}如果time\u partitioning不是其他的time\u partitioning
self.labels=标签
self.encryption\u configuration=加密\u configuration
self.cluster\u字段=cluster\u字段或[]
def执行(自身、上下文):
bq\u hook=BigQueryHook(bigquery\u conn\u id=self.bigquery\u conn\u id,
委托给=自我。委托给)
如果不是self.schema_字段和self.gcs_schema_对象:
gcs\u bucket,gcs\u object=\u parse\u gcs\u url(self.gcs\u schema\u object)
gcs_hook=GoogleCloudStorageHook(
google\u cloud\u storage\u conn\u id=self.google\u cloud\u storage\u conn\u id,
委托给=自我。委托给)
schema_fields=json.loads(gcs_hook.download(
地面军事系统,
gcs_对象)。解码(“utf-8”))
其他:
schema\u字段=self.schema\u字段
conn=bq\u hook.get\u conn()
游标=连接游标()
cursor.create\u空表(
project\u id=self.project\u id,
dataset\u id=self.dataset\u id,
table_id=self.table_id,
模式\字段=模式\字段,
时间分区=self.time分区,
标签=自我标签,
cluster\u字段=self.cluster\u字段,
加密\u配置=自加密\u配置
)
现在,您可以按如下方式使用它:

stop\u op=BQCreateEmptyTableWithClusteredFieldsOp(
task\u id='BigQuery\u CreateTable',
数据集\u id=数据集\u nm,
表号=表号,
项目编号=项目编号,
schema_fields=[{“name”:“emp_name”,“type”:“STRING”,“mode”:“REQUIRED”},
{“name”:“salary”,“type”:“INTEGER”,“mode”:“NULLABLE”},
{“name”:“distribution_name”,“type”:“STRING”,“mode”:“NULLABLE”},
{“name”:“transaction_date”,“type”:“date”,“mode”:“NULLABLE”}],
时间分区={'type':'DAY','field':'transaction\u date'},
集群_fields='distribution_name',
bigquery\u conn\u id='bigquery\u default',
google\u cloud\u storage\u conn\u id='bigquery\u default',
自动检测=真,
dag=dag
)

感谢您的解决方案,当我使用BQCreateEmptyTableWithClusteredFieldsOp类时,我收到错误:没有名为“airflow.gcp”的模块。我正在使用以下导入…从aiffort.utils.decorators导入应用默认值从aiffort.contrib.hooks.bigquery\u hook导入BigQueryHook从aiffort.contrib.hooks.gcs\u hook导入GoogleCloudStorageHook从aiffort.gcp.hooks.gcs导入解析\u gcs\u url从airflow.contrib.operators.bigquery\运算符导入BigQueryCreateEmptyTableOperator导入JS检查更新的答案,我已经在代码中添加了导入从aiffair.utils.decorators导入aiffair.contrib.hooks.bigquery\u hook导入aiffair.contrib.hooks.gcs\u hook导入GoogleCloudStorageHook中的默认值,从aiffair.contrib.operators.bigquery\u操作符导入BigQueryCreateEmptyTableOperator导入json``谢谢,我尝试过了,但错误仍然存在。错误-create_empty_table()得到一个意外的关键字参数“cluster_fields”。您可能正在使用旧版本的Airflow<代码>群集字段可用于气流1.10.5中的
创建空表()。链接: