Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Bigquery BQ command line execute query from a file

I use the bq command line tool to run queries, e.g:

bq query "select * from table"

What if I store the query in a file and run the query from that file? is there a way to do that?

like image 709
Echo Li Avatar asked Oct 29 '15 23:10

Echo Li


2 Answers

The other answers seem to be either outdated or needlessly brittle. As of 2019, bq query reads from stdin, so you can just redirect your file into it:

bq query < myfile.sql

Query parameters are passed like this:

bq query --parameter name:type:value < myfile.sql
like image 104
Thomas Avatar answered Oct 04 '22 03:10

Thomas


If you are using standard sql (Not Legacy Sql).

**Steps:**

1. Create .sql file (you can you any extension).
2. Put your query in that. Make sure (;) at the end of the query.
3. Go to command line ad execute below commands.
4. If you want add parameter then you have to specify sequentially.

Example:

bq query --use_legacy_sql=False  "$(cat /home/airflow/projects/bql/query/test.sql)"

for parameter
bq query --use_legacy_sql=False  --parameter=country::USA  "$(cat /home/airflow/projects/bql/query/test.sql)"

cat >/home/airflow/projects/bql/query/test.sql
select * from l1_gcb_trxn.account where country=@country;
like image 24
user3858193 Avatar answered Oct 04 '22 05:10

user3858193