Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for BigQuery external tables on Google Sheets using python #212

Open
daniel-bartley opened this issue May 29, 2023 · 3 comments
Open
Labels
bigquery enhancement New feature or request triage

Comments

@daniel-bartley
Copy link

Describe the feature

It's not yet possible to create an external table using sql.
However Google provides Python script that does the job.

Describe alternatives you've considered

https://cloud.google.com/bigquery/docs/external-data-drive#create_external_tables

from google.cloud import bigquery
import google.auth

# Create credentials with Drive & BigQuery API scopes.
# Both APIs must be enabled for your project before running this code.
credentials, project = google.auth.default(
    scopes=[
        "https://www.googleapis.com/auth/drive",
        "https://www.googleapis.com/auth/bigquery",
    ]
)

# Construct a BigQuery client object.
client = bigquery.Client(credentials=credentials, project=project)

# TODO(developer): Set dataset_id to the ID of the dataset to fetch.
# dataset_id = "your-project.your_dataset"

# Configure the external data source.
dataset = client.get_dataset(dataset_id)
table_id = "us_states"
schema = [
    bigquery.SchemaField("name", "STRING"),
    bigquery.SchemaField("post_abbr", "STRING"),
]
table = bigquery.Table(dataset.table(table_id), schema=schema)
external_config = bigquery.ExternalConfig("GOOGLE_SHEETS")
# Use a shareable link or grant viewing access to the email address you
# used to authenticate with BigQuery (this example Sheet is public).
sheet_url = (
    "https://docs.google.com/spreadsheets"
    "/d/1i_QCL-7HcSyUZmIbP9E6lO_T5u3HnpLe7dnpHaijg_E/edit?usp=sharing"
)
external_config.source_uris = [sheet_url]
external_config.options.skip_leading_rows = 1  # Optionally skip header row.
external_config.options.range = (
    "us-states!A20:B49"  # Optionally set range of the sheet to query from.
)
table.external_data_configuration = external_config

# Create a permanent table linked to the Sheets file.
table = client.create_table(table)  # Make an API request.

# Example query to find states starting with "W".
sql = 'SELECT * FROM `{dataset_id}.{table_id}` WHERE name LIKE "W%"'

query_job = client.query(sql)  # Make an API request.

# Wait for the query to complete.
w_states = list(query_job)
print(
    f"There are {len(w_states)} states with names starting with W in the selected range."
)

Additional context

Yes. BigQuery specific.

Who will this benefit?

BigQuery external tables over Google Sheets provides a user friendly endpoint to and mechanism to get spreadsheet data into BigQuery. Creating the external tables in dbt makes them more reproducible in different environments.

@daniel-bartley daniel-bartley added enhancement New feature or request triage labels May 29, 2023
@amirbtb
Copy link

amirbtb commented Oct 18, 2023

Hi,
I found this gist from @thomashandorf :
https://gist.github.com/thomashandorf/0d54848ccd84448001abb2a51d48794a

It seems that it's now possible to use SQL to create BigQuery external tables over Google Sheets but I couldn't find anything about this in BigQuery documentation.

Thank you @thomashandorf for that gist 🙏🏽 !

Copy link

github-actions bot commented Oct 3, 2024

This issue has been marked as Stale because it has been open for 180 days with no activity. If you would like the issue to remain open, please comment on the issue or else it will be closed in 7 days.

@github-actions github-actions bot added the Stale label Oct 3, 2024
@geo909
Copy link

geo909 commented Oct 3, 2024

🆙

I would like to see this feature implemented, too.

@github-actions github-actions bot removed the Stale label Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bigquery enhancement New feature or request triage
Projects
None yet
Development

No branches or pull requests

4 participants