> ## Documentation Index
> Fetch the complete documentation index at: https://docs.activeviam.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Connect to BigQuery

> How to connect Atoti DirectQuery to Google BigQuery, covering service account key file setup via `GOOGLE_APPLICATION_CREDENTIALS`, OAuth2 user credential authentication, and the BigQuery IAM permissions required for read sessions and job execution

## Credentials

Atoti DirectQuery delegates the credentials handling to a Google [BigQuery](https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery) Java client.

### Service account key file

A classic way to authenticate is to use a [Service Account key file](https://cloud.google.com/bigquery/docs/authentication/service-account-file).

This requires creating a Google Cloud service account with the correct permissions. The secret key is then saved locally and used.

There are two common ways to pass this service account key file to the BigQuery Java client:

* Put the path to the key file in the `GOOGLE_APPLICATION_CREDENTIALS` environment variable. See [Application Default Credentials](https://cloud.google.com/bigquery/docs/authentication/getting-started#application_default_credentials).
* Read the content of the key file and pass it to the client builder. See [Loading credentials from a file](https://cloud.google.com/bigquery/docs/authentication/service-account-file#loading_credentials_from_a_file).

### How to authenticate as a user

Instead of a service account, it is also possible to use the credentials of a specific user using OAuth2 flow.
This setup involves more steps and is described in the
[Google guide for authenticating as an end user](https://cloud.google.com/bigquery/docs/authentication/end-user-installed).

## Permissions

The service account or user used by DirectQuery must have the permissions to access the datasets and run jobs in BigQuery.

A simple way to achieve this is to give the [BigQuery User](https://cloud.google.com/bigquery/docs/access-control#bigquery.user) and [BigQuery Data Viewer](https://cloud.google.com/bigquery/docs/access-control#bigquery.dataViewer) roles to the user.

If those roles grant too many permissions, a role with this minimal set of permissions can be created instead:

* `bigquery.datasets.get`
* `bigquery.jobs.create`
* `bigquery.readsessions.create`
* `bigquery.readsessions.getData`
* `bigquery.readsessions.update`
* `bigquery.routines.get`
* `bigquery.tables.get`
* `bigquery.tables.getData`
* `bigquery.tables.list`
