---
name: bq_export_to_gcs
kind: function
lang: py
domain: infra
version: "1.0.0"
purity: impure
signature: "def bq_export_to_gcs(client: BQClient, dataset_id: str, table_id: str, destination_uri: str, destination_format: str = 'CSV', compression: str = 'NONE') -> dict"
description: "Exporta una tabla BigQuery a Google Cloud Storage usando extract_table del SDK. Soporta CSV, JSON, Avro y Parquet con compresion opcional."
tags: [bigquery, gcp, export, gcs, google-cloud, python, etl, pendiente-usar, sink]
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: [google-cloud-bigquery]
params:
  - name: client
    desc: "cliente BQClient autenticado contra el proyecto GCP"
  - name: dataset_id
    desc: "ID del dataset de origen en BigQuery"
  - name: table_id
    desc: "ID de la tabla de origen en BigQuery"
  - name: destination_uri
    desc: "URI de destino en GCS; para tablas grandes usar wildcard: gs://bucket/prefix_*.csv"
  - name: destination_format
    desc: "formato de exportacion: CSV, NEWLINE_DELIMITED_JSON, AVRO, PARQUET"
  - name: compression
    desc: "algoritmo de compresion: NONE, GZIP, SNAPPY (solo Parquet/Avro), DEFLATE"
output: "dict con {job_id: ID del ExtractJob, destination_uri: URI de destino, status: DONE o FAILED}"
tested: false
tests: []
test_file_path: ""
file_path: "python/functions/bigquery/queries.py"
---

## Ejemplo

```python
from bigquery.client import bq_auth
from bigquery.queries import bq_export_to_gcs

client = bq_auth("my-project")

# Exportar como CSV comprimido con GZIP
result = bq_export_to_gcs(
    client,
    "my_dataset", "users",
    "gs://my-bucket/exports/users_*.csv.gz",
    compression="GZIP",
)
print(f"Exportado: {result['destination_uri']} — job: {result['job_id']}")

# Exportar como Parquet con compresion Snappy
result = bq_export_to_gcs(
    client,
    "my_dataset", "events",
    "gs://my-bucket/exports/events_*.parquet",
    destination_format="PARQUET",
    compression="SNAPPY",
)
print(f"Status: {result['status']}")
```

## Notas

Para tablas grandes BigQuery genera multiples archivos automaticamente cuando se usa
wildcard (`*`) en el URI. Sin wildcard falla si la tabla supera 1 GB.

`SNAPPY` solo es valido para formatos binarios (PARQUET, AVRO). Usar `GZIP` para CSV
y NEWLINE_DELIMITED_JSON.

El ExtractJob es asincrono en BigQuery; `job.result()` bloquea hasta completar.
