最近 GCP BQ 支持动态 SQL。我想用 Cloud Functions 试试这个。
我的 BQ 动态 SQL(在 UI 上运行)
declare cols string;
set cols=(select STRING_AGG (column_name,',') from `my_db.INFORMATION_SCHEMA.COLUMNS` where table_name='tbla');
EXECUTE IMMEDIATE format("""select %s from `my_db.tbla`""",cols);
我想table_name从我的 python 代码传递值,但问题是,它会被 Python BQ lib 支持吗?
任何示例python代码?
我试过这些代码,但没有运气
代码 1:
def hello_gcs(event, context):
table_name='tbla'
client = bigquery.Client()
job_config = bigquery.QueryJobConfig(use_legacy_sql=False)
sql=( '''
declare cols string;
set cols=(select STRING_AGG (column_name,',') from `my_db.INFORMATION_SCHEMA.COLUMNS` where table_name=?);
EXECUTE IMMEDIATE format("""select @ col from `my_db.tbla`""") using cols
''',(table_name))
query_job = client.query(sql, job_config=job_config)
results = query_job.result()
for row in results:
print("{} : {} views".format(row.url, row.view_count))
错误:
, line 130, in result raise self._exception google.api_core.exceptions.BadRequest: 400 Query error: Positional parameters are not supported at [3:104]
from google.cloud import bigquery
def hello_gcs(event, context):
table_name='tbla'
client = bigquery.Client()
job_config = bigquery.QueryJobConfig(use_legacy_sql=False)
sql=( '''
declare cols string;
set cols=(select STRING_AGG (column_name,',') from `my_db.INFORMATION_SCHEMA.COLUMNS` where table_name=%s);
EXECUTE IMMEDIATE format("""select @ col from `my_db.tbla`""") using cols
''',(table_name))
query_job = client.query(sql, job_config=job_config)
results = query_job.result()
for row in results:
print("{} : {} views".format(row.url, row.view_count))
错误:
line 130, in result raise self._exception google.api_core.exceptions.BadRequest: 400 Syntax error: Illegal input character "%" at [3:104]
哔哔one
慕姐4208626
相关分类