Google bigquery 气流BigQueryInsertJobOperator配置

Google bigquery 气流BigQueryInsertJobOperator配置,google-bigquery,airflow,Google Bigquery,Airflow,我在将不推荐使用的BigQueryOperator转换为BigQueryInsertJobOperator时遇到一些问题。我有以下任务: bq_extract = BigQueryInsertJobOperator( dag="big_query_task, task_id='bq_query', gcp_conn_id='google_cloud_default', params={'data': Utils().querycontext},

我在将不推荐使用的
BigQueryOperator
转换为
BigQueryInsertJobOperator
时遇到一些问题。我有以下任务:

bq_extract = BigQueryInsertJobOperator(
    dag="big_query_task,
    task_id='bq_query',
    gcp_conn_id='google_cloud_default',
    params={'data': Utils().querycontext},
    configuration={
        "query": {"query": "{% include 'sql/bigquery.sql' %}", "useLegacySql": False,
                  "writeDisposition": "WRITE_TRUNCATE", "destinationTable": {"datasetId": bq_dataset}}
    })
my bigquery_extract.sql查询中的此行引发错误:

{% for field in data.bq_fields %}
我想使用params中的
'data'
,它正在调用一个方法,该方法正在从.json文件读取:

class Utils():
    bucket = Variable.get('s3_bucket')
    _qcontext = None

    @property
    def querycontext(self):
        if self._qcontext is None:
            self.load_querycontext()

        return self._qcontext

    def load_querycontext(self):
        with open(path.join(conf.get("core", "dags"), 'traffic/bq_query.json')) as f:
            self._qcontext = json.load(f)
bq_query.json
就是这种格式,我需要使用嵌套的
bq_字段
列表值:

{
"bq_fields": [
    { "name": "CONCAT(ID, '-', CAST(ID AS STRING), "alias": "new_id" },
    { "name": "TIMESTAMP(CAST(visitStartTime * 1000 AS INT64)", "alias": "new_timestamp" },
    { "name": "TO_JSON_STRING(hits.experiment)", "alias": "hit_experiment" }]
}
此文件有一个列表,我想在上面提到的查询行中使用该列表,但它会引发以下错误:

jinja2.exceptions.UndefinedError:“数据”未定义


您的代码有两个问题

BigQueryInsertJobOperator中不支持第一个“params”字段。请参阅本文,其中我介绍了如何在使用BigQueryInsertJobOperator时将参数传递到sql文件

其次,如果您碰巧遇到找不到文件的错误,请确保设置了文件的完整路径。在从本地测试迁移到云端时,即使文件位于同一目录中,我也必须这样做。您可以使用下面的示例在dag配置中设置路径(将路径替换为您的路径):

 with DAG(
    ...
    template_searchpath = '/opt/airflow/dags',
    ...
    
) as dag: