Export to GCS (EXPORT DATA)¶
Demonstrates the GoogleSQL EXPORT DATA OPTIONS(...) AS SELECT statement
against the emulator, writing query results to a Cloud Storage URI as
CSV.
gs:// URIs resolve under BQEMU_GCS_LOCAL_ROOT, so the script starts an
in-process emulator rooted at a temporary directory, runs the export, and
reads the exported file straight off that directory.
The script:
- Starts an ephemeral emulator on a random port with
BQEMU_GCS_LOCAL_ROOTpointed at a temp directory. - Creates a dataset and a
customers (id INT64, name STRING)table and inserts three rows. - Runs
EXPORT DATA OPTIONS(uri='gs://my-bucket/exports/customers_*.csv', format='CSV', overwrite=true) AS SELECT ... ORDER BY idas a query job and waits for it to finish. - Asserts the job's
statement_typeisEXPORT_DATA, then reads the single shardcustomers_000000000000.csvoff the GCS root and checks the header and rows.
Run¶
make test runs python example.py. In CI the repo is installed in
editable mode (pip install -e ".[dev,all]"), which provides both
bqemulator and the google-cloud-bigquery client; make install
installs the pinned client from requirements.txt for a standalone run.
What to look for¶
- The export runs as an ordinary query job —
client.query(...)— not a load/extract job.job.statement_typeisEXPORT_DATAand the result has zero rows. - The single
*wildcard expands to a 12-digit counter, so a small result is one file namedcustomers_000000000000.csv. gs://my-bucket/exports/customers_000000000000.csvresolves to$BQEMU_GCS_LOCAL_ROOT/my-bucket/exports/customers_000000000000.csv— the export creates the parent directories automatically.- CSV defaults to
header=true, so the first line isid,name.
See the Exporting data guide for the full OPTIONS reference, sharding behaviour, and limitations.