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

# BigQuery

## Connecting to BigQuery

For an overview of the connector, please refer to the following link.

* [Connecting to DB/SaaS](/docs/en/develop/guides/integration)

<Tabs>
  <Tab title="Dashboard">
    <Steps>
      <Step title="Create a Connector">
        Select the "Connectors" tab on the top page and press the "Create" button.

        <img src="https://mintcdn.com/queue-4c50ebb3/evgQaQjX53Vch8Y5/assets/images/docs/advanced/connectors/connect-data-platform-connection.png?fit=max&auto=format&n=evgQaQjX53Vch8Y5&q=85&s=19c6e7afc70da565c2a8ab4f75469549" alt="Connection" width="1907" height="711" data-path="assets/images/docs/advanced/connectors/connect-data-platform-connection.png" />
      </Step>

      <Step title="Enter BigQuery Credentials">
        BigQuery offers two authentication methods.

        ### Connecting with Service Account

        Select BigQuery (Service Account) and enter the credentials in the displayed form.

        <img src="https://mintcdn.com/queue-4c50ebb3/evgQaQjX53Vch8Y5/assets/images/docs/advanced/connectors/connect-data-platform-bigquery-sa-create.png?fit=max&auto=format&n=evgQaQjX53Vch8Y5&q=85&s=b046c1f44fbf3251f54f264a352c08ce" alt="BigQuery Service Account Connection Create" width="1711" height="830" data-path="assets/images/docs/advanced/connectors/connect-data-platform-bigquery-sa-create.png" />

        | Field Name   | Description          | Required | Example                   |
        | ------------ | -------------------- | -------- | ------------------------- |
        | `Credential` | Service Account JSON | ✅        | `{"project_id": "", ...}` |

        For creating a service account, please refer to the official page.

        * [Create a Service Account](https://cloud.google.com/iam/docs/service-accounts-create?hl=en)

        ### Connecting with OAuth

        Select BigQuery (OAuth) and press the displayed sign-in button to authenticate with Google.

        <img src="https://mintcdn.com/queue-4c50ebb3/evgQaQjX53Vch8Y5/assets/images/docs/advanced/connectors/connect-data-platform-bigquery-oauth-create.png?fit=max&auto=format&n=evgQaQjX53Vch8Y5&q=85&s=6c825cba760fc919692c6a31e0560f56" alt="BigQuery OAuth Connection Create" width="1684" height="795" data-path="assets/images/docs/advanced/connectors/connect-data-platform-bigquery-oauth-create.png" />

        The following fields are also required.

        | Field Name   | Description  | Required | Example           |
        | ------------ | ------------ | -------- | ----------------- |
        | `Project ID` | Project ID   | ✅        | `demo-project`    |
        | `Dataset`    | Dataset Name |          | `demo_dataset`    |
        | `Location`   | Location     |          | `asia-northeast1` |

        After entering the information, press the Create button to complete the creation. If there are any errors, please check the connection details again.
      </Step>

      <Step title="Connect to the Created Database">
        Once created successfully, you can retrieve the connector name from the list. The string next to the icon is `connection_name`. You can use this to connect to the database in your code.

        <img src="https://mintcdn.com/queue-4c50ebb3/evgQaQjX53Vch8Y5/assets/images/docs/advanced/connectors/connect-data-platform-bigquery-complete.png?fit=max&auto=format&n=evgQaQjX53Vch8Y5&q=85&s=2657675774cb21676697dfa1f27e85ad" alt="Snowflake Connection Create Complete" width="3010" height="996" data-path="assets/images/docs/advanced/connectors/connect-data-platform-bigquery-complete.png" />

        Enter the created connector name in SQL or Python code to retrieve data.

        <CodeGroup>
          ```sql SQL theme={"dark"}
          {{
              config(
                  connection="connection_name"
              )
          }}

          select * from table_name
          ```

          ```python Python theme={"dark"}
          import morph
          from morph import MorphGlobalContext
          from morph_lib.database import execute_sql


          @morph.func
          def main(context: MorphGlobalContext):
              df = execute_sql(
                  sql="SELECT * from table_name"
                  connection="connection_name"
              )
              return df
          ```
        </CodeGroup>
      </Step>
    </Steps>
  </Tab>

  <Tab title="Local Version">
    <Info>Currently, only the Service Account method can be created via command in the local version. For other methods, please use the dashboard or directly write to `~/.morph/connections.yml`.</Info>

    <Steps>
      <Step title="Run the morph init Command">
        Run the `morph init` command to save the DB connection information to `~/.morph/connections.yml`.

        ```bash Shell theme={"dark"}
        morph init
        ```

        <Warning>
          If you have not installed the morph package, please install it with the following command before proceeding.

          ```bash shell theme={"dark"}
          pip install morph-data
          ```
        </Warning>

        A list of database types will be displayed in the interactive interface, select Snowflake (User/Password).

        ```bash Shell theme={"dark"}
        Select your database type:
        1. PostgreSQL
        2. MySQL
        3. Redshift
        4. SQLServer
        5. Snowflake (User/Password)
        6. BigQuery (Service Account)
        Enter the number corresponding to your database type: 6

        BigQuery (Service Account) selected.
        ```

        Next, enter the credentials. `slug` is treated as the connector name, which you specify in SQL or Python.

        Replace the following input examples with actual values that can connect.

        ```bash shell theme={"dark"}
        Create a slug for your connection: bigquery-connection
        Enter your BigQuery project ID: demo-project
        Enter your BigQuery keyfile path: ~/path_to_pem_file
        Enter your BigQuery dataset name (Optional): demo_dataset
        Enter your BigQuery location (Optional): asia-northeast1
        ```

        Once the credentials are saved, the following message will be displayed.

        ```bash shell theme={"dark"}
        Successfully initialized! 🎉
        You can edit your connection details in `path_to_connections.yml`
        ```

        `connections.yml` is saved as follows. If other connectors are created, they will be added under `connections`.

        ```bash shell theme={"dark"}
        cat ~/.morph/connections.yml
        ```

        ```yaml theme={"dark"}
        connections:
          bigquery-connection:
            dataset: demo_dataset
            keyfile: ~/path_to_pem_file
            location: asia-northeast1
            method: bigquery_service_account
            project: demo-project
            type: bigquery
        ```
      </Step>

      <Step title="Retrieve Data Using the Connector in Code">
        Enter the created connector name in SQL or Python code to retrieve data.

        <CodeGroup>
          ```sql SQL theme={"dark"}
          {{
              config(
                  connection="connection_name"
              )
          }}

          select * from table_name
          ```

          ```python Python theme={"dark"}
          import morph
          from morph import MorphGlobalContext
          from morph_lib.database import execute_sql


          @morph.func
          def main(context: MorphGlobalContext):
              df = execute_sql(
                  sql="SELECT * from table_name"
                  connection="connection_name"
              )
              return df
          ```
        </CodeGroup>
      </Step>
    </Steps>

    <Warning>
      When actually specifying the connector and executing the file, the priority is as follows.
      Also, please note that only connectors created in the dashboard can be used in the environment where `morph deploy` is performed.

      1. Connectors listed in `~/.morph/connections.yml` on the local machine
      2. Connectors registered in the cloud
    </Warning>

    ### How to Write for OAuth

    When using OAuth, directly edit `~/.morph/connections.yml`.

    Refreshing the access token is a dashboard feature, so the one described will always be used in the local environment.

    **OAuth Method**

    ```yml theme={"dark"}
    connections:
      bigquery-oauth-connection
        type: bigquery # Fixed
        method: bigquery_oauth # Fixed
        project: str
        refresh_token: str
        client_id: str
        client_secret: str
        redirect_uri: str
        dataset: str # Optional
        location: str # Optional
        access_token: str # Optional
    ```
  </Tab>
</Tabs>
