第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用 Python 的 BigQuery 動(dòng)態(tài) SQL

使用 Python 的 BigQuery 動(dòng)態(tài) SQL

汪汪一只貓 2023-04-18 17:43:20
最近 GCP BQ 支持動(dòng)態(tài) SQL。我想用 Cloud Functions 試試這個(gè)。我的 BQ 動(dòng)態(tài) SQL(在 UI 上運(yùn)行)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 代碼傳遞值,但問題是,它會(huì)被 Python BQ lib 支持嗎?任何示例python代碼?我試過這些代碼,但沒有運(yùn)氣代碼 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))錯(cuò)誤:, 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 bigquerydef 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))錯(cuò)誤:line 130, in result raise self._exception google.api_core.exceptions.BadRequest: 400 Syntax error: Illegal input character "%" at [3:104]
查看完整描述

2 回答

?
嗶嗶one

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊

最后,我找到了正確的語(yǔ)法。還注意到我的代碼中存在一些錯(cuò)誤。

錯(cuò)誤:

  1. sql=('''.......''')SQL 變量 ( )中的 SQL 查詢語(yǔ)法錯(cuò)誤

  2. 最后一行的打印語(yǔ)句是錯(cuò)誤的,我的選擇查詢沒有url and view_count列。

  3. 在動(dòng)態(tài) SQL 中,我們必須在 where 條件中傳遞單引號(hào)(如果它是一個(gè)字符串)

示例工作代碼:

代碼 1:

from google.cloud import bigquery


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 %s from `manan.tbla` \"\"\",cols)".format(table_name)


print(sql)


query_job = client.query(sql, job_config=job_config)


results = query_job.result()

for row in results:

        print(row)

代碼 2:

from google.cloud import bigquery


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 ? from `my_db.tbla` \"\"\") using cols".format(table_name)


print(sql)


query_job = client.query(sql, job_config=job_config)


results = query_job.result()

for row in results:

        print(row)

代碼 3:

from google.cloud import bigquery


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 as col".format(table_name)


print(sql)


query_job = client.query(sql, job_config=job_config)


results = query_job.result()

for row in results:

        print(row)


查看完整回答
反對(duì) 回復(fù) 2023-04-18
?
慕姐4208626

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超7個(gè)贊

Jinja2 SQL 模板是構(gòu)建動(dòng)態(tài) SQL 的更好選擇。例子:


create or replace table {{ params.targetTable }}

as

select

    {{ params.targetColumnList|join(',') }},

    cast(null as timestamp) as begin_timestamp,

    cast(null as timestamp) as end_timestamp

from

    {{ params.sourceTable }};


查看完整回答
反對(duì) 回復(fù) 2023-04-18
  • 2 回答
  • 0 關(guān)注
  • 229 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)