2017-07-19 37 views
1

我想通過Python腳本在Bigquery中查詢表。不過,我已經將查詢寫爲標準的sql查詢。爲此,我需要使用'#standardsql'開始我的查詢。但是,當我這樣做,然後評論我的查詢的其餘部分。我試圖用多行編寫查詢,但它不允許我這樣做。有沒有人處理過這樣的問題,並找出解決方案?下面是我的第一個代碼,其中的查詢被註釋掉了。無法在python中運行standardQuery查詢到bigquery

client = bigquery.Client('dataworks-356fa') 

query = ("#standardsql SELECT count(distinct serial) FROM `dataworks-356fa.FirebaseArchive.test2` Where (PeripheralType = 1 or PeripheralType = 2 or PeripheralType = 12) AND EXTRACT(WEEK FROM createdAt) = EXTRACT(WEEK FROM CURRENT_TIMESTAMP()) - 1 AND serial != 'null'") 

dataset = client.dataset('FirebaseArchive') 
table = dataset.table('test2') 
tbl = dataset.table('Count_BB_Serial_weekly') 
job = client.run_async_query(str(uuid.uuid4()), query) 
job.destination = tbl 
job.write_disposition= 'WRITE_TRUNCATE' 
job.begin() 

當我嘗試寫這樣蟒蛇查詢第二行查詢不讀什麼過去。

query = ("#standardsql 
     SELECT count(distinct serial) FROM `dataworks-356fa.FirebaseArchive.test2` Where (PeripheralType = 1 or PeripheralType = 2 or PeripheralType = 12) AND EXTRACT(WEEK FROM createdAt) = EXTRACT(WEEK FROM CURRENT_TIMESTAMP()) - 1 AND serial != 'null'") 

查詢正在運行會選擇上週內已生成的值。如果使用standardql不需要這種變化,我會願意切換其他查詢,但我一直無法弄清楚如何做到這一點。我寧願這是最後的手段。感謝您的幫助!

回答

2

如果你想你會使用標準的SQL查詢本身內的標誌,你可以建造它想:

query = """#standardSQL 
      SELECT count(distinct serial) FROM `dataworks-356fa.FirebaseArchive.test2` Where (PeripheralType = 1 or PeripheralType = 2 or PeripheralType = 12) AND EXTRACT(WEEK FROM createdAt) = EXTRACT(WEEK FROM CURRENT_TIMESTAMP()) - 1 AND serial != 'null' 
     """ 

可以使用,以及另一種選擇是設置創建job的財產到False,類似於:

job = client.run_async_query(job_name, query) 
job.use_legacy_sql = False # -->this also makes the API use Standard SQL 
job.begin() 
+0

謝謝我使用了第二部分,它的工作! –