问题
Is it possible to use an input file with the bigquery CLI?
bq query < my_query.sql
回答1:
If you're using unix (or have cygwin installed on windows), you can use xargs:
xargs -a my_query.sql -0 bq query
Alternately you can use back-ticks:
bq query `cat my_query.sql`
Note that bq can only process one command at a time -- if your .sql script has several queries, you'll need to split the file on ;
回答2:
On windows I am using this method. Prerequisite is to have each command listed in a single row. This will process each command in row one by one using bq.
C:\temp>for /F "tokens=*" %A in (batch_query.sql) do bq query %A
C:\temp>bq query select count+1 from cmdwh_bq_prod.newtable ;
Waiting on bqjob_r63bf8c82_00000163004c7fd0_1 ... (0s) Current status: DONE
+-------+
| f0_ |
+-------+
| 20136 |
+-------+
C:\temp>bq query select count+2 from cmdwh_bq_prod.newtable ;
Waiting on bqjob_r7ffd9b2a_00000163004c9348_1 ... (0s) Current status: DONE
+-------+
| f0_ |
+-------+
| 20137 |
+-------+
C:\temp>bq query select count+3 from cmdwh_bq_prod.newtable ;
Waiting on bqjob_r223c57a3_00000163004ca682_1 ... (0s) Current status: DONE
+-------+
| f0_ |
+-------+
| 20138 |
+-------+
C:\temp>
C:\temp>type batch_query.sql
select count+1 from cmdwh_bq_prod.newtable ;
select count+2 from cmdwh_bq_prod.newtable ;
select count+3 from cmdwh_bq_prod.newtable ;
C:\temp>bq query select * from cmdwh_bq_prod.newtable
Waiting on bqjob_r3a363e1b_00000163004f4dba_1 ... (0s) Current status: DONE
+-------+
| count |
+-------+
| 20135 |
+-------+
C:\temp>
来源:https://stackoverflow.com/questions/12323326/using-a-sql-source-file-with-the-bigquery-cli