|
| 1 | +# ------------------------------------------------------------ |
| 2 | +# OPS EXTERNALIZED TABLES (For metadata caching) |
| 3 | +# ------------------------------------------------------------ |
| 4 | + |
| 5 | +resource "google_bigquery_connection" "biglake_connection" { |
| 6 | + connection_id = "ops_biglake_connection" |
| 7 | + location = var.region |
| 8 | + friendly_name = "BigLake Connection for GCS Parquet Scanning" |
| 9 | + cloud_resource {} |
| 10 | +} |
| 11 | + |
| 12 | +# Enable connection service to access pipeline bucket |
| 13 | +resource "google_storage_bucket_iam_member" "biglake_storage_viewer" { |
| 14 | + bucket = google_storage_bucket.ops_pipeline_bucket.name |
| 15 | + role = "roles/storage.objectViewer" |
| 16 | + member = "serviceAccount:${google_bigquery_connection.biglake_connection.cloud_resource[0].service_account_id}" |
| 17 | +} |
| 18 | + |
| 19 | +resource "google_bigquery_dataset" "silver_dataset" { |
| 20 | + dataset_id = var.bq_dataset_id |
| 21 | + location = var.region |
| 22 | + |
| 23 | + delete_contents_on_destroy = false |
| 24 | +} |
| 25 | + |
| 26 | +locals { |
| 27 | + external_tables = [ |
| 28 | + "df_orders", |
| 29 | + "df_customers", |
| 30 | + "df_order_items", |
| 31 | + "df_products", |
| 32 | + "df_payments" |
| 33 | + ] |
| 34 | +} |
| 35 | + |
| 36 | +resource "google_bigquery_table" "external_tables" { |
| 37 | + for_each = toset(local.external_tables) |
| 38 | + dataset_id = google_bigquery_dataset.silver_dataset.dataset_id |
| 39 | + table_id = each.key |
| 40 | + |
| 41 | + # Might throw error if contracted/ is empty |
| 42 | + external_data_configuration { |
| 43 | + autodetect = true |
| 44 | + source_format = "PARQUET" |
| 45 | + connection_id = google_bigquery_connection.biglake_connection.name |
| 46 | + source_uris = ["gs://${google_storage_bucket.ops_pipeline_bucket.name}/contracted/${each.key}_*.parquet"] |
| 47 | + |
| 48 | + # Triggered manually by pipeline |
| 49 | + metadata_cache_mode = "MANUAL" |
| 50 | + } |
| 51 | + lifecycle { |
| 52 | + prevent_destroy = true |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | + |
| 57 | +# ------------------------------------------------------------ |
| 58 | +# BIGQUERY SEMANTTIC DATASETS (For table versionining) |
| 59 | +# ------------------------------------------------------------ |
| 60 | + |
| 61 | +locals { |
| 62 | + # Expiration for versioned tables |
| 63 | + one_month_ms = 2678400000 |
| 64 | + |
| 65 | + semantic_datasets = [ |
| 66 | + "seller_semantic", |
| 67 | + "customer_semantic", |
| 68 | + "product_semantic" |
| 69 | + ] |
| 70 | +} |
| 71 | + |
| 72 | +resource "google_bigquery_dataset" "semantic_datasets" { |
| 73 | + for_each = toset(local.semantic_datasets) |
| 74 | + dataset_id = each.key |
| 75 | + location = var.region |
| 76 | + |
| 77 | + delete_contents_on_destroy = false |
| 78 | + default_table_expiration_ms = local.one_month_ms |
| 79 | + |
| 80 | + description = "Semantic layer for ${each.key}. Tables expire after 1 month." |
| 81 | + |
| 82 | + labels = { |
| 83 | + env = var.environment |
| 84 | + layer = "semantic" |
| 85 | + } |
| 86 | + |
| 87 | + lifecycle { |
| 88 | + prevent_destroy = true |
| 89 | + } |
| 90 | +} |
0 commit comments