我my_table在 BigQuery 中有一個在每個月的第一天填充的。資料如下所示:
date rate
01/01/22 1.5
01/02/22 1.4
...
01/31/22 1.7
2022 年 3 月 1 日,我將獲得 2 月份的資料(每天)。但是,雖然 2 月的資料不可用,但我需要每天使用 current_date 和 null 追加行rate,然后在 3 月 1 日資料可用時覆寫它。期望的輸出是:
date rate
01/01/22 1.5
01/02/22 1.4
...
01/31/22 1.7
02/01/22 null
02/02/22 null
...
02/17/22 null
uj5u.com熱心網友回復:
您可以使用以下邏輯來自動化您的流程:
在每個月的第一天,檢查是否有新檔案到達。如果它在那里,將其內容添加到單獨的表中(呼叫它
monthly_table)。每天運行一個查詢,該查詢將來自
monthly_tableand的輸出與合并的輸出相結合my_table并覆寫。my_table
對于 (#1),您可以運行一個命令,將檔案中的資料(假設它是 CSV)加載到monthly_table. 這可以使用bq命令列實用程式完成,帶有--noreplace標志以確保始終附加新資料:
bq load \
--source_format=CSV \
--skip_leading_rows=2 \
--noreplace \
mydataset.monthly_table \
gs://mybucket/data_for_2022_02_01.csv \
'[{"name": "date", "type": "STRING"}, {"name": "rate", "type": "FLOAT64"}]'
對于(#2),您可以每天運行一個合并兩個表的查詢,然后my_table用輸出覆寫:
select date, max(rate) as rate from (
select date, rate from mydataset.monthly_table
union all
select date, rate from mydataset.my_table
union all
select format_date('%m/%d/%Y', current_date()) as date, null as rate
)
group by date
order by date
您還可以使用bq實用程式運行第二個查詢(帶有--replace=true)標志。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/429348.html
