Google Bigquery BQ command line execute query from a file

后端 未结 6 501
天涯浪人
天涯浪人 2020-12-19 04:49

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

相关标签:
6条回答
  • 2020-12-19 05:15
    bq query --replace --use_legacy_sql=false --destination_table=syw-analytics:store_ranking.SHC_ENGAGEMENT_RANKING_TEST
    "SELECT RED,
            DEC,
            REDEM
    from `\syw.abc.xyz\`"
    
    0 讨论(0)
  • 2020-12-19 05:19

    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
    
    0 讨论(0)
  • 2020-12-19 05:29

    There is another way.

    Try this:

    bq query --flagfile=[your file with absolute path]
    

    Ex:

    bq query --flagfile=/home/user/abc.sql
    
    0 讨论(0)
  • 2020-12-19 05:30

    This thread offers good solution

    bq query `cat my_query.sql`
    
    0 讨论(0)
  • 2020-12-19 05:33

    You can run a query from a text file with a little bit of shell magic:

    $ echo "SELECT 17" > qq.txt
    $ bq query "$(cat qq.txt)"
    
    Waiting on bqjob_r603d91b7e0435a0f_00000150c56689c6_1 ... (0s) Current status: DONE   
    +-----+
    | f0_ |
    +-----+
    |  17 |
    +-----+
    

    Note this works on any unix variant (including mac). If you're using a windows, this should work under powershell but not the default cmd prompt.

    0 讨论(0)
  • 2020-12-19 05:36

    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;
    
    0 讨论(0)
提交回复
热议问题