echo Give yearmonth "yyyyMM"
setlocal enabledelayedexpansion
SET /p yearmonth=
SET ClientName[0]=abc
SET ClientName[1]=def
SET i = 0
:myLoop
if defined ClientName[%i%] (
call bq query --use_legacy_sql=false "CREATE EXTERNAL TABLE `test.!ClientName[%%i]!.%yearmonth%` OPTIONS (format = 'CSV',skip_leading_rows = 1 uris = ['gs://test/!ClientName[%%i]!/AWS/%yearmonth%/Metrics/data/*.csv'])"
set /a "i =1"
GOTO :myLoop
)
嗨,我正在嘗試創建一個批處理,以便我可以一次運行多個 BIG QUERY。上面我嘗試撰寫一個批處理腳本,將命令放入回圈中。
我正在嘗試通過使用 yearmonth 作為用戶輸入來創建表,然后創建陣列以創建具有不同客戶端名稱的表。
但是如果 i =0 ClientName[i] = abc 在我正在使用的呼叫查詢中,我無法列印!ClientName[%%i]! 列印,但它不作業。
當我執行 bat 檔案時,回圈內的呼叫查詢未在 GCP 控制臺中運行。
你能幫我解決這個問題嗎
uj5u.com熱心網友回復:
將
set變數作為獨立的字母字符(如i. 一個原因是正如您所經歷的那樣,您將formetavariable%%i與setvariable混淆了%i%。您正在回圈中擴展,但還沒有,
enabledelayedexpansion所以有兩種方法,我們將在一秒鐘內介紹。setting 變數之前或之后=不應該有空格set /a
所以,方法1,沒有delayedexpansion(注意變數如何%%在回圈中與call命令一起使用)。
@echo off
echo Give yearmonth "yyyyMM"
SET /p yearmonth=
SET ClientName[0]=abc
SET ClientName[1]=def
SET num=0
:myLoop
if defined ClientName[%num%] (
call bq query --use_legacy_sql=false "CREATE EXTERNAL TABLE `test.%%ClientName[%num%]%%.%yearmonth%` OPTIONS (format = 'CSV',skip_leading_rows = 1 uris = ['gs://test/%%ClientName[%num%]%%/AWS/%yearmonth%/Metrics/data/*.csv'])"
set /a num =1
GOTO :myLoop
)
方法2:(使用更好的方法delayedexpansion)
@echo off
setlocal enabledelayedexpansion
echo Give yearmonth "yyyyMM"
SET /p yearmonth=
SET ClientName[0]=abc
SET ClientName[1]=def
SET num=0
:myLoop
if defined ClientName[%num%] (
call bq query --use_legacy_sql=false "CREATE EXTERNAL TABLE `test.!ClientName[%num%]!.%yearmonth%` OPTIONS (format = 'CSV',skip_leading_rows = 1 uris = ['gs://test/!ClientName[%num%]!/AWS/%yearmonth%/Metrics/data/*.csv'])"
set /a num =1
GOTO :myLoop
)
uj5u.com熱心網友回復:
我會選擇更直觀的回圈機制:
For /F "Tokens=1,* Delims==" %%G In ('"(Set ClientName[) 2>NUL"') Do Call "%ProgramFiles%\Google\Cloud SDK\google-cloud-sdk\bin\bq.cmd" query --use_legacy_sql=false "CREATE EXTERNAL TABLE `test.%%G.%yearmonth%` OPTIONS (format = 'CSV',skip_leading_rows = 1 uris = ['gs://test/%%G/AWS/%yearmonth%/Metrics/data/*.csv'])"
它不需要延遲擴展或增加變數,如果沒有定義名為 ClientName[ n ]的變數,它什么也不做。
因此,這意味著您可以列出您的變數,但在某些時候注釋一個或多個:
Set "ClientName[0]=abc"
Set "ClientName[1]=def"
Rem Set "ClientName[2]=ghi"
Set "ClientName[3]=jkl"
當回圈運行時,與遞增機制不同,不會對帶括號的If Defined %ClientName[2]% …代碼塊進行不必要的決議。
但是,您的問題中有幾件事可能會引發潛在問題。最大的問題是您似乎期望用戶輸入,但同時相信輸入已提供,并且根據您的期望進行了格式化。在撰寫需要互動的腳本時,您必須始終假設最終用戶不能或不會始終遵循說明。
因此,這是一個設計更穩健的腳本來執行您的預期查詢:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Rem Define a variable named bq pointing to your bigquery batch file.
Set "bq=%ProgramFiles%\Google\Cloud SDK\google-cloud-sdk\bin\bq.cmd"
Rem If the bigquery batch file does not exist, terminate the script.
If Not Exist "%bq%" Exit /B
Rem Ensure that there are not existing ClientName[n] variables defined.
For /F "Delims=" %%G In ('"(Set ClientName[) 2>NUL"') Do Set "%%G="
Rem Define your ClientName[n] variable list.
Set "ClientName[0]=abc"
Set "ClientName[1]=def"
:GetYearMonth
Rem Ensure that no existing variable named YearMonth is defined.
Set "YearMonth="
Rem Request user input of date saved to the variable named YearMonth.
Set /P "YearMonth=Please provide the date in the format yyyyMM>"
Rem If no input was received, ask again.
If Not Defined YearMonth GoTo GetYearMonth
Rem Remove any doublequotes from the YearMonth variable value.
Set "YearMonth=%YearMonth:"=%"
Rem If YearMonth variable value is not within allowed range, ask again.
Rem This example uses 197001 - 202212
(Set YearMonth) 2>NUL | %SystemRoot%\System32\findstr.exe /R^
/C:"^YearMonth=19[789][0123456789]0[123456789]$"^
/C:"^YearMonth=19[789][0123456789]1[012]$"^
/C:"^YearMonth=20[01][0123456789]0[123456789]$"^
/C:"^YearMonth=20[01][0123456789]1[012]$"^
/C:"^YearMonth=202[012]0[123456789]$"^
/C:"^YearMonth=202[012]1[012]$"^
1>NUL || GoTo GetYearMonth
Rem As input information is valid loop your command.
For /F "Tokens=1,* Delims==" %%G In ('"(Set ClientName[) 2>NUL"'
) Do Call "%bq%" query --use_legacy_sql=false "CREATE EXTERNAL TABLE `test.%%G.%YearMonth%` OPTIONS (format = 'CSV',skip_leading_rows = 1 uris = ['gs://test/%%G/AWS/%YearMonth%/Metrics/data/*.csv'])"
EndLocal
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/406777.html
標籤:
上一篇:macOS上的Git推拉克隆命令在升級到Monterey后不執行任何操作
下一篇:在檔案中批量輸出變數
