I want to export query results from BigQuery to local file/Google storage.
I tried 'bq extract' command but it doesn't allow query as input.
Usage: bq extract <source_table> <destination_uris>
I don't want to extract the whole table as the table contains many columns which are not required and I need to aggregate the data.
As of now, the only workaround I could find is to create a table using the 'bq query' command and use the 'bq extract' to extract the data.
I'm looking for any better way to achieve this by doing something like below.
bq extract 'select dept_id,sum(sal) from temp.employee
group by dept_id' 'gs://XXXX/employee.csv'
Loading Datastore export service data. In the Google Cloud console, go to the BigQuery page. In the Explorer pane, expand your project, and then select a dataset. In the Dataset info section, click add_box Create table.
BigQuery stores table data in columnar format, meaning it stores each column separately. Column-oriented databases are particularly efficient at scanning individual columns over an entire dataset. Column-oriented databases are optimized for analytic workloads that aggregate data over a very large number of records.
If your data has more than 16,000 rows you'd need to save the result of your query as a BigQuery Table. Afterwards, export the data from the table into Google Cloud Storage using any of the available options (such as the Cloud Console, API, bq or client libraries).
Direct export from BigQuery Standard SQL was added recently: Exporting data to csv format
EXPORT DATA OPTIONS(
uri='gs://mybucket/myfolder2/*.csv',
format='CSV',
overwrite=true,
header=true,
field_delimiter=';') AS
SELECT 1 as field1, 2 as field2
You can export it using the EXPORT DATA which can enable writing query results directly to GCS, like
EXPORT DATA
[WITH CONNECTION connection_name]
OPTIONS (export_option_list) AS
query_statement
or using the Web UI in just three steps
When in BigQuery screen, before running the query go to More > Query Settings
This opens the following
Here you want to have
Then Save it and the Query is configured to be saved in a specific table. Now you can run the Query.
To export it to GCP you have to go to the table and click EXPORT > Export to GCS.
This opens the following screen
In Select GCS location you define the bucket, the folder and the file.
For instances, you have a bucket named daria_bucket (Use only lowercase letters, numbers, hyphens (-), and underscores (_). Dots (.) may be used to form a valid domain name.) and want to save the file(s) in the root of the bucket with the name test, then you write (in Select GCS location)
daria_bucket/test.csv
If the file is too big (more than 1 GB), you'll get an error. To fix it, you'll have to save it in more files using wildcard. So, you'll need to add *, just like that
daria_bucket/test*.csv
This is going to store, inside of the bucket daria_bucket, all the data extracted from the table in more than one file named test000000000000, test000000000001, test000000000002, ... testX.
Then go to Storage and you'll see the bucket.
Go inside of it and you'll find the one (or more) file(s). You can then download from there.
BigQuery does not provide ability to directly export/download query result to GCS or Local File. First you need to get result of query either in explicitly set destination table or if not set you can use temp (anonymous) table that holds query result - you can get it (table) from respective job attribute configuration.query.destinationTable
(after job is completed)
Then you can use that table as a source for Export job
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With