Skip to main content

Command Palette

Search for a command to run...

AWS Athena equivalent in GCP

Updated
•2 min read•View as Markdown
AWS Athena equivalent in GCP

Today, we want to explore a tool equivalent to AWS Athena in GCP. To do so we can query data directly from Google Cloud Storage (GCS) files in GCP using BigQuery's External Tables feature.

Here's how it works:

  1. Create an External Table:

    • You create a virtual table in BigQuery that points to your GCS data.

    • You define the schema (data types of each column) for this external table.

  2. Query the External Table:

    • You can then query this external table using standard SQL queries.

    • BigQuery will read the data directly from the GCS files when executing the query.

Example:

Let's say you have a CSV file in GCS at gs://my-bucket/data/my_data.csv.

1. Create an External Table (using the bq command-line tool):

bq mk --external_table \
    your_project_id:your_dataset.my_external_table \
    gs://my-bucket/data/my_data.csv \
    --schema 'field1:STRING,field2:INTEGER,field3:FLOAT' \
    --destination_table your_project_id:your_dataset.my_external_table

2. Query the External Table:

SELECT * 
FROM your_project_id.your_dataset.my_external_table 
WHERE field2 > 100;

Things to Considers:

  • Data Formats: BigQuery supports various file formats for external tables, including CSV, JSON, Avro, Parquet, and ORC.

  • Schema Definition: Accurately defining the schema is crucial for correct data interpretation.

  • Performance:

    • Query performance can be impacted by factors like file size, data format, and query complexity.

    • Consider using optimized formats like Parquet or Avro for better performance.

  • Data Updates: If your GCS files are updated, you'll need to refresh the external table in BigQuery to reflect the changes.

Benefits of Using External Tables:

  • Reduced Data Ingestion Overhead: Avoids the need to load data into a managed table before querying.

  • Flexibility: Easily query data directly from GCS without the need for data movement.

  • Cost-Effectiveness: Can be more cost-effective for ad-hoc queries or when you don't need to frequently analyze the same data.

By leveraging BigQuery's external tables feature, we can efficiently query data directly from your GCS files, providing a flexible and cost-effective solution for our data analysis needs.

More from this blog

Engineering

11 posts