我需要访问sql文件中BigqueryOperator传递的参数,但是ERROR - queryParameters argument must have a type <class 'dict'> not <class 'list'>
我在使用以下代码时遇到错误:
t2 = bigquery_operator.BigQueryOperator(
task_id='bq_from_source_to_clean',
sql='prepare.sql',
use_legacy_sql=False,
allow_large_results=True,
query_params=[{ 'name': 'threshold_date', 'parameterType': { 'type': 'STRING' },'parameterValue': { 'value': '2020-01-01' } }],
destination_dataset_table="{}.{}.{}".format('xxxx',
'xxxx',
'temp_airflow_test'),
create_disposition="CREATE_IF_NEEDED",
write_disposition="WRITE_TRUNCATE",
dag=dag
)
SQL:
select cast(DATE_ADD(a.dt_2, interval 7 day) as DATE) as dt_1
,a.dt_2
,cast('2010-01-01' as DATE) as dt_3
from (select cast(@threshold_date as date) as dt_2) a
我正在使用Google composer版本composer-1.7.0-airflow-1.10.2
提前致谢。
深入研究源代码后,似乎BigQueryHook
在Airflow 1.10.3中修复了一个错误。
您定义的方式query_params
对于较新版本的Airflow是正确的,并且应该是根据BigQuery API列出的清单:请参见https://cloud.google.com/bigquery/docs/parameterized-queries#bigquery_query_params_named-python。
无论如何,您都会收到此错误,因为在Airflow 1.10.2中,该错误query_params
定义为dict
,请参见:
https://github.com/apache/airflow/blob/1.10.2/airflow/contrib/hooks/bigquery_hook.py#L678
query_param_list = [
...
(query_params, 'queryParameters', None, dict),
...
]
这导致内部_validate_value
函数抛出TypeError
:
https://github.com/apache/airflow/blob/1.10.2/airflow/contrib/hooks/bigquery_hook.py#L1954
def _validate_value(key, value, expected_type):
""" function to check expected type and raise
error if type is not correct """
if not isinstance(value, expected_type):
raise TypeError("{} argument must have a type {} not {}".format(
key, expected_type, type(value)))
我没有query_params
在Airflow 1.10.2中找到任何示例(或任何单元测试...),但是我认为这仅仅是因为它不可用。
这些错误已通过以下提交修复:
dict
为list
这些更改已嵌入到Airflow 1.10.3中,但是到目前为止,Composer中不提供Airflow 1.10.3(https://cloud.google.com/composer/docs/concepts/versioning/composer-versions#new_environments):最新版本已于2019年5月16日发布并嵌入版本1.10.2。
等待这个新版本,我看到2种方法来解决您的问题:
BigQueryOperator
然后BigQueryHook
将其嵌入您的源中以使用它们,或者扩展现有的BigQueryHook
方法并覆盖错误的方法。我不确定您可以BigQueryHook
直接打补丁(在Composer环境中无法访问这些文件)query_params
)本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句