How to create BiqQuery view from SQL source in a file (Windows command line)

Viewed 1278

I created a number of BigQuery views and all works well. I need to move the SQL source for the queries into my source control and manage changes from there. Is there a way to create/update a view from the command line using the source from a file? The bq mk command seems to only allow the SQL code to be inline on the command line --view keyword. Some of my views are quite lengthy and I'm sure there are characters that would need to be escaped - which I obviously don't want to get into. I'm running on Windows. Thanks

2 Answers

Simply use the flagfile parameter:

bq mk --help:

--flagfile: Insert flag definitions from the given file into the command line.

bq mk --view --flagfile=<path_to_to_your_file> dataset.newview

Let us assume that file MyQuery.sql contains view definition.

Create a Script file script.sh with below contents

query=`cat MyQuery.sql`    
bq mk --use_legacy_sql=false --view "$query"  dataset.myview

Run using command sh script.sh

This worked for me in Shell..!! You can make necessary changes for Windows..!!

Related