# Google BigQuery

The BigQuery data source connector loads data from [BigQuery](https://cloud.google.com/bigquery/) and brings it into Keboola. 
Running the connector creates a background job that

- executes the queries in Google BigQuery.
- saves the results to Google Cloud Storage.
- exports the results from Google Cloud Storage and stores them in specified tables in Keboola Storage.
- removes the results from Google Cloud Storage.

***Note:** Using the Google BigQuery connector is also described in our [Getting Started Tutorial](https://help.keboola.com/getting-started/ad-hoc/#using-bigquery-connector).*

**Before you start**

You need:

- **A Google Cloud project with billing enabled** that has the dataset you want to read, and rights to create a service account with a JSON key, grant it roles and create a Cloud Storage bucket. For example, the project Owner role, or Service Account Admin, Service Account Key Admin, Project IAM Admin and Storage Admin.
- **Service account key creation allowed in the project.** In organizations created on or after 3 May 2024, the policy `iam.disableServiceAccountKeyCreation` blocks it by default, so ask an organization admin for an exception.
- **A service account with a JSON key.** See Service account (#service-account) below.
- **A Cloud Storage bucket** for the temporary export files. See Bucket (#bucket) below.

What each tab needs:

- **Prompt tab:** Kai, the **Kai Agent** button in the top bar, on supported stacks. A new Free Plan project has it from the start; elsewhere an organization admin turns it on once: Get started with Kai (/kai/getting-started/).
- **UI tab:** a browser.
- **CLI / API tab:** kbagent (/cli/getting-started/), connected to your project. The checks only read.

### Service Account
To access and extract data from your BigQuery dataset, you need to set up a Google service account. Go 
to [**Google Cloud Platform Console > IAM & admin > Service accounts**](https://console.cloud.google.com/iam-admin/serviceaccounts)
and select the project you want the data source connector to have access to. Click **Create Service Account**
and enter a *Service account name* (e.g., `Keboola BigQuery connector`).

![Screenshot - Create service account](https://help.keboola.com/components/extractors/database/bigquery/googlecloud-1.png)

Then add the roles `BigQuery Data Editor`, `BigQuery Job User` and `Storage Object Admin`.

![Screenshot - Set admin roles](https://help.keboola.com/components/extractors/database/bigquery/googlecloud-2.png)

Finally, click **+ Create Key** to create a new JSON key, and then click **Create** to download it to your computer.

![Screenshot - Create JSON key](https://help.keboola.com/components/extractors/database/bigquery/googlecloud-3.png)

### Bucket
The data source connector uses a Google Cloud Storage bucket as a temporary storage for off-loading the data from BigQuery.
Go to the [**Google Cloud Platform Console > Storage >  Cloud Storage > Browser**](https://console.cloud.google.com/storage/browser)
and click **Create Bucket**. **Name** the bucket and select its **location** (must be the same as of your dataset).

![Screenshot - Google Cloud Platform](https://help.keboola.com/components/extractors/database/bigquery/googlecloud-4.png)

Do not set a retention policy on the bucket. The bucket contains only temporary data and no retention is needed.

## Configure Extraction
[Create a new configuration](https://help.keboola.com/components/configurations/#create-a-configuration) of the BigQuery connector.
Click **Set Service Account Key**.

![Screenshot - Set Service Account Key](https://help.keboola.com/components/extractors/database/bigquery/bigquery-1.png)

Open the downloaded key in a text editor, copy & paste it in the input field, and click **Submit**. 

![Screenshot - Copy & Paste Service Account Key](https://help.keboola.com/components/extractors/database/bigquery/bigquery-2.png)

Click **Save** to store the credentials.

**Important:** The private key is stored in an **encrypted** form and only the non-sensitive parts are visible in the UI
for your verification. The key can be deleted or replaced by a new one at any time.

![Screenshot - Save Service Account Key](https://help.keboola.com/components/extractors/database/bigquery/bigquery-3.png)

In the section **Unload Configuration**, enter `Cloud Storage Bucket Name` as the name of the bucket 
you [have created earlier](https://help.keboola.com/components/extractors/database/bigquery/#bucket), and select the correct *Dataset Location*. Click **Save**.

![Screenshot - Configure Bucket](https://help.keboola.com/components/extractors/database/bigquery/bigquery-4.png)

### Configure Queries
Start by clicking the button **Add Query**.

![Screenshot - Add Query](https://help.keboola.com/components/extractors/database/bigquery/bigquery-5.png)

Name the query and click **Create**.  

![Screenshot - Name Query](https://help.keboola.com/components/extractors/database/bigquery/bigquery-6.png)

To learn how to modify your configuration, go to the [SQL databases section](https://help.keboola.com/components/extractors/database/sqldb/#modify-configuration).

Click **Run Component**.

**Check:**

#### Prompt

```text title="Prompt" wrap
Did the latest job of my Google BigQuery configuration succeed, and which tables did it load, with row counts?
```

Kai only reads here, so it asks for no approval, and its answer links to what it found in your project.

#### UI

The job ends with status **Success** and Storage has one table per query, with the query's result.

#### CLI / API

```bash
kbagent job list --component-id keboola.ex-google-bigquery-v2 --config-id 123456789 --limit 1
kbagent storage tables --bucket-id in.c-keboola-ex-google-bigquery-v2-123456789
```

Replace `123456789` with your configuration ID: `status` shows whether the job succeeded, and `id` lists the table it loaded for each query, with its `rows_count`.

---

## Avro files variant

The **Google BigQuery avro files** connector (`keboola.ex-google-bigquery-v2-avro-files`) is configured the same way, with the service account, the bucket and the queries, but it does not write the query results into Storage tables. Each query's result is exported to one Avro file in [Storage Files](https://help.keboola.com/storage/files/) instead. Use it when the next step of your pipeline works with files, for example a transformation or a [processor](https://help.keboola.com/extend/component/processors/) that reads Avro.

Because each result goes to a single file, it is bound by BigQuery's limit of [1 GB per exported file](https://cloud.google.com/bigquery/docs/exporting-data); a larger result fails. The main connector unloads its results into several files through a wildcard, so this limit does not apply to it.
